Fixed bug 5052 - Interval between SDL_SENSORUPDATE events for gyroscopes is too high
authorSam Lantinga <slouken@libsdl.org>
Mon, 23 Mar 2020 12:10:05 -0700
changeset 1366906adf174772e
parent 13668 db0dcf23f7a5
child 13670 63387e8920f5
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).
src/sensor/android/SDL_androidsensor.c
     1.1 --- a/src/sensor/android/SDL_androidsensor.c	Mon Mar 23 14:54:31 2020 -0400
     1.2 +++ b/src/sensor/android/SDL_androidsensor.c	Mon Mar 23 12:10:05 2020 -0700
     1.3 @@ -130,6 +130,7 @@
     1.4  SDL_ANDROID_SensorOpen(SDL_Sensor *sensor, int device_index)
     1.5  {
     1.6      struct sensor_hwdata *hwdata;
     1.7 +    int delay_us, min_delay_us;
     1.8  
     1.9      hwdata = (struct sensor_hwdata *)SDL_calloc(1, sizeof(*hwdata));
    1.10      if (hwdata == NULL) {
    1.11 @@ -149,7 +150,14 @@
    1.12          return SDL_SetError("Couldn't enable sensor");
    1.13      }
    1.14  
    1.15 -    /* FIXME: What rate should we set for this sensor? 60 FPS? Let's try the default rate for now... */
    1.16 +    /* Use 60 Hz update rate if possible */
    1.17 +    /* FIXME: Maybe add a hint for this? */
    1.18 +    delay_us = 1000000 / 60;
    1.19 +    min_delay_us = ASensor_getMinDelay(hwdata->asensor);
    1.20 +    if (delay_us < min_delay_us) {
    1.21 +        delay_us = min_delay_us;
    1.22 +    }
    1.23 +    ASensorEventQueue_setEventRate(hwdata->eventqueue, hwdata->asensor, delay_us);
    1.24  
    1.25      sensor->hwdata = hwdata;
    1.26      return 0;