The timestamp_us member of the sensor events has been renamed sensor_timestamp and now represents nanoseconds.

This commit is contained in:
Sam Lantinga
2022-12-04 08:31:19 -08:00
parent 73f4aeee6a
commit b8760a3ffe
26 changed files with 134 additions and 128 deletions

View File

@@ -225,8 +225,8 @@ typedef struct
Uint8 led_green;
Uint8 led_blue;
EDS5LEDResetState led_reset_state;
Uint32 last_timestamp;
Uint64 timestamp;
Uint64 sensor_ticks;
Uint32 last_tick;
union
{
PS5SimpleStatePacket_t simple;
@@ -975,7 +975,6 @@ static int HIDAPI_DriverPS5_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device,
HIDAPI_DriverPS5_LoadCalibrationData(device);
}
ctx->report_sensors = enabled;
ctx->timestamp = 0;
return 0;
}
@@ -1167,40 +1166,35 @@ static void HIDAPI_DriverPS5_HandleStatePacketCommon(SDL_Joystick *joystick, SDL
SDL_PrivateJoystickAxis(timestamp, joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
if (ctx->report_sensors) {
Uint32 timestamp;
Uint64 timestamp_us;
Uint32 tick;
Uint32 delta;
Uint64 sensor_timestamp;
float data[3];
timestamp = LOAD32(packet->rgucSensorTimestamp[0],
packet->rgucSensorTimestamp[1],
packet->rgucSensorTimestamp[2],
packet->rgucSensorTimestamp[3]);
if (ctx->timestamp) {
Uint32 delta;
if (ctx->last_timestamp > timestamp) {
delta = (SDL_MAX_UINT32 - ctx->last_timestamp + timestamp + 1);
} else {
delta = (timestamp - ctx->last_timestamp);
}
ctx->timestamp += delta;
tick = LOAD32(packet->rgucSensorTimestamp[0],
packet->rgucSensorTimestamp[1],
packet->rgucSensorTimestamp[2],
packet->rgucSensorTimestamp[3]);
if (ctx->last_tick < tick) {
delta = (tick - ctx->last_tick);
} else {
ctx->timestamp = timestamp;
delta = (SDL_MAX_UINT32 - ctx->last_tick + tick + 1);
}
ctx->last_timestamp = timestamp;
ctx->sensor_ticks += delta;
ctx->last_tick = tick;
/* Sensor timestamp is in 0.33us units */
timestamp_us = ctx->timestamp / 3;
sensor_timestamp = (ctx->sensor_ticks * SDL_NS_PER_US) / 3;
data[0] = HIDAPI_DriverPS5_ApplyCalibrationData(ctx, 0, LOAD16(packet->rgucGyroX[0], packet->rgucGyroX[1]));
data[1] = HIDAPI_DriverPS5_ApplyCalibrationData(ctx, 1, LOAD16(packet->rgucGyroY[0], packet->rgucGyroY[1]));
data[2] = HIDAPI_DriverPS5_ApplyCalibrationData(ctx, 2, LOAD16(packet->rgucGyroZ[0], packet->rgucGyroZ[1]));
SDL_PrivateJoystickSensor(timestamp, joystick, SDL_SENSOR_GYRO, timestamp_us, data, 3);
SDL_PrivateJoystickSensor(timestamp, joystick, SDL_SENSOR_GYRO, sensor_timestamp, data, 3);
data[0] = HIDAPI_DriverPS5_ApplyCalibrationData(ctx, 3, LOAD16(packet->rgucAccelX[0], packet->rgucAccelX[1]));
data[1] = HIDAPI_DriverPS5_ApplyCalibrationData(ctx, 4, LOAD16(packet->rgucAccelY[0], packet->rgucAccelY[1]));
data[2] = HIDAPI_DriverPS5_ApplyCalibrationData(ctx, 5, LOAD16(packet->rgucAccelZ[0], packet->rgucAccelZ[1]));
SDL_PrivateJoystickSensor(timestamp, joystick, SDL_SENSOR_ACCEL, timestamp_us, data, 3);
SDL_PrivateJoystickSensor(timestamp, joystick, SDL_SENSOR_ACCEL, sensor_timestamp, data, 3);
}
}