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.

{
static char unknown_type[64];
switch (type)
{
case SDL_SENSOR_INVALID:
return "SDL_SENSOR_INVALID";
case SDL_SENSOR_UNKNOWN:
return "SDL_SENSOR_UNKNOWN";
case SDL_SENSOR_ACCEL:
return "SDL_SENSOR_ACCEL";
case SDL_SENSOR_GYRO:
return "SDL_SENSOR_GYRO";
default:
SDL_snprintf(unknown_type, sizeof(unknown_type), "UNKNOWN (%d)", type);
return unknown_type;
}
}

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.

{
SDL_Sensor *sensor = SDL_SensorFromInstanceID(event->which);
if (!sensor) {
SDL_Log("Couldn't get sensor for sensor event\n");
return;
}
switch (SDL_SensorGetType(sensor)) {
case SDL_SENSOR_ACCEL:
SDL_Log("Accelerometer update: %.2f, %.2f, %.2f\n", event->data[0], event->data[1], event->data[2]);
break;
case SDL_SENSOR_GYRO:
SDL_Log("Gyro update: %.2f, %.2f, %.2f\n", event->data[0], event->data[1], event->data[2]);
break;
default:
SDL_Log("Sensor update for sensor type %s\n", GetSensorTypeString(SDL_SensorGetType(sensor)));
break;
}
}

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.

{
int i;
int num_sensors, num_opened;
/* Load the SDL library */
if (SDL_Init(SDL_INIT_SENSOR) < 0) {
SDL_Log("Couldn't initialize SDL: %s\n", SDL_GetError());
return (1);
}
num_sensors = SDL_NumSensors();
num_opened = 0;
SDL_Log("There are %d sensors available\n", num_sensors);
for (i = 0; i < num_sensors; ++i) {
SDL_Log("Sensor %d: %s, type %s, platform type %d\n",
SDL_SensorGetDeviceInstanceID(i),
SDL_SensorGetDeviceName(i),
GetSensorTypeString(SDL_SensorGetDeviceType(i)),
SDL_SensorGetDeviceNonPortableType(i));
if (SDL_SensorGetDeviceType(i) != SDL_SENSOR_UNKNOWN) {
SDL_Sensor *sensor = SDL_SensorOpen(i);
if (sensor == NULL) {
SDL_Log("Couldn't open sensor %d: %s\n", SDL_SensorGetDeviceInstanceID(i), SDL_GetError());
} else {
++num_opened;
}
}
}
SDL_Log("Opened %d sensors\n", num_opened);
if (num_opened > 0) {
SDL_bool done = SDL_FALSE;
SDL_Event event;
SDL_CreateWindow("Sensor Test", 0, 0, 0, 0, SDL_WINDOW_FULLSCREEN_DESKTOP);
while (!done) {
while (SDL_PollEvent(&event) > 0) {
switch (event.type) {
case SDL_SENSORUPDATE:
HandleSensorEvent(&event.sensor);
break;
case SDL_MOUSEBUTTONUP:
case SDL_KEYUP:
case SDL_QUIT:
done = SDL_TRUE;
break;
default:
break;
}
}
}
}
SDL_Quit();
return (0);
}

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.

SDL_GetError
#define SDL_GetError
Definition: SDL_dynapi_overrides.h:113
SDL_SENSOR_UNKNOWN
@ SDL_SENSOR_UNKNOWN
Definition: SDL_sensor.h:72
SDL_PollEvent
#define SDL_PollEvent
Definition: SDL_dynapi_overrides.h:122
NULL
#define NULL
Definition: begin_code.h:167
SDL_KEYUP
@ SDL_KEYUP
Definition: SDL_events.h:97
SDL_SensorGetType
#define SDL_SensorGetType
Definition: SDL_dynapi_overrides.h:691
SDL_MOUSEBUTTONUP
@ SDL_MOUSEBUTTONUP
Definition: SDL_events.h:107
SDL_WINDOW_FULLSCREEN_DESKTOP
@ SDL_WINDOW_FULLSCREEN_DESKTOP
Definition: SDL_video.h:111
SDL_CreateWindow
#define SDL_CreateWindow
Definition: SDL_dynapi_overrides.h:514
HandleSensorEvent
static void HandleSensorEvent(SDL_SensorEvent *event)
Definition: testsensor.c:37
SDL_SensorFromInstanceID
#define SDL_SensorFromInstanceID
Definition: SDL_dynapi_overrides.h:689
SDL_SENSOR_GYRO
@ SDL_SENSOR_GYRO
Definition: SDL_sensor.h:74
SDL_INIT_SENSOR
#define SDL_INIT_SENSOR
Definition: SDL.h:84
event
struct _cl_event * event
Definition: SDL_opengl_glext.h:2649
SDL_FALSE
@ SDL_FALSE
Definition: SDL_stdinc.h:163
done
int done
Definition: checkkeys.c:28
SDL_SensorOpen
#define SDL_SensorOpen
Definition: SDL_dynapi_overrides.h:688
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:31
SDL_SENSORUPDATE
@ SDL_SENSORUPDATE
Definition: SDL_events.h:151
SDL_QUIT
@ SDL_QUIT
Definition: SDL_events.h:60
SDL_SensorGetDeviceInstanceID
#define SDL_SensorGetDeviceInstanceID
Definition: SDL_dynapi_overrides.h:687
SDL_Quit
#define SDL_Quit
Definition: SDL_dynapi_overrides.h:58
SDL_SENSOR_INVALID
@ SDL_SENSOR_INVALID
Definition: SDL_sensor.h:71
SDL_SensorGetDeviceNonPortableType
#define SDL_SensorGetDeviceNonPortableType
Definition: SDL_dynapi_overrides.h:686
SDL_SensorGetDeviceName
#define SDL_SensorGetDeviceName
Definition: SDL_dynapi_overrides.h:684
SDL_TRUE
@ SDL_TRUE
Definition: SDL_stdinc.h:164
SDL_snprintf
#define SDL_snprintf
Definition: SDL_dynapi_overrides.h:40
SDL_Event
General event structure.
Definition: SDL_events.h:557
SDL_SENSOR_ACCEL
@ SDL_SENSOR_ACCEL
Definition: SDL_sensor.h:73
SDL_NumSensors
#define SDL_NumSensors
Definition: SDL_dynapi_overrides.h:683
SDL_Init
#define SDL_Init
Definition: SDL_dynapi_overrides.h:54
GetSensorTypeString
static const char * GetSensorTypeString(SDL_SensorType type)
Definition: testsensor.c:17
type
GLuint GLuint GLsizei GLenum type
Definition: SDL_opengl.h:1571
i
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
SDL_SensorGetDeviceType
#define SDL_SensorGetDeviceType
Definition: SDL_dynapi_overrides.h:685
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:161