2018-08-21 22:11:34 +03:00
|
|
|
/*
|
2023-01-09 20:41:41 +03:00
|
|
|
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
|
2018-08-21 22:11:34 +03:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Simple test of the SDL sensor code */
|
|
|
|
|
2022-11-27 07:43:38 +03:00
|
|
|
#include <SDL3/SDL.h>
|
2022-12-15 07:58:20 +03:00
|
|
|
#include <SDL3/SDL_main.h>
|
2023-03-17 02:25:39 +03:00
|
|
|
#include <SDL3/SDL_test.h>
|
2018-08-21 22:11:34 +03:00
|
|
|
|
|
|
|
static const char *GetSensorTypeString(SDL_SensorType type)
|
|
|
|
{
|
|
|
|
static char unknown_type[64];
|
|
|
|
|
2022-11-30 23:51:59 +03:00
|
|
|
switch (type) {
|
2018-08-21 22:11:34 +03:00
|
|
|
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:
|
2023-03-10 02:10:00 +03:00
|
|
|
(void)SDL_snprintf(unknown_type, sizeof(unknown_type), "UNKNOWN (%d)", type);
|
2018-08-21 22:11:34 +03:00
|
|
|
return unknown_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void HandleSensorEvent(SDL_SensorEvent *event)
|
|
|
|
{
|
2022-12-27 17:23:39 +03:00
|
|
|
SDL_Sensor *sensor = SDL_GetSensorFromInstanceID(event->which);
|
2022-11-27 19:38:43 +03:00
|
|
|
if (sensor == NULL) {
|
2018-08-21 22:11:34 +03:00
|
|
|
SDL_Log("Couldn't get sensor for sensor event\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-27 17:23:39 +03:00
|
|
|
switch (SDL_GetSensorType(sensor)) {
|
2018-08-21 22:11:34 +03:00
|
|
|
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:
|
2022-12-27 17:23:39 +03:00
|
|
|
SDL_Log("Sensor update for sensor type %s\n", GetSensorTypeString(SDL_GetSensorType(sensor)));
|
2018-08-21 22:11:34 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-30 23:51:59 +03:00
|
|
|
int main(int argc, char **argv)
|
2018-08-21 22:11:34 +03:00
|
|
|
{
|
2022-12-28 05:10:06 +03:00
|
|
|
SDL_SensorID *sensors;
|
|
|
|
int i, num_sensors, num_opened;
|
2023-03-17 02:25:39 +03:00
|
|
|
SDLTest_CommonState *state;
|
|
|
|
|
|
|
|
/* Initialize test framework */
|
|
|
|
state = SDLTest_CommonCreateState(argv, 0);
|
|
|
|
if (state == NULL) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Enable standard application logging */
|
|
|
|
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
|
|
|
|
|
|
|
if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
|
|
|
|
SDLTest_CommonDestroyState(state);
|
|
|
|
return 1;
|
|
|
|
}
|
2018-08-21 22:11:34 +03:00
|
|
|
|
|
|
|
/* Load the SDL library */
|
|
|
|
if (SDL_Init(SDL_INIT_SENSOR) < 0) {
|
|
|
|
SDL_Log("Couldn't initialize SDL: %s\n", SDL_GetError());
|
2023-03-17 02:25:39 +03:00
|
|
|
SDLTest_CommonDestroyState(state);
|
2022-11-27 19:38:43 +03:00
|
|
|
return 1;
|
2018-08-21 22:11:34 +03:00
|
|
|
}
|
|
|
|
|
2022-12-28 05:10:06 +03:00
|
|
|
sensors = SDL_GetSensors(&num_sensors);
|
2018-08-21 22:11:34 +03:00
|
|
|
num_opened = 0;
|
|
|
|
|
|
|
|
SDL_Log("There are %d sensors available\n", num_sensors);
|
2022-12-28 05:10:06 +03:00
|
|
|
if (sensors) {
|
|
|
|
for (i = 0; i < num_sensors; ++i) {
|
|
|
|
SDL_Log("Sensor %" SDL_PRIu32 ": %s, type %s, platform type %d\n",
|
|
|
|
sensors[i],
|
|
|
|
SDL_GetSensorInstanceName(sensors[i]),
|
|
|
|
GetSensorTypeString(SDL_GetSensorInstanceType(sensors[i])),
|
|
|
|
SDL_GetSensorInstanceNonPortableType(sensors[i]));
|
|
|
|
|
|
|
|
if (SDL_GetSensorInstanceType(sensors[i]) != SDL_SENSOR_UNKNOWN) {
|
|
|
|
SDL_Sensor *sensor = SDL_OpenSensor(sensors[i]);
|
|
|
|
if (sensor == NULL) {
|
|
|
|
SDL_Log("Couldn't open sensor %" SDL_PRIu32 ": %s\n", sensors[i], SDL_GetError());
|
|
|
|
} else {
|
|
|
|
++num_opened;
|
|
|
|
}
|
2018-08-21 22:11:34 +03:00
|
|
|
}
|
|
|
|
}
|
2022-12-28 05:10:06 +03:00
|
|
|
SDL_free(sensors);
|
2018-08-21 22:11:34 +03:00
|
|
|
}
|
|
|
|
SDL_Log("Opened %d sensors\n", num_opened);
|
|
|
|
|
|
|
|
if (num_opened > 0) {
|
|
|
|
SDL_bool done = SDL_FALSE;
|
|
|
|
SDL_Event event;
|
|
|
|
|
2023-03-06 01:44:38 +03:00
|
|
|
SDL_CreateWindow("Sensor Test", 0, 0, SDL_WINDOW_FULLSCREEN);
|
2018-08-21 22:11:34 +03:00
|
|
|
while (!done) {
|
2021-07-29 16:36:20 +03:00
|
|
|
/* Update to get the current event state */
|
|
|
|
SDL_PumpEvents();
|
|
|
|
|
|
|
|
/* Process all currently pending events */
|
2023-01-24 04:54:09 +03:00
|
|
|
while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_EVENT_FIRST, SDL_EVENT_LAST) == 1) {
|
2018-08-21 22:11:34 +03:00
|
|
|
switch (event.type) {
|
2023-01-24 04:54:09 +03:00
|
|
|
case SDL_EVENT_SENSOR_UPDATE:
|
2018-08-21 22:11:34 +03:00
|
|
|
HandleSensorEvent(&event.sensor);
|
|
|
|
break;
|
2023-01-30 06:06:08 +03:00
|
|
|
case SDL_EVENT_MOUSE_BUTTON_UP:
|
2023-01-24 04:54:09 +03:00
|
|
|
case SDL_EVENT_KEY_UP:
|
|
|
|
case SDL_EVENT_QUIT:
|
2018-08-21 22:11:34 +03:00
|
|
|
done = SDL_TRUE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Quit();
|
2023-03-17 02:25:39 +03:00
|
|
|
SDLTest_CommonDestroyState(state);
|
2022-11-27 19:38:43 +03:00
|
|
|
return 0;
|
2018-08-21 22:11:34 +03:00
|
|
|
}
|