22
22
23
23
#ifdef SDL_JOYSTICK_HIDAPI
24
24
25
+ #include "../../SDL_hints_c.h"
25
26
#include "../SDL_sysjoystick.h"
26
27
#include "SDL_hidapijoystick_c.h"
27
28
#include "SDL_hidapi_rumble.h"
47
48
#define SDL_8BITDO_REPORTID_NOT_SUPPORTED_SDL_REPORTID 0x03
48
49
#define SDL_8BITDO_BT_REPORTID_SDL_REPORTID 0x01
49
50
51
+ #define SDL_8BITDO_SENSOR_TIMESTAMP_ENABLE 0xAA
50
52
#define ABITDO_ACCEL_SCALE 4096.f
51
53
#define ABITDO_GYRO_MAX_DEGREES_PER_SECOND 2000.f
52
54
55
+
56
+ #define LOAD32 (A , B , C , D ) ((((Uint32)(A)) << 0) | \
57
+ (((Uint32)(B)) << 8) | \
58
+ (((Uint32)(C)) << 16) | \
59
+ (((Uint32)(D)) << 24))
60
+
61
+
53
62
typedef struct
54
63
{
55
64
bool sensors_supported ;
@@ -61,6 +70,7 @@ typedef struct
61
70
bool rgb_supported ;
62
71
bool player_led_supported ;
63
72
bool powerstate_supported ;
73
+ bool sensor_timestamp_supported ;
64
74
Uint8 serial [6 ];
65
75
Uint16 version ;
66
76
Uint16 version_beta ;
@@ -69,6 +79,7 @@ typedef struct
69
79
Uint8 last_state [USB_PACKET_LENGTH ];
70
80
Uint64 sensor_timestamp ; // Nanoseconds. Simulate onboard clock. Different models have different rates vs different connection styles.
71
81
Uint64 sensor_timestamp_interval ;
82
+ Uint32 last_tick ;
72
83
} SDL_Driver8BitDo_Context ;
73
84
74
85
#pragma pack(push,1)
@@ -181,6 +192,9 @@ static bool HIDAPI_Driver8BitDo_InitDevice(SDL_HIDAPI_Device *device)
181
192
ctx -> rumble_supported = true;
182
193
ctx -> powerstate_supported = true;
183
194
195
+ if (data [13 ] == SDL_8BITDO_SENSOR_TIMESTAMP_ENABLE ) {
196
+ ctx -> sensor_timestamp_supported = true;
197
+ }
184
198
// Set the serial number to the Bluetooth MAC address
185
199
if (size >= 12 && data [10 ] != 0 ) {
186
200
char serial [18 ];
@@ -544,6 +558,21 @@ static void HIDAPI_Driver8BitDo_HandleStatePacket(SDL_Joystick *joystick, SDL_Dr
544
558
float values [3 ];
545
559
ABITDO_SENSORS * sensors = (ABITDO_SENSORS * )& data [15 ];
546
560
561
+ if (ctx -> sensor_timestamp_supported ) {
562
+ Uint32 delta ;
563
+ Uint32 tick = LOAD32 (data [27 ], data [28 ], data [29 ], data [30 ]);
564
+
565
+ if (ctx -> last_tick ) {
566
+ if (ctx -> last_tick < tick ) {
567
+ delta = (tick - ctx -> last_tick );
568
+ } else {
569
+ delta = (SDL_MAX_UINT32 - ctx -> last_tick + tick + 1 );
570
+ }
571
+ ctx -> sensor_timestamp_interval = SDL_US_TO_NS (delta );
572
+ }
573
+ ctx -> last_tick = tick ;
574
+ }
575
+
547
576
// Note: we cannot use the time stamp of the receiving computer due to packet delay creating "spiky" timings.
548
577
// The imu time stamp is intended to be the sample time of the on-board hardware.
549
578
// In the absence of time stamp data from the data[], we can simulate that by
0 commit comments