48
48
#define SDL_8BITDO_BT_REPORTID_SDL_REPORTID 0x01
49
49
50
50
#define ABITDO_ACCEL_SCALE 4096.f
51
- #define ABITDO_SENSOR_POLLING_RATE 125.f
52
- #define SENSOR_INTERVAL_NS 8000000ULL
53
51
#define ABITDO_GYRO_MAX_DEGREES_PER_SECOND 2000.f
54
52
55
53
typedef struct
@@ -69,7 +67,8 @@ typedef struct
69
67
float accelScale ;
70
68
float gyroScale ;
71
69
Uint8 last_state [USB_PACKET_LENGTH ];
72
- Uint64 sensor_timestamp ; // Nanoseconds. Simulate onboard clock. Advance by known rate: SENSOR_INTERVAL_NS == 8ms = 125 Hz
70
+ Uint64 sensor_timestamp ; // Nanoseconds. Simulate onboard clock. Different models have different rates vs different connection styles.
71
+ Uint64 sensor_timestamp_interval ;
73
72
} SDL_Driver8BitDo_Context ;
74
73
75
74
#pragma pack(push,1)
@@ -217,6 +216,27 @@ static void HIDAPI_Driver8BitDo_SetDevicePlayerIndex(SDL_HIDAPI_Device *device,
217
216
{
218
217
}
219
218
219
+ static Uint64 HIDAPI_Driver8BitDo_GetIMURateForProductID (SDL_HIDAPI_Device * device )
220
+ {
221
+ // TODO: If sensor time stamp is sent, these fixed settings from observation can be replaced
222
+ switch (device -> product_id ) {
223
+ // Note, This is estimated by observation of Bluetooth packets received in the testcontroller tool
224
+ case USB_PRODUCT_8BITDO_SN30_PRO_BT :
225
+ case USB_PRODUCT_8BITDO_SF30_PRO_BT :
226
+ return 90 ; // Observed to be anywhere between 60-90 hz. Possibly lossy in current state
227
+ case USB_PRODUCT_8BITDO_SF30_PRO :
228
+ case USB_PRODUCT_8BITDO_SN30_PRO :
229
+ return 100 ;
230
+ case USB_PRODUCT_8BITDO_PRO_2 :
231
+ case USB_PRODUCT_8BITDO_PRO_2_BT :// Note, labelled as "BT" but appears this way when wired. Observed bluetooth packet rate seems to be 80-90hz
232
+ return (device -> is_bluetooth ? 85 : 100 );
233
+ case USB_PRODUCT_8BITDO_ULTIMATE2_WIRELESS :
234
+ default :
235
+ return 120 ;
236
+ break ;
237
+ }
238
+ }
239
+
220
240
#ifndef DEG2RAD
221
241
#define DEG2RAD (x ) ((float)(x) * (float)(SDL_PI_F / 180.f))
222
242
#endif
@@ -242,9 +262,13 @@ static bool HIDAPI_Driver8BitDo_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joys
242
262
joystick -> nhats = 1 ;
243
263
244
264
if (ctx -> sensors_supported ) {
245
- SDL_PrivateJoystickAddSensor (joystick , SDL_SENSOR_GYRO , ABITDO_SENSOR_POLLING_RATE );
246
- SDL_PrivateJoystickAddSensor (joystick , SDL_SENSOR_ACCEL , ABITDO_SENSOR_POLLING_RATE );
247
265
266
+ // Different 8Bitdo controllers in different connection modes have different polling rates.
267
+ const Uint64 imu_polling_rate = HIDAPI_Driver8BitDo_GetIMURateForProductID (device );
268
+ ctx -> sensor_timestamp_interval = SDL_NS_PER_SECOND / imu_polling_rate ;
269
+
270
+ SDL_PrivateJoystickAddSensor (joystick , SDL_SENSOR_GYRO , (float )imu_polling_rate );
271
+ SDL_PrivateJoystickAddSensor (joystick , SDL_SENSOR_ACCEL , (float )imu_polling_rate );
248
272
249
273
ctx -> accelScale = SDL_STANDARD_GRAVITY / ABITDO_ACCEL_SCALE ;
250
274
// Hardware senses +/- N Degrees per second mapped to +/- INT16_MAX
@@ -525,7 +549,7 @@ static void HIDAPI_Driver8BitDo_HandleStatePacket(SDL_Joystick *joystick, SDL_Dr
525
549
// In the absence of time stamp data from the data[], we can simulate that by
526
550
// advancing a time stamp by the observed/known imu clock rate. This is 8ms = 125 Hz
527
551
sensor_timestamp = ctx -> sensor_timestamp ;
528
- ctx -> sensor_timestamp += SENSOR_INTERVAL_NS ;
552
+ ctx -> sensor_timestamp += ctx -> sensor_timestamp_interval ;
529
553
530
554
// This device's IMU values are reported differently from SDL
531
555
// Thus we perform a rotation of the coordinate system to match the SDL standard.
0 commit comments