From 66112af37e330f322be3a6ff16bb768e16f0e0f3 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 29 Apr 2020 19:53:47 +0300 Subject: [PATCH] Fix PWM performence issue --- JoyKit/JOYController.m | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/JoyKit/JOYController.m b/JoyKit/JOYController.m index 2b218618..c18fbdb1 100644 --- a/JoyKit/JOYController.m +++ b/JoyKit/JOYController.m @@ -115,6 +115,7 @@ typedef struct __attribute__((packed)) { volatile double _rumblePWMRatio; bool _physicallyConnected; bool _logicallyConnected; + bool _rumblePWMThreadRunning; } - (instancetype)initWithDevice:(IOHIDDeviceRef) device @@ -609,7 +610,7 @@ typedef struct __attribute__((packed)) { [NSThread sleepForTimeInterval:(1 - _rumblePWMRatio) / 10]; } [_rumblePWMThreadLock lock]; - [_rumblePWMThreadLock signal]; + _rumblePWMThreadRunning = false; [_rumblePWMThreadLock unlock]; } @@ -657,23 +658,23 @@ typedef struct __attribute__((packed)) { } else { if (_rumbleElement.max == 1 && _rumbleElement.min == 0) { - if (_rumblePWMRatio == 0) { // PWM thread not running, start it. + [_rumblePWMThreadLock lock]; + if (!_rumblePWMThreadRunning) { // PWM thread not running, start it. if (amp != 0) { _rumblePWMRatio = amp; + _rumblePWMThreadRunning = true; [self performSelectorInBackground:@selector(pwmThread) withObject:nil]; } } else { if (amp == 0) { // Thread is running, signal it to stop - [_rumblePWMThreadLock lock]; _rumblePWMRatio = 0; - [_rumblePWMThreadLock wait]; - [_rumblePWMThreadLock unlock]; } else { _rumblePWMRatio = amp; } } + [_rumblePWMThreadLock unlock]; } else { [_rumbleElement setValue:amp * (_rumbleElement.max - _rumbleElement.min) + _rumbleElement.min];