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

@ -384,7 +384,7 @@ int SDL_SensorGetDataWithTimestamp(SDL_Sensor *sensor, Uint64 *timestamp, float
num_values = SDL_min(num_values, SDL_arraysize(sensor->data));
SDL_memcpy(data, sensor->data, num_values * sizeof(*data));
if (timestamp) {
*timestamp = sensor->timestamp_us;
*timestamp = sensor->sensor_timestamp;
}
return 0;
}
@ -475,7 +475,7 @@ void SDL_SensorQuit(void)
/* These are global for SDL_syssensor.c and SDL_events.c */
int SDL_PrivateSensorUpdate(SDL_Sensor *sensor, Uint64 timestamp_us, float *data, int num_values)
int SDL_PrivateSensorUpdate(Uint64 timestamp, SDL_Sensor *sensor, Uint64 sensor_timestamp, float *data, int num_values)
{
int posted;
@ -484,7 +484,7 @@ int SDL_PrivateSensorUpdate(SDL_Sensor *sensor, Uint64 timestamp_us, float *data
/* Update internal sensor state */
num_values = SDL_min(num_values, SDL_arraysize(sensor->data));
SDL_memcpy(sensor->data, data, num_values * sizeof(*data));
sensor->timestamp_us = timestamp_us;
sensor->sensor_timestamp = sensor_timestamp;
/* Post the event, if desired */
posted = 0;
@ -492,12 +492,12 @@ int SDL_PrivateSensorUpdate(SDL_Sensor *sensor, Uint64 timestamp_us, float *data
if (SDL_GetEventState(SDL_SENSORUPDATE) == SDL_ENABLE) {
SDL_Event event;
event.type = SDL_SENSORUPDATE;
event.common.timestamp = 0;
event.common.timestamp = timestamp;
event.sensor.which = sensor->instance_id;
num_values = SDL_min(num_values, SDL_arraysize(event.sensor.data));
SDL_memset(event.sensor.data, 0, sizeof(event.sensor.data));
SDL_memcpy(event.sensor.data, data, num_values * sizeof(*data));
event.sensor.timestamp_us = timestamp_us;
event.sensor.sensor_timestamp = sensor_timestamp;
posted = SDL_PushEvent(&event) == 1;
}
#endif /* !SDL_EVENTS_DISABLED */

View file

@ -35,7 +35,7 @@ extern int SDL_SensorInit(void);
extern void SDL_SensorQuit(void);
/* Internal event queueing functions */
extern int SDL_PrivateSensorUpdate(SDL_Sensor *sensor, Uint64 timestamp_us, float *data, int num_values);
extern int SDL_PrivateSensorUpdate(Uint64 timestamp, SDL_Sensor *sensor, Uint64 sensor_timestamp, float *data, int num_values);
#endif /* SDL_sensor_c_h_ */

View file

@ -30,13 +30,13 @@
/* The SDL sensor structure */
struct _SDL_Sensor
{
SDL_SensorID instance_id; /* Device instance, monotonically increasing from 0 */
char *name; /* Sensor name - system dependent */
SDL_SensorType type; /* Type of the sensor */
int non_portable_type; /* Platform dependent type of the sensor */
SDL_SensorID instance_id; /* Device instance, monotonically increasing from 0 */
char *name; /* Sensor name - system dependent */
SDL_SensorType type; /* Type of the sensor */
int non_portable_type; /* Platform dependent type of the sensor */
Uint64 timestamp_us; /* The timestamp of the last sensor update */
float data[16]; /* The current state of the sensor */
Uint64 sensor_timestamp; /* The timestamp of the last sensor update */
float data[16]; /* The current state of the sensor */
struct _SDL_SensorDriver *driver;

View file

@ -156,11 +156,12 @@ static void SDL_ANDROID_SensorUpdate(SDL_Sensor *sensor)
int events;
ASensorEvent event;
struct android_poll_source *source;
Uint64 timestamp = SDL_GetTicks();
if (ALooper_pollAll(0, NULL, &events, (void **)&source) == LOOPER_ID_USER) {
SDL_zero(event);
while (ASensorEventQueue_getEvents(sensor->hwdata->eventqueue, &event, 1) > 0) {
SDL_PrivateSensorUpdate(sensor, 0, event.data, SDL_arraysize(event.data));
SDL_PrivateSensorUpdate(timestamp, sensor, timestamp, event.data, SDL_arraysize(event.data));
}
}
}

View file

@ -137,6 +137,8 @@ static int SDL_COREMOTION_SensorOpen(SDL_Sensor *sensor, int device_index)
static void SDL_COREMOTION_SensorUpdate(SDL_Sensor *sensor)
{
Uint64 timestamp = SDL_GetTicks();
switch (sensor->type) {
case SDL_SENSOR_ACCEL:
{
@ -148,7 +150,7 @@ static void SDL_COREMOTION_SensorUpdate(SDL_Sensor *sensor)
data[1] = -acceleration.y * SDL_STANDARD_GRAVITY;
data[2] = -acceleration.z * SDL_STANDARD_GRAVITY;
if (SDL_memcmp(data, sensor->hwdata->data, sizeof(data)) != 0) {
SDL_PrivateSensorUpdate(sensor, 0, data, SDL_arraysize(data));
SDL_PrivateSensorUpdate(timestamp, sensor, timestamp, data, SDL_arraysize(data));
SDL_memcpy(sensor->hwdata->data, data, sizeof(data));
}
}
@ -163,7 +165,7 @@ static void SDL_COREMOTION_SensorUpdate(SDL_Sensor *sensor)
data[1] = rotationRate.y;
data[2] = rotationRate.z;
if (SDL_memcmp(data, sensor->hwdata->data, sizeof(data)) != 0) {
SDL_PrivateSensorUpdate(sensor, 0, data, SDL_arraysize(data));
SDL_PrivateSensorUpdate(timestamp, sensor, timestamp, data, SDL_arraysize(data));
SDL_memcpy(sensor->hwdata->data, data, sizeof(data));
}
}

View file

@ -149,6 +149,7 @@ UpdateN3DSAccelerometer(SDL_Sensor *sensor)
static accelVector previous_state = { 0, 0, 0 };
accelVector current_state;
float data[3];
Uint64 timestamp = SDL_GetTicksNS();
hidAccelRead(&current_state);
if (SDL_memcmp(&previous_state, &current_state, sizeof(accelVector)) != 0) {
@ -156,7 +157,7 @@ UpdateN3DSAccelerometer(SDL_Sensor *sensor)
data[0] = (float)current_state.x * SDL_STANDARD_GRAVITY;
data[1] = (float)current_state.y * SDL_STANDARD_GRAVITY;
data[2] = (float)current_state.z * SDL_STANDARD_GRAVITY;
SDL_PrivateSensorUpdate(sensor, 0, data, sizeof data);
SDL_PrivateSensorUpdate(timestamp, sensor, timestamp, data, sizeof data);
}
}
@ -166,6 +167,7 @@ UpdateN3DSGyroscope(SDL_Sensor *sensor)
static angularRate previous_state = { 0, 0, 0 };
angularRate current_state;
float data[3];
Uint64 timestamp = SDL_GetTicksNS();
hidGyroRead(&current_state);
if (SDL_memcmp(&previous_state, &current_state, sizeof(angularRate)) != 0) {
@ -173,7 +175,7 @@ UpdateN3DSGyroscope(SDL_Sensor *sensor)
data[0] = (float)current_state.x;
data[1] = (float)current_state.y;
data[2] = (float)current_state.z;
SDL_PrivateSensorUpdate(sensor, 0, data, sizeof data);
SDL_PrivateSensorUpdate(timestamp, sensor, timestamp, data, sizeof data);
}
}

View file

@ -130,8 +130,9 @@ static void SDL_VITA_SensorUpdate(SDL_Sensor *sensor)
{
int err = 0;
SceMotionSensorState motionState[SCE_MOTION_MAX_NUM_STATES];
SDL_memset(motionState, 0, sizeof(motionState));
Uint64 timestamp = SDL_GetTicksNS();
SDL_zero(motionState);
err = sceMotionGetSensorState(motionState, SCE_MOTION_MAX_NUM_STATES);
if (err != 0) {
return;
@ -139,23 +140,19 @@ static void SDL_VITA_SensorUpdate(SDL_Sensor *sensor)
for (int i = 0; i < SCE_MOTION_MAX_NUM_STATES; i++) {
if (sensor->hwdata->counter < motionState[i].counter) {
unsigned int timestamp = motionState[i].timestamp;
unsigned int tick = motionState[i].timestamp;
unsigned int delta;
sensor->hwdata->counter = motionState[i].counter;
if (sensor->hwdata->timestamp_us) {
unsigned int delta;
if (sensor->hwdata->last_timestamp > timestamp) {
SDL_COMPILE_TIME_ASSERT(timestamp, sizeof(timestamp) == sizeof(Uint32));
delta = (SDL_MAX_UINT32 - sensor->hwdata->last_timestamp + timestamp + 1);
} else {
delta = (timestamp - sensor->hwdata->last_timestamp);
}
sensor->hwdata->timestamp_us += delta;
if (sensor->hwdata->last_tick > tick) {
SDL_COMPILE_TIME_ASSERT(tick, sizeof(tick) == sizeof(Uint32));
delta = (SDL_MAX_UINT32 - sensor->hwdata->last_tick + tick + 1);
} else {
sensor->hwdata->timestamp_us = timestamp;
delta = (tick - sensor->hwdata->last_tick);
}
sensor->hwdata->last_timestamp = timestamp;
sensor->hwdata->sensor_timestamp += SDL_US_TO_NS(delta);
sensor->hwdata->last_tick = tick;
switch (sensor->type) {
case SDL_SENSOR_ACCEL:
@ -164,7 +161,7 @@ static void SDL_VITA_SensorUpdate(SDL_Sensor *sensor)
data[0] = motionState[i].accelerometer.x * SDL_STANDARD_GRAVITY;
data[1] = motionState[i].accelerometer.y * SDL_STANDARD_GRAVITY;
data[2] = motionState[i].accelerometer.z * SDL_STANDARD_GRAVITY;
SDL_PrivateSensorUpdate(sensor, sensor->hwdata->timestamp_us, data, SDL_arraysize(data));
SDL_PrivateSensorUpdate(timestamp, sensor, sensor->hwdata->sensor_timestamp, data, SDL_arraysize(data));
} break;
case SDL_SENSOR_GYRO:
{
@ -172,7 +169,7 @@ static void SDL_VITA_SensorUpdate(SDL_Sensor *sensor)
data[0] = motionState[i].gyro.x;
data[1] = motionState[i].gyro.y;
data[2] = motionState[i].gyro.z;
SDL_PrivateSensorUpdate(sensor, sensor->hwdata->timestamp_us, data, SDL_arraysize(data));
SDL_PrivateSensorUpdate(timestamp, sensor, sensor->hwdata->sensor_timestamp, data, SDL_arraysize(data));
} break;
default:
break;

View file

@ -24,8 +24,8 @@
struct sensor_hwdata
{
Uint32 counter;
unsigned int last_timestamp;
Uint64 timestamp_us;
unsigned int last_tick;
Uint64 sensor_timestamp;
};
/* vi: set ts=4 sw=4 expandtab: */

View file

@ -143,6 +143,7 @@ static HRESULT STDMETHODCALLTYPE ISensorEventsVtbl_OnStateChanged(ISensorEvents
static HRESULT STDMETHODCALLTYPE ISensorEventsVtbl_OnDataUpdated(ISensorEvents *This, ISensor *pSensor, ISensorDataReport *pNewData)
{
int i;
Uint64 timestamp = SDL_GetTicks();
SDL_LockSensors();
for (i = 0; i < SDL_num_sensors; ++i) {
@ -150,10 +151,23 @@ static HRESULT STDMETHODCALLTYPE ISensorEventsVtbl_OnDataUpdated(ISensorEvents *
if (SDL_sensors[i].sensor_opened) {
HRESULT hrX, hrY, hrZ;
PROPVARIANT valueX, valueY, valueZ;
SYSTEMTIME sensor_systemtime;
FILETIME sensor_filetime;
Uint64 sensor_timestamp;
#ifdef DEBUG_SENSORS
SDL_Log("Sensor %s data updated\n", SDL_sensors[i].name);
#endif
if (SUCCEEDED(ISensorDataReport_GetTimestamp(pNewData, &sensor_systemtime)) &&
SystemTimeToFileTime(&sensor_systemtime, &sensor_filetime)) {
ULARGE_INTEGER sensor_time;
sensor_time.u.HighPart = sensor_filetime.dwHighDateTime;
sensor_time.u.LowPart = sensor_filetime.dwLowDateTime;
sensor_timestamp = sensor_time.QuadPart * 100;
} else {
sensor_timestamp = timestamp;
}
switch (SDL_sensors[i].type) {
case SDL_SENSOR_ACCEL:
hrX = ISensorDataReport_GetSensorValue(pNewData, &SENSOR_DATA_TYPE_ACCELERATION_X_G, &valueX);
@ -166,7 +180,7 @@ static HRESULT STDMETHODCALLTYPE ISensorEventsVtbl_OnDataUpdated(ISensorEvents *
values[0] = (float)valueX.dblVal * SDL_STANDARD_GRAVITY;
values[1] = (float)valueY.dblVal * SDL_STANDARD_GRAVITY;
values[2] = (float)valueZ.dblVal * SDL_STANDARD_GRAVITY;
SDL_PrivateSensorUpdate(SDL_sensors[i].sensor_opened, 0, values, 3);
SDL_PrivateSensorUpdate(timestamp, SDL_sensors[i].sensor_opened, sensor_timestamp, values, 3);
}
break;
case SDL_SENSOR_GYRO:
@ -181,7 +195,7 @@ static HRESULT STDMETHODCALLTYPE ISensorEventsVtbl_OnDataUpdated(ISensorEvents *
values[0] = (float)valueX.dblVal * DEGREES_TO_RADIANS;
values[1] = (float)valueY.dblVal * DEGREES_TO_RADIANS;
values[2] = (float)valueZ.dblVal * DEGREES_TO_RADIANS;
SDL_PrivateSensorUpdate(SDL_sensors[i].sensor_opened, 0, values, 3);
SDL_PrivateSensorUpdate(timestamp, SDL_sensors[i].sensor_opened, sensor_timestamp, values, 3);
}
break;
default: