Added microsecond timestamp to sensor values for PS4 and PS5 controllers using the HIDAPI driver
This commit is contained in:
parent
d71df6448b
commit
2c518747b9
26 changed files with 204 additions and 63 deletions
|
@ -307,7 +307,7 @@ SDL_SensorFromInstanceID(SDL_SensorID instance_id)
|
|||
* Checks to make sure the sensor is valid.
|
||||
*/
|
||||
static int
|
||||
SDL_PrivateSensorValid(SDL_Sensor * sensor)
|
||||
SDL_PrivateSensorValid(SDL_Sensor *sensor)
|
||||
{
|
||||
int valid;
|
||||
|
||||
|
@ -325,7 +325,7 @@ SDL_PrivateSensorValid(SDL_Sensor * sensor)
|
|||
* Get the friendly name of this sensor
|
||||
*/
|
||||
const char *
|
||||
SDL_SensorGetName(SDL_Sensor * sensor)
|
||||
SDL_SensorGetName(SDL_Sensor *sensor)
|
||||
{
|
||||
if (!SDL_PrivateSensorValid(sensor)) {
|
||||
return NULL;
|
||||
|
@ -338,7 +338,7 @@ SDL_SensorGetName(SDL_Sensor * sensor)
|
|||
* Get the type of this sensor
|
||||
*/
|
||||
SDL_SensorType
|
||||
SDL_SensorGetType(SDL_Sensor * sensor)
|
||||
SDL_SensorGetType(SDL_Sensor *sensor)
|
||||
{
|
||||
if (!SDL_PrivateSensorValid(sensor)) {
|
||||
return SDL_SENSOR_INVALID;
|
||||
|
@ -351,7 +351,7 @@ SDL_SensorGetType(SDL_Sensor * sensor)
|
|||
* Get the platform dependent type of this sensor
|
||||
*/
|
||||
int
|
||||
SDL_SensorGetNonPortableType(SDL_Sensor * sensor)
|
||||
SDL_SensorGetNonPortableType(SDL_Sensor *sensor)
|
||||
{
|
||||
if (!SDL_PrivateSensorValid(sensor)) {
|
||||
return -1;
|
||||
|
@ -364,7 +364,7 @@ SDL_SensorGetNonPortableType(SDL_Sensor * sensor)
|
|||
* Get the instance id for this opened sensor
|
||||
*/
|
||||
SDL_SensorID
|
||||
SDL_SensorGetInstanceID(SDL_Sensor * sensor)
|
||||
SDL_SensorGetInstanceID(SDL_Sensor *sensor)
|
||||
{
|
||||
if (!SDL_PrivateSensorValid(sensor)) {
|
||||
return -1;
|
||||
|
@ -377,7 +377,16 @@ SDL_SensorGetInstanceID(SDL_Sensor * sensor)
|
|||
* Get the current state of this sensor
|
||||
*/
|
||||
int
|
||||
SDL_SensorGetData(SDL_Sensor * sensor, float *data, int num_values)
|
||||
SDL_SensorGetData(SDL_Sensor *sensor, float *data, int num_values)
|
||||
{
|
||||
return SDL_SensorGetDataWithTimestamp(sensor, NULL, data, num_values);
|
||||
}
|
||||
|
||||
/*
|
||||
* Get the current state of this sensor
|
||||
*/
|
||||
int
|
||||
SDL_SensorGetDataWithTimestamp(SDL_Sensor *sensor, Uint64 *timestamp, float *data, int num_values)
|
||||
{
|
||||
if (!SDL_PrivateSensorValid(sensor)) {
|
||||
return -1;
|
||||
|
@ -385,6 +394,9 @@ SDL_SensorGetData(SDL_Sensor * sensor, float *data, int num_values)
|
|||
|
||||
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;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -392,7 +404,7 @@ SDL_SensorGetData(SDL_Sensor * sensor, float *data, int num_values)
|
|||
* Close a sensor previously opened with SDL_SensorOpen()
|
||||
*/
|
||||
void
|
||||
SDL_SensorClose(SDL_Sensor * sensor)
|
||||
SDL_SensorClose(SDL_Sensor *sensor)
|
||||
{
|
||||
SDL_Sensor *sensorlist;
|
||||
SDL_Sensor *sensorlistprev;
|
||||
|
@ -478,7 +490,7 @@ SDL_SensorQuit(void)
|
|||
/* These are global for SDL_syssensor.c and SDL_events.c */
|
||||
|
||||
int
|
||||
SDL_PrivateSensorUpdate(SDL_Sensor *sensor, float *data, int num_values)
|
||||
SDL_PrivateSensorUpdate(SDL_Sensor *sensor, Uint64 timestamp_us, float *data, int num_values)
|
||||
{
|
||||
int posted;
|
||||
|
||||
|
@ -487,6 +499,7 @@ SDL_PrivateSensorUpdate(SDL_Sensor *sensor, float *data, int num_values)
|
|||
/* 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;
|
||||
|
||||
/* Post the event, if desired */
|
||||
posted = 0;
|
||||
|
@ -498,6 +511,7 @@ SDL_PrivateSensorUpdate(SDL_Sensor *sensor, float *data, int num_values)
|
|||
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;
|
||||
posted = SDL_PushEvent(&event) == 1;
|
||||
}
|
||||
#endif /* !SDL_EVENTS_DISABLED */
|
||||
|
|
|
@ -37,7 +37,7 @@ extern int SDL_SensorInit(void);
|
|||
extern void SDL_SensorQuit(void);
|
||||
|
||||
/* Internal event queueing functions */
|
||||
extern int SDL_PrivateSensorUpdate(SDL_Sensor *sensor, float *data, int num_values);
|
||||
extern int SDL_PrivateSensorUpdate(SDL_Sensor *sensor, Uint64 timestamp_us, float *data, int num_values);
|
||||
|
||||
#endif /* SDL_sensor_c_h_ */
|
||||
|
||||
|
|
|
@ -37,6 +37,7 @@ struct _SDL_Sensor
|
|||
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 */
|
||||
|
||||
struct _SDL_SensorDriver *driver;
|
||||
|
|
|
@ -174,7 +174,7 @@ SDL_ANDROID_SensorUpdate(SDL_Sensor *sensor)
|
|||
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, event.data, SDL_arraysize(event.data));
|
||||
SDL_PrivateSensorUpdate(sensor, 0, event.data, SDL_arraysize(event.data));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -162,7 +162,7 @@ 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, data, SDL_arraysize(data));
|
||||
SDL_PrivateSensorUpdate(sensor, 0, data, SDL_arraysize(data));
|
||||
SDL_memcpy(sensor->hwdata->data, data, sizeof(data));
|
||||
}
|
||||
}
|
||||
|
@ -178,7 +178,7 @@ 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, data, SDL_arraysize(data));
|
||||
SDL_PrivateSensorUpdate(sensor, 0, data, SDL_arraysize(data));
|
||||
SDL_memcpy(sensor->hwdata->data, data, sizeof(data));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -154,7 +154,24 @@ SDL_VITA_SensorUpdate(SDL_Sensor *sensor)
|
|||
{
|
||||
if (sensor->hwdata->counter < motionState[i].counter)
|
||||
{
|
||||
unsigned int timestamp = motionState[i].timestamp;
|
||||
|
||||
sensor->hwdata->counter = motionState[i].counter;
|
||||
|
||||
if (sensor->hwdata->timestamp_us) {
|
||||
unsigned int delta;
|
||||
if (sensor->hwdata->last_timestamp > timestamp) {
|
||||
SDL_COMPILE_TIME_ASSERT(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;
|
||||
} else {
|
||||
sensor->hwdata->timestamp_us = timestamp;
|
||||
}
|
||||
sensor->hwdata->last_timestamp = timestamp;
|
||||
|
||||
switch (sensor->type)
|
||||
{
|
||||
case SDL_SENSOR_ACCEL:
|
||||
|
@ -163,10 +180,7 @@ 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;
|
||||
if (SDL_memcmp(data, sensor->hwdata->data, sizeof(data)) != 0) {
|
||||
SDL_PrivateSensorUpdate(sensor, data, SDL_arraysize(data));
|
||||
SDL_memcpy(sensor->hwdata->data, data, sizeof(data));
|
||||
}
|
||||
SDL_PrivateSensorUpdate(sensor, sensor->hwdata->timestamp_us, data, SDL_arraysize(data));
|
||||
}
|
||||
break;
|
||||
case SDL_SENSOR_GYRO:
|
||||
|
@ -175,10 +189,7 @@ SDL_VITA_SensorUpdate(SDL_Sensor *sensor)
|
|||
data[0] = motionState[i].gyro.x;
|
||||
data[1] = motionState[i].gyro.y;
|
||||
data[2] = motionState[i].gyro.z;
|
||||
if (SDL_memcmp(data, sensor->hwdata->data, sizeof(data)) != 0) {
|
||||
SDL_PrivateSensorUpdate(sensor, data, SDL_arraysize(data));
|
||||
SDL_memcpy(sensor->hwdata->data, data, sizeof(data));
|
||||
}
|
||||
SDL_PrivateSensorUpdate(sensor, sensor->hwdata->timestamp_us, data, SDL_arraysize(data));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
|
|
|
@ -23,8 +23,9 @@
|
|||
/* The private structure used to keep track of a sensor */
|
||||
struct sensor_hwdata
|
||||
{
|
||||
float data[3];
|
||||
Uint32 counter;
|
||||
unsigned int last_timestamp;
|
||||
Uint64 timestamp_us;
|
||||
};
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
|
|
|
@ -171,7 +171,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, values, 3);
|
||||
SDL_PrivateSensorUpdate(SDL_sensors[i].sensor_opened, 0, values, 3);
|
||||
}
|
||||
break;
|
||||
case SDL_SENSOR_GYRO:
|
||||
|
@ -186,7 +186,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, values, 3);
|
||||
SDL_PrivateSensorUpdate(SDL_sensors[i].sensor_opened, 0, values, 3);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue