From 4c443d51ce52431e52dbd9e9b9313746a6248825 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 29 Apr 2020 16:06:38 +0300 Subject: [PATCH] Minor JoyKit improvements --- JoyKit/JOYAxes2D.m | 27 ++++++++++++++++++++------- JoyKit/JOYController.m | 3 +++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/JoyKit/JOYAxes2D.m b/JoyKit/JOYAxes2D.m index 28ce16c..624ccef 100644 --- a/JoyKit/JOYAxes2D.m +++ b/JoyKit/JOYAxes2D.m @@ -125,23 +125,21 @@ double old1 = _state1, old2 = _state2; { - double min = [self effectiveMinX]; - double max = [self effectiveMaxX]; - if (min == max) return false; int32_t value = x; - + if (initialX != 0) { minX = MIN(value, minX); maxX = MAX(value, maxX); } + double min = [self effectiveMinX]; + double max = [self effectiveMaxX]; + if (min == max) return false; + _state1 = (value - min) / (max - min) * 2 - 1; } { - double min = [self effectiveMinY]; - double max = [self effectiveMaxY]; - if (min == max) return false; int32_t value = y; if (initialY != 0) { @@ -149,8 +147,23 @@ maxY = MAX(value, maxY); } + double min = [self effectiveMinY]; + double max = [self effectiveMaxY]; + if (min == max) return false; + _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; } diff --git a/JoyKit/JOYController.m b/JoyKit/JOYController.m index d5e1acb..2b21861 100644 --- a/JoyKit/JOYController.m +++ b/JoyKit/JOYController.m @@ -382,11 +382,13 @@ typedef struct __attribute__((packed)) { - (NSString *)deviceName { + if (!_device) return nil; return IOHIDDeviceGetProperty(_device, CFSTR(kIOHIDProductKey)); } - (NSString *)uniqueID { + if (!_device) return nil; NSString *serial = (__bridge NSString *)IOHIDDeviceGetProperty(_device, CFSTR(kIOHIDSerialNumberKey)); if (!serial || [(__bridge NSString *)IOHIDDeviceGetProperty(_device, CFSTR(kIOHIDTransportKey)) isEqualToString:@"USB"]) { serial = [NSString stringWithFormat:@"%04x%04x%08x", @@ -581,6 +583,7 @@ typedef struct __attribute__((packed)) { - (void)sendReport:(NSData *)report { if (!report.length) return; + if (!_device) return; IOHIDDeviceSetReport(_device, kIOHIDReportTypeOutput, *(uint8_t *)report.bytes, report.bytes, report.length); }