Minor JoyKit improvements

This commit is contained in:
Lior Halphon 2020-04-29 16:06:38 +03:00
parent 6448a692e2
commit 4c443d51ce
2 changed files with 23 additions and 7 deletions

View File

@ -125,23 +125,21 @@
double old1 = _state1, old2 = _state2; double old1 = _state1, old2 = _state2;
{ {
double min = [self effectiveMinX];
double max = [self effectiveMaxX];
if (min == max) return false;
int32_t value = x; int32_t value = x;
if (initialX != 0) { if (initialX != 0) {
minX = MIN(value, minX); minX = MIN(value, minX);
maxX = MAX(value, maxX); maxX = MAX(value, maxX);
} }
double min = [self effectiveMinX];
double max = [self effectiveMaxX];
if (min == max) return false;
_state1 = (value - min) / (max - min) * 2 - 1; _state1 = (value - min) / (max - min) * 2 - 1;
} }
{ {
double min = [self effectiveMinY];
double max = [self effectiveMaxY];
if (min == max) return false;
int32_t value = y; int32_t value = y;
if (initialY != 0) { if (initialY != 0) {
@ -149,8 +147,23 @@
maxY = MAX(value, maxY); maxY = MAX(value, maxY);
} }
double min = [self effectiveMinY];
double max = [self effectiveMaxY];
if (min == max) return false;
_state2 = (value - min) / (max - min) * 2 - 1; _state2 = (value - min) / (max - min) * 2 - 1;
} }
if (_state1 < -1 || _state1 > 1 ||
_state2 < -1 || _state2 > 1) {
// Makes no sense, recalibrate
_state1 = _state2 = 0;
initialX = initialY = 0;
minX = _element1.max;
minY = _element2.max;
maxX = _element1.min;
maxY = _element2.min;
}
return old1 != _state1 || old2 != _state2; return old1 != _state1 || old2 != _state2;
} }

View File

@ -382,11 +382,13 @@ typedef struct __attribute__((packed)) {
- (NSString *)deviceName - (NSString *)deviceName
{ {
if (!_device) return nil;
return IOHIDDeviceGetProperty(_device, CFSTR(kIOHIDProductKey)); return IOHIDDeviceGetProperty(_device, CFSTR(kIOHIDProductKey));
} }
- (NSString *)uniqueID - (NSString *)uniqueID
{ {
if (!_device) return nil;
NSString *serial = (__bridge NSString *)IOHIDDeviceGetProperty(_device, CFSTR(kIOHIDSerialNumberKey)); NSString *serial = (__bridge NSString *)IOHIDDeviceGetProperty(_device, CFSTR(kIOHIDSerialNumberKey));
if (!serial || [(__bridge NSString *)IOHIDDeviceGetProperty(_device, CFSTR(kIOHIDTransportKey)) isEqualToString:@"USB"]) { if (!serial || [(__bridge NSString *)IOHIDDeviceGetProperty(_device, CFSTR(kIOHIDTransportKey)) isEqualToString:@"USB"]) {
serial = [NSString stringWithFormat:@"%04x%04x%08x", serial = [NSString stringWithFormat:@"%04x%04x%08x",
@ -581,6 +583,7 @@ typedef struct __attribute__((packed)) {
- (void)sendReport:(NSData *)report - (void)sendReport:(NSData *)report
{ {
if (!report.length) return; if (!report.length) return;
if (!_device) return;
IOHIDDeviceSetReport(_device, kIOHIDReportTypeOutput, *(uint8_t *)report.bytes, report.bytes, report.length); IOHIDDeviceSetReport(_device, kIOHIDReportTypeOutput, *(uint8_t *)report.bytes, report.bytes, report.length);
} }