Atlas - SDL_vitasensor.c
Home / ext / SDL / src / sensor / vita Lines: 1 | Size: 5832 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#include "SDL_internal.h" 22 23#ifdef SDL_SENSOR_VITA 24 25#include "SDL_vitasensor.h" 26#include "../SDL_syssensor.h" 27#include <psp2/motion.h> 28 29#ifndef SCE_MOTION_MAX_NUM_STATES 30#define SCE_MOTION_MAX_NUM_STATES 64 31#endif 32 33typedef struct 34{ 35 SDL_SensorType type; 36 SDL_SensorID instance_id; 37} SDL_VitaSensor; 38 39static SDL_VitaSensor *SDL_sensors; 40static int SDL_sensors_count; 41 42static bool SDL_VITA_SensorInit(void) 43{ 44 sceMotionReset(); 45 sceMotionStartSampling(); 46 // not sure if these are needed, we are reading unfiltered state 47 sceMotionSetAngleThreshold(0); 48 sceMotionSetDeadband(SCE_FALSE); 49 sceMotionSetTiltCorrection(SCE_FALSE); 50 51 SDL_sensors_count = 2; 52 53 SDL_sensors = (SDL_VitaSensor *)SDL_calloc(SDL_sensors_count, sizeof(*SDL_sensors)); 54 if (!SDL_sensors) { 55 return false; 56 } 57 58 SDL_sensors[0].type = SDL_SENSOR_ACCEL; 59 SDL_sensors[0].instance_id = SDL_GetNextObjectID(); 60 SDL_sensors[1].type = SDL_SENSOR_GYRO; 61 SDL_sensors[1].instance_id = SDL_GetNextObjectID(); 62 63 return true; 64} 65 66static int SDL_VITA_SensorGetCount(void) 67{ 68 return SDL_sensors_count; 69} 70 71static void SDL_VITA_SensorDetect(void) 72{ 73} 74 75static const char *SDL_VITA_SensorGetDeviceName(int device_index) 76{ 77 if (device_index < SDL_sensors_count) { 78 switch (SDL_sensors[device_index].type) { 79 case SDL_SENSOR_ACCEL: 80 return "Accelerometer"; 81 case SDL_SENSOR_GYRO: 82 return "Gyro"; 83 default: 84 return "Unknown"; 85 } 86 } 87 88 return NULL; 89} 90 91static SDL_SensorType SDL_VITA_SensorGetDeviceType(int device_index) 92{ 93 if (device_index < SDL_sensors_count) { 94 return SDL_sensors[device_index].type; 95 } 96 97 return SDL_SENSOR_INVALID; 98} 99 100static int SDL_VITA_SensorGetDeviceNonPortableType(int device_index) 101{ 102 if (device_index < SDL_sensors_count) { 103 return SDL_sensors[device_index].type; 104 } 105 return -1; 106} 107 108static SDL_SensorID SDL_VITA_SensorGetDeviceInstanceID(int device_index) 109{ 110 if (device_index < SDL_sensors_count) { 111 return SDL_sensors[device_index].instance_id; 112 } 113 return -1; 114} 115 116static bool SDL_VITA_SensorOpen(SDL_Sensor *sensor, int device_index) 117{ 118 struct sensor_hwdata *hwdata; 119 120 hwdata = (struct sensor_hwdata *)SDL_calloc(1, sizeof(*hwdata)); 121 if (!hwdata) { 122 return false; 123 } 124 sensor->hwdata = hwdata; 125 126 return true; 127} 128 129static void SDL_VITA_SensorUpdate(SDL_Sensor *sensor) 130{ 131 int err = 0; 132 SceMotionSensorState motionState[SCE_MOTION_MAX_NUM_STATES]; 133 Uint64 timestamp = SDL_GetTicksNS(); 134 135 SDL_zero(motionState); 136 err = sceMotionGetSensorState(motionState, SCE_MOTION_MAX_NUM_STATES); 137 if (err != 0) { 138 return; 139 } 140 141 for (int i = 0; i < SCE_MOTION_MAX_NUM_STATES; i++) { 142 if (sensor->hwdata->counter < motionState[i].counter) { 143 unsigned int tick = motionState[i].timestamp; 144 unsigned int delta; 145 146 sensor->hwdata->counter = motionState[i].counter; 147 148 if (sensor->hwdata->last_tick > tick) { 149 SDL_COMPILE_TIME_ASSERT(tick, sizeof(tick) == sizeof(Uint32)); 150 delta = (SDL_MAX_UINT32 - sensor->hwdata->last_tick + tick + 1); 151 } else { 152 delta = (tick - sensor->hwdata->last_tick); 153 } 154 sensor->hwdata->sensor_timestamp += SDL_US_TO_NS(delta); 155 sensor->hwdata->last_tick = tick; 156 157 switch (sensor->type) { 158 case SDL_SENSOR_ACCEL: 159 { 160 float data[3]; 161 data[0] = motionState[i].accelerometer.x * SDL_STANDARD_GRAVITY; 162 data[1] = motionState[i].accelerometer.y * SDL_STANDARD_GRAVITY; 163 data[2] = motionState[i].accelerometer.z * SDL_STANDARD_GRAVITY; 164 SDL_SendSensorUpdate(timestamp, sensor, sensor->hwdata->sensor_timestamp, data, SDL_arraysize(data)); 165 } break; 166 case SDL_SENSOR_GYRO: 167 { 168 float data[3]; 169 data[0] = motionState[i].gyro.x; 170 data[1] = motionState[i].gyro.y; 171 data[2] = motionState[i].gyro.z; 172 SDL_SendSensorUpdate(timestamp, sensor, sensor->hwdata->sensor_timestamp, data, SDL_arraysize(data)); 173 } break; 174 default: 175 break; 176 } 177 } 178 } 179} 180 181static void SDL_VITA_SensorClose(SDL_Sensor *sensor) 182{ 183} 184 185static void SDL_VITA_SensorQuit(void) 186{ 187 sceMotionStopSampling(); 188} 189 190SDL_SensorDriver SDL_VITA_SensorDriver = { 191 SDL_VITA_SensorInit, 192 SDL_VITA_SensorGetCount, 193 SDL_VITA_SensorDetect, 194 SDL_VITA_SensorGetDeviceName, 195 SDL_VITA_SensorGetDeviceType, 196 SDL_VITA_SensorGetDeviceNonPortableType, 197 SDL_VITA_SensorGetDeviceInstanceID, 198 SDL_VITA_SensorOpen, 199 SDL_VITA_SensorUpdate, 200 SDL_VITA_SensorClose, 201 SDL_VITA_SensorQuit, 202}; 203 204#endif // SDL_SENSOR_VITA 205[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.