Skip to content

Commit f30f968

Browse files
committed
Work in progress, iterating on heartbeat logic
1 parent 2cfb90d commit f30f968

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

src/joystick/hidapi/SDL_hidapi_flydigi.c

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -196,19 +196,19 @@ static bool GetReply(SDL_HIDAPI_Device* device, Uint8 command, Uint8* data, size
196196
return false;
197197
}
198198

199-
static bool SDL_HIDAPI_Flydigi_SendHeartbeat(SDL_HIDAPI_Device *device)
199+
static bool SDL_HIDAPI_Flydigi_SendAcquireRequest(SDL_HIDAPI_Device *device, bool acquire)
200200
{
201-
const Uint8 acquireControllerCmd[] = { FLYDIGI_V2_CMD_REPORT_ID, FLYDIGI_V2_MAGIC1, FLYDIGI_V2_MAGIC2, FLYDIGI_V2_ACQUIRE_CONTROLLER_COMMAND, 23, 1, 83, 68, 76, 0 };
201+
const Uint8 acquireControllerCmd[] = {
202+
FLYDIGI_V2_CMD_REPORT_ID,
203+
FLYDIGI_V2_MAGIC1,
204+
FLYDIGI_V2_MAGIC2,
205+
FLYDIGI_V2_ACQUIRE_CONTROLLER_COMMAND,
206+
23,
207+
acquire ? 1 : 0,
208+
83, 68, 76, 0
209+
};
202210
if (SDL_hid_write(device->dev, acquireControllerCmd, sizeof(acquireControllerCmd)) < 0) {
203-
return SDL_SetError("Couldn't enable input reports");
204-
}
205-
206-
Uint8 acquireControllerData[USB_PACKET_LENGTH];
207-
if (!GetReply(device, FLYDIGI_V2_ACQUIRE_CONTROLLER_COMMAND, acquireControllerData, sizeof(acquireControllerData))) {
208-
return SDL_SetError("Controller acquiring is not supported");
209-
}
210-
if (acquireControllerData[6] != 1) {
211-
return SDL_SetError("Controller acquiring is disabled");
211+
return SDL_SetError("Couldn't send acquire command");
212212
}
213213
return true;
214214
}
@@ -243,7 +243,16 @@ static bool HIDAPI_DriverFlydigi_InitControllerV2(SDL_HIDAPI_Device *device)
243243

244244
ctx->last_heartbeat = SDL_GetTicks();
245245

246-
return SDL_HIDAPI_Flydigi_SendHeartbeat(device);
246+
if (!SDL_HIDAPI_Flydigi_SendAcquireRequest(device, true)) {
247+
return false;
248+
}
249+
if (!GetReply(device, FLYDIGI_V2_ACQUIRE_CONTROLLER_COMMAND, data, sizeof(data))) {
250+
return SDL_SetError("Controller acquiring is not supported");
251+
}
252+
if (data[6] != 1) {
253+
return SDL_SetError("Controller acquiring is disabled");
254+
}
255+
return true;
247256
}
248257

249258
static void HIDAPI_DriverFlydigi_UpdateDeviceIdentity(SDL_HIDAPI_Device *device)
@@ -770,6 +779,14 @@ static bool HIDAPI_DriverFlydigi_UpdateDevice(SDL_HIDAPI_Device *device)
770779
return false;
771780
}
772781

782+
if (device->vendor_id == USB_VENDOR_FLYDIGI_V2) {
783+
Uint64 now = SDL_GetTicks();
784+
if (now >= (ctx->last_heartbeat + FLYDIGI_ACQUIRE_CONTROLLER_HEARTBEAT_TIME)) {
785+
SDL_HIDAPI_Flydigi_SendAcquireRequest(device, true);
786+
ctx->last_heartbeat = now;
787+
}
788+
}
789+
773790
while ((size = SDL_hid_read_timeout(device->dev, data, sizeof(data), 0)) > 0) {
774791
#ifdef DEBUG_FLYDIGI_PROTOCOL
775792
HIDAPI_DumpPacket("Flydigi packet: size = %d", data, size);
@@ -784,18 +801,6 @@ static bool HIDAPI_DriverFlydigi_UpdateDevice(SDL_HIDAPI_Device *device)
784801
}
785802
}
786803

787-
if (device->vendor_id == USB_VENDOR_FLYDIGI_V2) {
788-
Uint64 now = SDL_GetTicks();
789-
if (now >= (ctx->last_heartbeat + FLYDIGI_ACQUIRE_CONTROLLER_HEARTBEAT_TIME)) {
790-
if (!SDL_HIDAPI_Flydigi_SendHeartbeat(device)) {
791-
// We can no longer acquire the device, mark it as disconnected
792-
HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
793-
return false;
794-
}
795-
ctx->last_heartbeat = now;
796-
}
797-
}
798-
799804
if (size < 0) {
800805
// Read error, device is disconnected
801806
HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
@@ -805,8 +810,7 @@ static bool HIDAPI_DriverFlydigi_UpdateDevice(SDL_HIDAPI_Device *device)
805810

806811
static void HIDAPI_DriverFlydigi_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
807812
{
808-
const Uint8 acquireControllerCmd[] = { FLYDIGI_V2_CMD_REPORT_ID, FLYDIGI_V2_MAGIC1, FLYDIGI_V2_MAGIC2, FLYDIGI_V2_ACQUIRE_CONTROLLER_COMMAND, 23, 0, 83, 68, 76, 0 };
809-
SDL_hid_write(device->dev, acquireControllerCmd, sizeof(acquireControllerCmd));
813+
SDL_HIDAPI_Flydigi_SendAcquireRequest(device, false);
810814
}
811815

812816
static void HIDAPI_DriverFlydigi_FreeDevice(SDL_HIDAPI_Device *device)

0 commit comments

Comments
 (0)