Fixed spamming the controller with reset IMU commands when they are failing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
diff --git a/src/joystick/hidapi/SDL_hidapi_switch.c b/src/joystick/hidapi/SDL_hidapi_switch.c
index 45b7bb1..baf0d62 100644
--- a/src/joystick/hidapi/SDL_hidapi_switch.c
+++ b/src/joystick/hidapi/SDL_hidapi_switch.c
@@ -270,6 +270,7 @@ typedef struct {
SDL_bool m_bReportSensors;
SDL_bool m_bHasSensorData;
Uint32 m_unLastInput;
+ Uint32 m_unLastIMUReset;
SwitchInputOnlyControllerStatePacket_t m_lastInputOnlyState;
SwitchSimpleStatePacket_t m_lastSimpleState;
@@ -1369,7 +1370,7 @@ HIDAPI_DriverSwitch_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joysti
/* Set up for input */
ctx->m_bSyncWrite = SDL_FALSE;
- ctx->m_unLastInput = SDL_GetTicks();
+ ctx->m_unLastIMUReset = ctx->m_unLastInput = SDL_GetTicks();
return SDL_TRUE;
@@ -2005,16 +2006,22 @@ static void HandleFullControllerState(SDL_Joystick *joystick, SDL_DriverSwitch_C
} else if (ctx->m_bHasSensorData) {
/* Uh oh, someone turned off the IMU? */
- SDL_HIDAPI_Device *device = ctx->device;
+ const Uint32 IMU_RESET_DELAY_MS = 3000;
+ Uint32 now = SDL_GetTicks();
- if (device->updating) {
- SDL_UnlockMutex(device->dev_lock);
- }
+ if (SDL_TICKS_PASSED(now, ctx->m_unLastIMUReset + IMU_RESET_DELAY_MS)) {
+ SDL_HIDAPI_Device *device = ctx->device;
+
+ if (device->updating) {
+ SDL_UnlockMutex(device->dev_lock);
+ }
- SetIMUEnabled(ctx, SDL_TRUE);
+ SetIMUEnabled(ctx, SDL_TRUE);
- if (device->updating) {
- SDL_LockMutex(device->dev_lock);
+ if (device->updating) {
+ SDL_LockMutex(device->dev_lock);
+ }
+ ctx->m_unLastIMUReset = now;
}
} else {