Fixed spamming the controller with reset IMU commands when they are failing

This commit is contained in:
Sam Lantinga 2022-08-09 21:30:11 -07:00
parent eab27b9049
commit 8e782876bb

View file

@ -270,6 +270,7 @@ typedef struct {
SDL_bool m_bReportSensors; SDL_bool m_bReportSensors;
SDL_bool m_bHasSensorData; SDL_bool m_bHasSensorData;
Uint32 m_unLastInput; Uint32 m_unLastInput;
Uint32 m_unLastIMUReset;
SwitchInputOnlyControllerStatePacket_t m_lastInputOnlyState; SwitchInputOnlyControllerStatePacket_t m_lastInputOnlyState;
SwitchSimpleStatePacket_t m_lastSimpleState; SwitchSimpleStatePacket_t m_lastSimpleState;
@ -1369,7 +1370,7 @@ HIDAPI_DriverSwitch_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joysti
/* Set up for input */ /* Set up for input */
ctx->m_bSyncWrite = SDL_FALSE; ctx->m_bSyncWrite = SDL_FALSE;
ctx->m_unLastInput = SDL_GetTicks(); ctx->m_unLastIMUReset = ctx->m_unLastInput = SDL_GetTicks();
return SDL_TRUE; return SDL_TRUE;
@ -2005,6 +2006,10 @@ static void HandleFullControllerState(SDL_Joystick *joystick, SDL_DriverSwitch_C
} else if (ctx->m_bHasSensorData) { } else if (ctx->m_bHasSensorData) {
/* Uh oh, someone turned off the IMU? */ /* Uh oh, someone turned off the IMU? */
const Uint32 IMU_RESET_DELAY_MS = 3000;
Uint32 now = SDL_GetTicks();
if (SDL_TICKS_PASSED(now, ctx->m_unLastIMUReset + IMU_RESET_DELAY_MS)) {
SDL_HIDAPI_Device *device = ctx->device; SDL_HIDAPI_Device *device = ctx->device;
if (device->updating) { if (device->updating) {
@ -2016,6 +2021,8 @@ static void HandleFullControllerState(SDL_Joystick *joystick, SDL_DriverSwitch_C
if (device->updating) { if (device->updating) {
SDL_LockMutex(device->dev_lock); SDL_LockMutex(device->dev_lock);
} }
ctx->m_unLastIMUReset = now;
}
} else { } else {
/* We have never gotten IMU data, probably not supported on this device */ /* We have never gotten IMU data, probably not supported on this device */