🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.3 KiB

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