SDL 2.0
testsensor.c File Reference
#include "SDL.h"
+ Include dependency graph for testsensor.c:

Go to the source code of this file.

Functions

static const char * GetSensorTypeString (SDL_SensorType type)
 
static void HandleSensorEvent (SDL_SensorEvent *event)
 
int main (int argc, char **argv)
 

Function Documentation

◆ GetSensorTypeString()

static const char * GetSensorTypeString ( SDL_SensorType  type)
static

Definition at line 17 of file testsensor.c.

18{
19 static char unknown_type[64];
20
21 switch (type)
22 {
24 return "SDL_SENSOR_INVALID";
26 return "SDL_SENSOR_UNKNOWN";
28 return "SDL_SENSOR_ACCEL";
29 case SDL_SENSOR_GYRO:
30 return "SDL_SENSOR_GYRO";
31 default:
32 SDL_snprintf(unknown_type, sizeof(unknown_type), "UNKNOWN (%d)", type);
33 return unknown_type;
34 }
35}
#define SDL_snprintf
GLuint GLuint GLsizei GLenum type
Definition: SDL_opengl.h:1571
@ SDL_SENSOR_INVALID
Definition: SDL_sensor.h:71
@ SDL_SENSOR_GYRO
Definition: SDL_sensor.h:74
@ SDL_SENSOR_UNKNOWN
Definition: SDL_sensor.h:72
@ SDL_SENSOR_ACCEL
Definition: SDL_sensor.h:73

References SDL_SENSOR_ACCEL, SDL_SENSOR_GYRO, SDL_SENSOR_INVALID, SDL_SENSOR_UNKNOWN, and SDL_snprintf.

Referenced by HandleSensorEvent(), and main().

◆ HandleSensorEvent()

static void HandleSensorEvent ( SDL_SensorEvent event)
static

Definition at line 37 of file testsensor.c.

38{
39 SDL_Sensor *sensor = SDL_SensorFromInstanceID(event->which);
40 if (!sensor) {
41 SDL_Log("Couldn't get sensor for sensor event\n");
42 return;
43 }
44
45 switch (SDL_SensorGetType(sensor)) {
47 SDL_Log("Accelerometer update: %.2f, %.2f, %.2f\n", event->data[0], event->data[1], event->data[2]);
48 break;
49 case SDL_SENSOR_GYRO:
50 SDL_Log("Gyro update: %.2f, %.2f, %.2f\n", event->data[0], event->data[1], event->data[2]);
51 break;
52 default:
53 SDL_Log("Sensor update for sensor type %s\n", GetSensorTypeString(SDL_SensorGetType(sensor)));
54 break;
55 }
56}
#define SDL_SensorGetType
#define SDL_SensorFromInstanceID
#define SDL_Log
struct _cl_event * event
static const char * GetSensorTypeString(SDL_SensorType type)
Definition: testsensor.c:17

References GetSensorTypeString(), SDL_Log, SDL_SENSOR_ACCEL, SDL_SENSOR_GYRO, SDL_SensorFromInstanceID, and SDL_SensorGetType.

Referenced by main().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 59 of file testsensor.c.

60{
61 int i;
62 int num_sensors, num_opened;
63
64 /* Load the SDL library */
65 if (SDL_Init(SDL_INIT_SENSOR) < 0) {
66 SDL_Log("Couldn't initialize SDL: %s\n", SDL_GetError());
67 return (1);
68 }
69
70 num_sensors = SDL_NumSensors();
71 num_opened = 0;
72
73 SDL_Log("There are %d sensors available\n", num_sensors);
74 for (i = 0; i < num_sensors; ++i) {
75 SDL_Log("Sensor %d: %s, type %s, platform type %d\n",
80
82 SDL_Sensor *sensor = SDL_SensorOpen(i);
83 if (sensor == NULL) {
84 SDL_Log("Couldn't open sensor %d: %s\n", SDL_SensorGetDeviceInstanceID(i), SDL_GetError());
85 } else {
86 ++num_opened;
87 }
88 }
89 }
90 SDL_Log("Opened %d sensors\n", num_opened);
91
92 if (num_opened > 0) {
95
96 SDL_CreateWindow("Sensor Test", 0, 0, 0, 0, SDL_WINDOW_FULLSCREEN_DESKTOP);
97 while (!done) {
98 while (SDL_PollEvent(&event) > 0) {
99 switch (event.type) {
100 case SDL_SENSORUPDATE:
101 HandleSensorEvent(&event.sensor);
102 break;
104 case SDL_KEYUP:
105 case SDL_QUIT:
106 done = SDL_TRUE;
107 break;
108 default:
109 break;
110 }
111 }
112 }
113 }
114
115 SDL_Quit();
116 return (0);
117}
#define SDL_INIT_SENSOR
Definition: SDL.h:84
#define SDL_SensorGetDeviceNonPortableType
#define SDL_GetError
#define SDL_SensorOpen
#define SDL_PollEvent
#define SDL_CreateWindow
#define SDL_SensorGetDeviceInstanceID
#define SDL_NumSensors
#define SDL_SensorGetDeviceName
#define SDL_Quit
#define SDL_Init
#define SDL_SensorGetDeviceType
@ SDL_SENSORUPDATE
Definition: SDL_events.h:151
@ SDL_QUIT
Definition: SDL_events.h:60
@ SDL_MOUSEBUTTONUP
Definition: SDL_events.h:107
@ SDL_KEYUP
Definition: SDL_events.h:97
SDL_bool
Definition: SDL_stdinc.h:162
@ SDL_TRUE
Definition: SDL_stdinc.h:164
@ SDL_FALSE
Definition: SDL_stdinc.h:163
@ SDL_WINDOW_FULLSCREEN_DESKTOP
Definition: SDL_video.h:111
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define NULL
Definition: begin_code.h:167
int done
Definition: checkkeys.c:28
static void HandleSensorEvent(SDL_SensorEvent *event)
Definition: testsensor.c:37
General event structure.
Definition: SDL_events.h:558

References done, GetSensorTypeString(), HandleSensorEvent(), i, NULL, SDL_CreateWindow, SDL_FALSE, SDL_GetError, SDL_Init, SDL_INIT_SENSOR, SDL_KEYUP, SDL_Log, SDL_MOUSEBUTTONUP, SDL_NumSensors, SDL_PollEvent, SDL_QUIT, SDL_Quit, SDL_SENSOR_UNKNOWN, SDL_SensorGetDeviceInstanceID, SDL_SensorGetDeviceName, SDL_SensorGetDeviceNonPortableType, SDL_SensorGetDeviceType, SDL_SensorOpen, SDL_SENSORUPDATE, SDL_TRUE, and SDL_WINDOW_FULLSCREEN_DESKTOP.