Hash :
bf4f9aaa
Author :
Date :
2022-11-14T16:11:58
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 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(¤t_state);
if (SDL_memcmp(&previous_state, ¤t_state, sizeof(accelVector)) != 0) {
SDL_memcpy(&previous_state, ¤t_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, 0, 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(¤t_state);
if (SDL_memcmp(&previous_state, ¤t_state, sizeof(angularRate)) != 0) {
SDL_memcpy(&previous_state, ¤t_state, sizeof(angularRate));
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);
}
}
static void
N3DS_SensorClose(SDL_Sensor *sensor)
{
}
static void
N3DS_SensorQuit(void)
{
HIDUSER_DisableGyroscope();
HIDUSER_DisableAccelerometer();
hidExit();
}
SDL_SensorDriver SDL_N3DS_SensorDriver = {
.Init = N3DS_SensorInit,
.GetCount = N3DS_SensorGetCount,
.Detect = N3DS_SensorDetect,
.GetDeviceName = N3DS_SensorGetDeviceName,
.GetDeviceType = N3DS_SensorGetDeviceType,
.GetDeviceNonPortableType = N3DS_SensorGetDeviceNonPortableType,
.GetDeviceInstanceID = N3DS_SensorGetDeviceInstanceID,
.Open = N3DS_SensorOpen,
.Update = N3DS_SensorUpdate,
.Close = N3DS_SensorClose,
.Quit = N3DS_SensorQuit,
};
#endif /* SDL_SENSOR_N3DS */
/* vi: set ts=4 sw=4 expandtab: */