Skip to content

Commit 6d3e8b7

Browse files
authored
add 8BitDo Controller sensor_timestamp (#13278)
1 parent 727b492 commit 6d3e8b7

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/joystick/hidapi/SDL_hidapi_8bitdo.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#ifdef SDL_JOYSTICK_HIDAPI
2424

25+
#include "../../SDL_hints_c.h"
2526
#include "../SDL_sysjoystick.h"
2627
#include "SDL_hidapijoystick_c.h"
2728
#include "SDL_hidapi_rumble.h"
@@ -47,9 +48,17 @@ enum
4748
#define SDL_8BITDO_REPORTID_NOT_SUPPORTED_SDL_REPORTID 0x03
4849
#define SDL_8BITDO_BT_REPORTID_SDL_REPORTID 0x01
4950

51+
#define SDL_8BITDO_SENSOR_TIMESTAMP_ENABLE 0xAA
5052
#define ABITDO_ACCEL_SCALE 4096.f
5153
#define ABITDO_GYRO_MAX_DEGREES_PER_SECOND 2000.f
5254

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+
5362
typedef struct
5463
{
5564
bool sensors_supported;
@@ -61,6 +70,7 @@ typedef struct
6170
bool rgb_supported;
6271
bool player_led_supported;
6372
bool powerstate_supported;
73+
bool sensor_timestamp_supported;
6474
Uint8 serial[6];
6575
Uint16 version;
6676
Uint16 version_beta;
@@ -69,6 +79,7 @@ typedef struct
6979
Uint8 last_state[USB_PACKET_LENGTH];
7080
Uint64 sensor_timestamp; // Nanoseconds. Simulate onboard clock. Different models have different rates vs different connection styles.
7181
Uint64 sensor_timestamp_interval;
82+
Uint32 last_tick;
7283
} SDL_Driver8BitDo_Context;
7384

7485
#pragma pack(push,1)
@@ -181,6 +192,9 @@ static bool HIDAPI_Driver8BitDo_InitDevice(SDL_HIDAPI_Device *device)
181192
ctx->rumble_supported = true;
182193
ctx->powerstate_supported = true;
183194

195+
if (data[13] == SDL_8BITDO_SENSOR_TIMESTAMP_ENABLE) {
196+
ctx->sensor_timestamp_supported = true;
197+
}
184198
// Set the serial number to the Bluetooth MAC address
185199
if (size >= 12 && data[10] != 0) {
186200
char serial[18];
@@ -544,6 +558,21 @@ static void HIDAPI_Driver8BitDo_HandleStatePacket(SDL_Joystick *joystick, SDL_Dr
544558
float values[3];
545559
ABITDO_SENSORS *sensors = (ABITDO_SENSORS *)&data[15];
546560

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+
547576
// Note: we cannot use the time stamp of the receiving computer due to packet delay creating "spiky" timings.
548577
// The imu time stamp is intended to be the sample time of the on-board hardware.
549578
// In the absence of time stamp data from the data[], we can simulate that by

0 commit comments

Comments
 (0)