Fix sensors

This commit is contained in:
Ivan Epifanov 2020-12-05 12:37:53 +03:00 committed by Sam Lantinga
parent e7edb06e7a
commit 6d85637786
2 changed files with 58 additions and 25 deletions

View File

@ -45,8 +45,8 @@ SDL_VITA_SensorInit(void)
sceMotionReset(); sceMotionReset();
sceMotionStartSampling(); sceMotionStartSampling();
// sceMotionMagnetometerOn(); // sceMotionMagnetometerOn();
sceMotionSetAngleThreshold(40); sceMotionSetAngleThreshold(0);
sceMotionSetGyroBiasCorrection(SCE_TRUE); sceMotionSetGyroBiasCorrection(SCE_FALSE);
SDL_sensors_count = 2; SDL_sensors_count = 2;
@ -122,39 +122,65 @@ SDL_VITA_SensorGetDeviceInstanceID(int device_index)
static int static int
SDL_VITA_SensorOpen(SDL_Sensor *sensor, int device_index) SDL_VITA_SensorOpen(SDL_Sensor *sensor, int device_index)
{ {
struct sensor_hwdata *hwdata;
hwdata = (struct sensor_hwdata *)SDL_calloc(1, sizeof(*hwdata));
if (hwdata == NULL) {
return SDL_OutOfMemory();
}
sensor->hwdata = hwdata;
return 0; return 0;
} }
static void static void
SDL_VITA_SensorUpdate(SDL_Sensor *sensor) SDL_VITA_SensorUpdate(SDL_Sensor *sensor)
{ {
SceMotionState motionState; SceMotionSensorState motionState[SCE_MOTION_MAX_NUM_STATES];
int err = SCE_OK; SDL_memset(motionState, 0, sizeof(motionState));
err = sceMotionGetState(&motionState);
if (err != SCE_OK) return;
switch (sensor->type) int err = SCE_OK;
err = sceMotionGetSensorState(motionState, SCE_MOTION_MAX_NUM_STATES);
if (err != SCE_OK)
{ {
case SDL_SENSOR_ACCEL: return;
}
for (int i = 0; i < SCE_MOTION_MAX_NUM_STATES; i++)
{
if (sensor->hwdata->counter < motionState[i].counter)
{ {
float data[3]; sensor->hwdata->counter = motionState[i].counter;
data[0] = motionState.acceleration.x * SDL_STANDARD_GRAVITY; switch (sensor->type)
data[1] = motionState.acceleration.y * SDL_STANDARD_GRAVITY; {
data[2] = motionState.acceleration.z * SDL_STANDARD_GRAVITY; case SDL_SENSOR_ACCEL:
SDL_PrivateSensorUpdate(sensor, data, SDL_arraysize(data)); {
float data[3];
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));
}
}
break;
case SDL_SENSOR_GYRO:
{
float data[3];
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));
}
}
break;
default:
break;
}
} }
break;
case SDL_SENSOR_GYRO:
{
float data[3];
data[0] = motionState.angularVelocity.x;
data[1] = motionState.angularVelocity.y;
data[2] = motionState.angularVelocity.z;
SDL_PrivateSensorUpdate(sensor, data, SDL_arraysize(data));
}
break;
default:
break;
} }
} }

View File

@ -20,4 +20,11 @@
*/ */
#include "SDL_config.h" #include "SDL_config.h"
/* The private structure used to keep track of a sensor */
struct sensor_hwdata
{
float data[3];
int counter;
};
/* vi: set ts=4 sw=4 expandtab: */ /* vi: set ts=4 sw=4 expandtab: */