N3DS: Use SDL_Sensor instead of Joystick sensors.

This commit is contained in:
Pierre Wendling 2022-09-20 22:45:24 -04:00 committed by Sam Lantinga
parent 27557b62b6
commit 392f3882d0
6 changed files with 234 additions and 44 deletions

View file

@ -41,12 +41,15 @@ static SDL_SensorDriver *SDL_sensor_drivers[] = {
#ifdef SDL_SENSOR_WINDOWS
&SDL_WINDOWS_SensorDriver,
#endif
#ifdef SDL_SENSOR_VITA
&SDL_VITA_SensorDriver,
#endif
#ifdef SDL_SENSOR_N3DS
&SDL_N3DS_SensorDriver,
#endif
#if defined(SDL_SENSOR_DUMMY) || defined(SDL_SENSOR_DISABLED)
&SDL_DUMMY_SensorDriver
#endif
#if defined(SDL_SENSOR_VITA)
&SDL_VITA_SensorDriver
#endif
};
static SDL_Sensor *SDL_sensors = NULL;
static SDL_bool SDL_updating_sensor = SDL_FALSE;

View file

@ -102,6 +102,7 @@ extern SDL_SensorDriver SDL_COREMOTION_SensorDriver;
extern SDL_SensorDriver SDL_WINDOWS_SensorDriver;
extern SDL_SensorDriver SDL_DUMMY_SensorDriver;
extern SDL_SensorDriver SDL_VITA_SensorDriver;
extern SDL_SensorDriver SDL_N3DS_SensorDriver;
#endif /* SDL_syssensor_h_ */

View file

@ -0,0 +1,218 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#ifdef SDL_SENSOR_N3DS
#include <3ds.h>
#include "../SDL_syssensor.h"
/* 1 accelerometer and 1 gyroscope */
#define N3DS_SENSOR_COUNT 2
typedef struct
{
SDL_SensorType type;
SDL_SensorID instance_id;
} SDL_N3DSSensor;
static SDL_N3DSSensor N3DS_sensors[N3DS_SENSOR_COUNT];
SDL_FORCE_INLINE int InitN3DSServices(void);
SDL_FORCE_INLINE void UpdateN3DSAccelerometer(SDL_Sensor *sensor);
SDL_FORCE_INLINE void UpdateN3DSGyroscope(SDL_Sensor *sensor);
SDL_FORCE_INLINE SDL_bool
IsDeviceIndexValid(int device_index)
{
return device_index >= 0 && device_index < N3DS_SENSOR_COUNT;
}
static int
N3DS_SensorInit(void)
{
if (InitN3DSServices() < 0) {
return SDL_SetError("Failed to initialise N3DS services");
}
N3DS_sensors[0].type = SDL_SENSOR_ACCEL;
N3DS_sensors[0].instance_id = SDL_GetNextSensorInstanceID();
N3DS_sensors[1].type = SDL_SENSOR_GYRO;
N3DS_sensors[1].instance_id = SDL_GetNextSensorInstanceID();
return 0;
}
SDL_FORCE_INLINE int
InitN3DSServices(void)
{
if (R_FAILED(hidInit())) {
return -1;
}
if (R_FAILED(HIDUSER_EnableAccelerometer())) {
return -1;
}
if (R_FAILED(HIDUSER_EnableGyroscope())) {
return -1;
}
return 0;
}
static int
N3DS_SensorGetCount(void)
{
return N3DS_SENSOR_COUNT;
}
static void
N3DS_SensorDetect(void)
{
}
static const char *
N3DS_SensorGetDeviceName(int device_index)
{
if (IsDeviceIndexValid(device_index)) {
switch (N3DS_sensors[device_index].type) {
case SDL_SENSOR_ACCEL:
return "Accelerometer";
case SDL_SENSOR_GYRO:
return "Gyroscope";
default:
return "Unknown";
}
}
return NULL;
}
static SDL_SensorType
N3DS_SensorGetDeviceType(int device_index)
{
if (IsDeviceIndexValid(device_index)) {
return N3DS_sensors[device_index].type;
}
return SDL_SENSOR_INVALID;
}
static int
N3DS_SensorGetDeviceNonPortableType(int device_index)
{
return (int) N3DS_SensorGetDeviceType(device_index);
}
static SDL_SensorID
N3DS_SensorGetDeviceInstanceID(int device_index)
{
if (IsDeviceIndexValid(device_index)) {
return N3DS_sensors[device_index].instance_id;
}
return -1;
}
static int
N3DS_SensorOpen(SDL_Sensor *sensor, int device_index)
{
return 0;
}
static void
N3DS_SensorUpdate(SDL_Sensor *sensor)
{
switch (sensor->type) {
case SDL_SENSOR_ACCEL:
UpdateN3DSAccelerometer(sensor);
break;
case SDL_SENSOR_GYRO:
UpdateN3DSGyroscope(sensor);
break;
default:
break;
}
}
SDL_FORCE_INLINE void
UpdateN3DSAccelerometer(SDL_Sensor *sensor)
{
static accelVector previous_state = { 0, 0, 0 };
accelVector current_state;
float data[3];
hidAccelRead(&current_state);
if (SDL_memcmp(&previous_state, &current_state, sizeof(accelVector)) != 0) {
SDL_memcpy(&previous_state, &current_state, sizeof(accelVector));
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, data, sizeof data);
}
}
SDL_FORCE_INLINE void
UpdateN3DSGyroscope(SDL_Sensor *sensor)
{
static angularRate previous_state = { 0, 0, 0 };
angularRate current_state;
float data[3];
hidGyroRead(&current_state);
if (SDL_memcmp(&previous_state, &current_state, sizeof(angularRate)) != 0) {
SDL_memcpy(&previous_state, &current_state, sizeof(angularRate));
data[0] = (float) current_state.x;
data[1] = (float) current_state.y;
data[2] = (float) current_state.z;
SDL_PrivateSensorUpdate(sensor, data, sizeof data);
}
}
static void
N3DS_SensorClose(SDL_Sensor *sensor)
{
}
static void
N3DS_SensorQuit(void)
{
HIDUSER_DisableGyroscope();
HIDUSER_DisableAccelerometer();
hidExit();
}
SDL_SensorDriver SDL_N3DS_SensorDriver = {
N3DS_SensorInit,
N3DS_SensorGetCount,
N3DS_SensorDetect,
N3DS_SensorGetDeviceName,
N3DS_SensorGetDeviceType,
N3DS_SensorGetDeviceNonPortableType,
N3DS_SensorGetDeviceInstanceID,
N3DS_SensorOpen,
N3DS_SensorUpdate,
N3DS_SensorClose,
N3DS_SensorQuit,
};
#endif /* SDL_SENSOR_N3DS */
/* vi: set ts=4 sw=4 expandtab: */