Atlas - SDL_n3dssensor.c
Home / ext / SDL / src / sensor / n3ds Lines: 1 | Size: 5494 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any damages 7 arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, 10 including commercial applications, and to alter it and redistribute it 11 freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; you must not 14 claim that you wrote the original software. If you use this software 15 in a product, an acknowledgment in the product documentation would be 16 appreciated but is not required. 17 2. Altered source versions must be plainly marked as such, and must not be 18 misrepresented as being the original software. 19 3. This notice may not be removed or altered from any source distribution. 20*/ 21 22#include "SDL_internal.h" 23 24#ifdef SDL_SENSOR_N3DS 25 26#include <3ds.h> 27 28#include "../SDL_syssensor.h" 29 30// 1 accelerometer and 1 gyroscope 31#define N3DS_SENSOR_COUNT 2 32 33typedef struct 34{ 35 SDL_SensorType type; 36 SDL_SensorID instance_id; 37} SDL_N3DSSensor; 38 39static SDL_N3DSSensor N3DS_sensors[N3DS_SENSOR_COUNT]; 40 41static bool InitN3DSServices(void); 42static void UpdateN3DSAccelerometer(SDL_Sensor *sensor); 43static void UpdateN3DSGyroscope(SDL_Sensor *sensor); 44 45static bool IsDeviceIndexValid(int device_index) 46{ 47 return device_index >= 0 && device_index < N3DS_SENSOR_COUNT; 48} 49 50static bool N3DS_SensorInit(void) 51{ 52 if (!InitN3DSServices()) { 53 return SDL_SetError("Failed to initialise N3DS services"); 54 } 55 56 N3DS_sensors[0].type = SDL_SENSOR_ACCEL; 57 N3DS_sensors[0].instance_id = SDL_GetNextObjectID(); 58 N3DS_sensors[1].type = SDL_SENSOR_GYRO; 59 N3DS_sensors[1].instance_id = SDL_GetNextObjectID(); 60 return true; 61} 62 63static bool InitN3DSServices(void) 64{ 65 if (R_FAILED(hidInit())) { 66 return false; 67 } 68 69 if (R_FAILED(HIDUSER_EnableAccelerometer())) { 70 return false; 71 } 72 73 if (R_FAILED(HIDUSER_EnableGyroscope())) { 74 return false; 75 } 76 return true; 77} 78 79static int N3DS_SensorGetCount(void) 80{ 81 return N3DS_SENSOR_COUNT; 82} 83 84static void N3DS_SensorDetect(void) 85{ 86} 87 88static const char *N3DS_SensorGetDeviceName(int device_index) 89{ 90 if (IsDeviceIndexValid(device_index)) { 91 switch (N3DS_sensors[device_index].type) { 92 case SDL_SENSOR_ACCEL: 93 return "Accelerometer"; 94 case SDL_SENSOR_GYRO: 95 return "Gyroscope"; 96 default: 97 return "Unknown"; 98 } 99 } 100 101 return NULL; 102} 103 104static SDL_SensorType N3DS_SensorGetDeviceType(int device_index) 105{ 106 if (IsDeviceIndexValid(device_index)) { 107 return N3DS_sensors[device_index].type; 108 } 109 return SDL_SENSOR_INVALID; 110} 111 112static int N3DS_SensorGetDeviceNonPortableType(int device_index) 113{ 114 return (int)N3DS_SensorGetDeviceType(device_index); 115} 116 117static SDL_SensorID N3DS_SensorGetDeviceInstanceID(int device_index) 118{ 119 if (IsDeviceIndexValid(device_index)) { 120 return N3DS_sensors[device_index].instance_id; 121 } 122 return -1; 123} 124 125static bool N3DS_SensorOpen(SDL_Sensor *sensor, int device_index) 126{ 127 return true; 128} 129 130static void N3DS_SensorUpdate(SDL_Sensor *sensor) 131{ 132 switch (sensor->type) { 133 case SDL_SENSOR_ACCEL: 134 UpdateN3DSAccelerometer(sensor); 135 break; 136 case SDL_SENSOR_GYRO: 137 UpdateN3DSGyroscope(sensor); 138 break; 139 default: 140 break; 141 } 142} 143 144static void UpdateN3DSAccelerometer(SDL_Sensor *sensor) 145{ 146 static accelVector previous_state = { 0, 0, 0 }; 147 accelVector current_state; 148 float data[3]; 149 Uint64 timestamp = SDL_GetTicksNS(); 150 151 hidAccelRead(¤t_state); 152 if (SDL_memcmp(&previous_state, ¤t_state, sizeof(accelVector)) != 0) { 153 SDL_memcpy(&previous_state, ¤t_state, sizeof(accelVector)); 154 data[0] = (float)current_state.x * SDL_STANDARD_GRAVITY; 155 data[1] = (float)current_state.y * SDL_STANDARD_GRAVITY; 156 data[2] = (float)current_state.z * SDL_STANDARD_GRAVITY; 157 SDL_SendSensorUpdate(timestamp, sensor, timestamp, data, sizeof(data)); 158 } 159} 160 161static void UpdateN3DSGyroscope(SDL_Sensor *sensor) 162{ 163 static angularRate previous_state = { 0, 0, 0 }; 164 angularRate current_state; 165 float data[3]; 166 Uint64 timestamp = SDL_GetTicksNS(); 167 168 hidGyroRead(¤t_state); 169 if (SDL_memcmp(&previous_state, ¤t_state, sizeof(angularRate)) != 0) { 170 SDL_memcpy(&previous_state, ¤t_state, sizeof(angularRate)); 171 data[0] = (float)current_state.x; 172 data[1] = (float)current_state.y; 173 data[2] = (float)current_state.z; 174 SDL_SendSensorUpdate(timestamp, sensor, timestamp, data, sizeof(data)); 175 } 176} 177 178static void N3DS_SensorClose(SDL_Sensor *sensor) 179{ 180} 181 182static void N3DS_SensorQuit(void) 183{ 184 HIDUSER_DisableGyroscope(); 185 HIDUSER_DisableAccelerometer(); 186 hidExit(); 187} 188 189SDL_SensorDriver SDL_N3DS_SensorDriver = { 190 .Init = N3DS_SensorInit, 191 .GetCount = N3DS_SensorGetCount, 192 .Detect = N3DS_SensorDetect, 193 .GetDeviceName = N3DS_SensorGetDeviceName, 194 .GetDeviceType = N3DS_SensorGetDeviceType, 195 .GetDeviceNonPortableType = N3DS_SensorGetDeviceNonPortableType, 196 .GetDeviceInstanceID = N3DS_SensorGetDeviceInstanceID, 197 .Open = N3DS_SensorOpen, 198 .Update = N3DS_SensorUpdate, 199 .Close = N3DS_SensorClose, 200 .Quit = N3DS_SensorQuit, 201}; 202 203#endif // SDL_SENSOR_N3DS 204[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.