Repository: libsdl-org/SDL
File: src/joystick/hidapi/SDL_hidapi_8bitdo.c (C)

Code before:
        case 1:
            state = SDL_POWERSTATE_CHARGING;
            percent = level;
            break;
        case 2:
            state = SDL_POWERSTATE_CHARGED;
            percent = 100;
            break;
        default:
            state = SDL_POWERSTATE_UNKNOWN;
            percent = 0;
            break;
        }
        SDL_SendJoystickPowerInfo(joystick, state, percent);
    }

    if (ctx->sensors_enabled) {
        Uint64 sensor_timestamp;
        float values[3];
        ABITDO_SENSORS *sensors = (ABITDO_SENSORS *)&data[15];

         if (ctx->sensor_timestamp_supported) {
            Uint32 delta;
            Uint32 tick = LOAD32(data[27], data[28], data[29], data[30]);
            if (ctx->last_tick < tick) {
                delta = (tick - ctx->last_tick);
            } else {
                delta = (SDL_MAX_UINT32 - ctx->last_tick + tick + 1);
            }
            ctx->last_tick = tick;
            ctx->sensor_timestamp_interval = SDL_US_TO_NS(delta);
        }

        // Note: we cannot use the time stamp of the receiving computer due to packet delay creating "spiky" timings.
        // The imu time stamp is intended to be the sample time of the on-board hardware.
        // In the absence of time stamp data from the data[], we can simulate that by
        // advancing a time stamp by the observed/known imu clock rate. This is 8ms = 125 Hz
        sensor_timestamp = ctx->sensor_timestamp;
        ctx->sensor_timestamp += ctx->sensor_timestamp_interval;

        // This device's IMU values are reported differently from SDL
        // Thus we perform a rotation of the coordinate system to match the SDL standard.

        // By observation of this device:
        // Hardware x is reporting roll (rotation about the power jack's axis)
        // Hardware y is reporting pitch (rotation about the horizontal axis)
        // Hardware z is reporting yaw (rotation about the joysticks' center axis)
        values[0] = -sensors->sGyroY * ctx->gyroScale;  // Rotation around pitch axis
        values[1] = sensors->sGyroZ * ctx->gyroScale;   // Rotation around yaw axis
        values[2] = -sensors->sGyroX * ctx->gyroScale;  // Rotation around roll axis
        SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_GYRO, sensor_timestamp, values, 3);

Review comment (suggestion):
It looks like there will be a very large delta on the first packet.

Maybe something like:
```c
if (ctx->last_tick) {
    // code in the PR
} else {
    // Use the initial timestamp interval for the first tick
    ctx->last_tick = tick;
}

Code after:
        case 1:
            state = SDL_POWERSTATE_CHARGING;
            percent = level;
            break;
        case 2:
            state = SDL_POWERSTATE_CHARGED;
            percent = 100;
            break;
        default:
            state = SDL_POWERSTATE_UNKNOWN;
            percent = 0;
            break;
        }
        SDL_SendJoystickPowerInfo(joystick, state, percent);
    }

    if (ctx->sensors_enabled) {
        Uint64 sensor_timestamp;
        float values[3];
        ABITDO_SENSORS *sensors = (ABITDO_SENSORS *)&data[15];

         if (ctx->sensor_timestamp_supported) {
            Uint32 delta;
            Uint32 tick = LOAD32(data[27], data[28], data[29], data[30]);

            if (ctx->last_tick) {
                if (ctx->last_tick < tick) {
                    delta = (tick - ctx->last_tick);
                } else {
                    delta = (SDL_MAX_UINT32 - ctx->last_tick + tick + 1);
                }
                ctx->sensor_timestamp_interval = SDL_US_TO_NS(delta);
            } 
            ctx->last_tick = tick;
        }

        // Note: we cannot use the time stamp of the receiving computer due to packet delay creating "spiky" timings.
        // The imu time stamp is intended to be the sample time of the on-board hardware.
        // In the absence of time stamp data from the data[], we can simulate that by
        // advancing a time stamp by the observed/known imu clock rate. This is 8ms = 125 Hz
        sensor_timestamp = ctx->sensor_timestamp;
        ctx->sensor_timestamp += ctx->sensor_timestamp_interval;

        // This device's IMU values are reported differently from SDL
        // Thus we perform a rotation of the coordinate system to match the SDL standard.

        // By observation of this device:
        // Hardware x is reporting roll (rotation about the power jack's axis)
        // Hardware y is reporting pitch (rotation about the horizontal axis)
        // Hardware z is reporting yaw (rotation about the joysticks' center axis)
        values[0] = -sensors->sGyroY * ctx->gyroScale;  // Rotation around pitch axis