| 1 |
/* |
| 2 |
Copyright (C) 1997-2026 Sam Lantinga <[email protected]> |
| 3 |
|
| 4 |
This software is provided 'as-is', without any express or implied |
| 5 |
warranty. In no event will the authors be held liable for any damages |
| 6 |
arising from the use of this software. |
| 7 |
|
| 8 |
Permission is granted to anyone to use this software for any purpose, |
| 9 |
including commercial applications, and to alter it and redistribute it |
| 10 |
freely. |
| 11 |
*/ |
| 12 |
|
| 13 |
/* Simple test of the SDL sensor code */ |
| 14 |
|
| 15 |
#include "SDL.h" |
| 16 |
|
| 17 |
static const char *GetSensorTypeString(SDL_SensorType type) |
| 18 |
{ |
| 19 |
static char unknown_type[64]; |
| 20 |
|
| 21 |
switch (type) { |
| 22 |
case SDL_SENSOR_INVALID: |
| 23 |
return "SDL_SENSOR_INVALID"; |
| 24 |
case SDL_SENSOR_UNKNOWN: |
| 25 |
return "SDL_SENSOR_UNKNOWN"; |
| 26 |
case SDL_SENSOR_ACCEL: |
| 27 |
return "SDL_SENSOR_ACCEL"; |
| 28 |
case SDL_SENSOR_GYRO: |
| 29 |
return "SDL_SENSOR_GYRO"; |
| 30 |
default: |
| 31 |
(void)SDL_snprintf(unknown_type, sizeof(unknown_type), "UNKNOWN (%d)", type); |
| 32 |
return unknown_type; |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
static void HandleSensorEvent(SDL_SensorEvent *event) |
| 37 |
{ |
| 38 |
SDL_Sensor *sensor = SDL_SensorFromInstanceID(event->which); |
| 39 |
if (!sensor) { |
| 40 |
SDL_Log("Couldn't get sensor for sensor event\n"); |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
switch (SDL_SensorGetType(sensor)) { |
| 45 |
case SDL_SENSOR_ACCEL: |
| 46 |
SDL_Log("Accelerometer update: %.2f, %.2f, %.2f\n", event->data[0], event->data[1], event->data[2]); |
| 47 |
break; |
| 48 |
case SDL_SENSOR_GYRO: |
| 49 |
SDL_Log("Gyro update: %.2f, %.2f, %.2f\n", event->data[0], event->data[1], event->data[2]); |
| 50 |
break; |
| 51 |
default: |
| 52 |
SDL_Log("Sensor update for sensor type %s\n", GetSensorTypeString(SDL_SensorGetType(sensor))); |
| 53 |
break; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
int main(int argc, char **argv) |
| 58 |
{ |
| 59 |
int i; |
| 60 |
int num_sensors, num_opened; |
| 61 |
|
| 62 |
/* Load the SDL library */ |
| 63 |
if (SDL_Init(SDL_INIT_SENSOR) < 0) { |
| 64 |
SDL_Log("Couldn't initialize SDL: %s\n", SDL_GetError()); |
| 65 |
return 1; |
| 66 |
} |
| 67 |
|
| 68 |
num_sensors = SDL_NumSensors(); |
| 69 |
num_opened = 0; |
| 70 |
|
| 71 |
SDL_Log("There are %d sensors available\n", num_sensors); |
| 72 |
for (i = 0; i < num_sensors; ++i) { |
| 73 |
SDL_Log("Sensor %" SDL_PRIs32 ": %s, type %s, platform type %d\n", |
| 74 |
SDL_SensorGetDeviceInstanceID(i), |
| 75 |
SDL_SensorGetDeviceName(i), |
| 76 |
GetSensorTypeString(SDL_SensorGetDeviceType(i)), |
| 77 |
SDL_SensorGetDeviceNonPortableType(i)); |
| 78 |
|
| 79 |
if (SDL_SensorGetDeviceType(i) != SDL_SENSOR_UNKNOWN) { |
| 80 |
SDL_Sensor *sensor = SDL_SensorOpen(i); |
| 81 |
if (!sensor) { |
| 82 |
SDL_Log("Couldn't open sensor %" SDL_PRIs32 ": %s\n", SDL_SensorGetDeviceInstanceID(i), SDL_GetError()); |
| 83 |
} else { |
| 84 |
++num_opened; |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
SDL_Log("Opened %d sensors\n", num_opened); |
| 89 |
|
| 90 |
if (num_opened > 0) { |
| 91 |
SDL_bool done = SDL_FALSE; |
| 92 |
SDL_Event event; |
| 93 |
|
| 94 |
SDL_CreateWindow("Sensor Test", 0, 0, 0, 0, SDL_WINDOW_FULLSCREEN_DESKTOP); |
| 95 |
while (!done) { |
| 96 |
/* Update to get the current event state */ |
| 97 |
SDL_PumpEvents(); |
| 98 |
|
| 99 |
/* Process all currently pending events */ |
| 100 |
while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) == 1) { |
| 101 |
switch (event.type) { |
| 102 |
case SDL_SENSORUPDATE: |
| 103 |
HandleSensorEvent(&event.sensor); |
| 104 |
break; |
| 105 |
case SDL_MOUSEBUTTONUP: |
| 106 |
case SDL_KEYUP: |
| 107 |
case SDL_QUIT: |
| 108 |
done = SDL_TRUE; |
| 109 |
break; |
| 110 |
default: |
| 111 |
break; |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
SDL_Quit(); |
| 118 |
return 0; |
| 119 |
} |