Add controller motion deduplication and fix gyro units

This commit is contained in:
Cameron Gutman 2023-07-02 19:22:00 -05:00
parent df283d80c6
commit 53a572a4a4
2 changed files with 18 additions and 4 deletions

View file

@ -364,15 +364,27 @@ void SdlInputHandler::handleControllerSensorEvent(SDL_ControllerSensorEvent* eve
switch (event->sensor) {
case SDL_SENSOR_ACCEL:
if (state->accelReportPeriodMs && SDL_TICKS_PASSED(event->timestamp, state->lastAccelEventTime + state->accelReportPeriodMs)) {
LiSendControllerMotionEvent((uint8_t)state->index, LI_MOTION_TYPE_ACCEL, event->data[0], event->data[1], event->data[2]);
if (state->accelReportPeriodMs &&
SDL_TICKS_PASSED(event->timestamp, state->lastAccelEventTime + state->accelReportPeriodMs) &&
memcmp(event->data, state->lastAccelEventData, sizeof(event->data)) != 0) {
memcpy(state->lastAccelEventData, event->data, sizeof(event->data));
state->lastAccelEventTime = event->timestamp;
LiSendControllerMotionEvent((uint8_t)state->index, LI_MOTION_TYPE_ACCEL, event->data[0], event->data[1], event->data[2]);
}
break;
case SDL_SENSOR_GYRO:
if (state->gyroReportPeriodMs && SDL_TICKS_PASSED(event->timestamp, state->lastGyroEventTime + state->gyroReportPeriodMs)) {
LiSendControllerMotionEvent((uint8_t)state->index, LI_MOTION_TYPE_GYRO, event->data[0], event->data[1], event->data[2]);
if (state->gyroReportPeriodMs &&
SDL_TICKS_PASSED(event->timestamp, state->lastGyroEventTime + state->gyroReportPeriodMs) &&
memcmp(event->data, state->lastGyroEventData, sizeof(event->data)) != 0) {
memcpy(state->lastGyroEventData, event->data, sizeof(event->data));
state->lastGyroEventTime = event->timestamp;
// Convert rad/s to deg/s
LiSendControllerMotionEvent((uint8_t)state->index, LI_MOTION_TYPE_GYRO,
event->data[0] * 57.2957795f,
event->data[1] * 57.2957795f,
event->data[2] * 57.2957795f);
}
break;
}

View file

@ -20,9 +20,11 @@ struct GamepadState {
uint32_t lastStartDownTime;
uint8_t gyroReportPeriodMs;
float lastGyroEventData[SDL_arraysize(SDL_ControllerSensorEvent::data)];
uint32_t lastGyroEventTime;
uint8_t accelReportPeriodMs;
float lastAccelEventData[SDL_arraysize(SDL_ControllerSensorEvent::data)];
uint32_t lastAccelEventTime;
int buttons;