Commit 2c02b7410cefdf1602c2ad953541bfa62458bb42

Sam Lantinga 2020-03-23T12:10:05

Fixed bug 5052 - Interval between SDL_SENSORUPDATE events for gyroscopes is too high Andrei Kortunov Hello. I try to implement an application for Android, which uses a new sensors API from 2.0.9 to control a camera rotation via built-in gyroscope, using the code from the test/testsensor.c as an example. Gyroscope input itself works well, but an interval between SDL_SENSORUPDATE events is about 200ms (the SENSOR_DELAY_NORMAL, I believe), when I need the interval about 20-40ms (the SENSOR_DELAY_GAME or SENSOR_DELAY_FASTEST).

diff --git a/src/sensor/android/SDL_androidsensor.c b/src/sensor/android/SDL_androidsensor.c
index d7c0a7e..165eb9b 100644
--- a/src/sensor/android/SDL_androidsensor.c
+++ b/src/sensor/android/SDL_androidsensor.c
@@ -130,6 +130,7 @@ static int
 SDL_ANDROID_SensorOpen(SDL_Sensor *sensor, int device_index)
 {
     struct sensor_hwdata *hwdata;
+    int delay_us, min_delay_us;
 
     hwdata = (struct sensor_hwdata *)SDL_calloc(1, sizeof(*hwdata));
     if (hwdata == NULL) {
@@ -149,7 +150,14 @@ SDL_ANDROID_SensorOpen(SDL_Sensor *sensor, int device_index)
         return SDL_SetError("Couldn't enable sensor");
     }
 
-    /* FIXME: What rate should we set for this sensor? 60 FPS? Let's try the default rate for now... */
+    /* Use 60 Hz update rate if possible */
+    /* FIXME: Maybe add a hint for this? */
+    delay_us = 1000000 / 60;
+    min_delay_us = ASensor_getMinDelay(hwdata->asensor);
+    if (delay_us < min_delay_us) {
+        delay_us = min_delay_us;
+    }
+    ASensorEventQueue_setEventRate(hwdata->eventqueue, hwdata->asensor, delay_us);
 
     sensor->hwdata = hwdata;
     return 0;