Skip to content

Commit b152c9f

Browse files
endriftslouken
authored andcommitted
switch2: Refactor out flash reading
1 parent ee4c2ce commit b152c9f

File tree

1 file changed

+48
-56
lines changed

1 file changed

+48
-56
lines changed

src/joystick/hidapi/SDL_hidapi_switch2.c

Lines changed: 48 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,34 @@ static bool UpdateSlotLED(SDL_DriverSwitch2_Context *ctx)
233233
return (RecvBulkData(ctx, reply, sizeof(reply)) > 0);
234234
}
235235

236+
static int ReadFlashBlock(SDL_DriverSwitch2_Context *ctx, Uint32 address, Uint8 *out)
237+
{
238+
unsigned char flash_read_command[] = {
239+
0x02, 0x91, 0x00, 0x01, 0x00, 0x08, 0x00, 0x00,
240+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
241+
};
242+
unsigned char buffer[0x50] = {0};
243+
int res;
244+
245+
flash_read_command[12] = address;
246+
flash_read_command[13] = address >> 8;
247+
flash_read_command[14] = address >> 16;
248+
flash_read_command[15] = address >> 24;
249+
250+
res = SendBulkData(ctx, flash_read_command, sizeof(flash_read_command));
251+
if (res < 0) {
252+
return res;
253+
}
254+
255+
res = RecvBulkData(ctx, buffer, sizeof(buffer));
256+
if (res < 0) {
257+
return res;
258+
}
259+
260+
SDL_memcpy(out, &buffer[0x10], 0x40);
261+
return 0;
262+
}
263+
236264
static void SDLCALL SDL_PlayerLEDHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
237265
{
238266
SDL_DriverSwitch2_Context *ctx = (SDL_DriverSwitch2_Context *)userdata;
@@ -382,81 +410,45 @@ static bool HIDAPI_DriverSwitch2_InitUSB(SDL_HIDAPI_Device *device)
382410
},
383411
NULL, // Sentinel
384412
};
385-
unsigned char flash_read_command[] = {
386-
0x02, 0x91, 0x00, 0x01, 0x00, 0x08, 0x00, 0x00,
387-
0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x01, 0x00
388-
};
389-
unsigned char calibration_data[0x50] = {0};
390413

391-
flash_read_command[12] = 0x80;
392-
res = SendBulkData(ctx, flash_read_command, sizeof(flash_read_command));
414+
unsigned char calibration_data[0x40] = {0};
415+
416+
res = ReadFlashBlock(ctx, 0x13080, calibration_data);
393417
if (res < 0) {
394-
SDL_LogWarn(SDL_LOG_CATEGORY_INPUT, "Couldn't request factory calibration data: %d", res);
418+
SDL_LogWarn(SDL_LOG_CATEGORY_INPUT, "Couldn't read factory calibration data: %d", res);
395419
} else {
396-
res = RecvBulkData(ctx, calibration_data, sizeof(calibration_data));
397-
if (res < 0) {
398-
SDL_LogWarn(SDL_LOG_CATEGORY_INPUT, "Couldn't read factory calibration data: %d", res);
399-
} else {
400-
ParseStickCalibration(&ctx->left_stick, &calibration_data[0x38]);
401-
}
420+
ParseStickCalibration(&ctx->left_stick, &calibration_data[0x28]);
402421
}
403422

404-
flash_read_command[12] = 0xC0;
405-
res = SendBulkData(ctx, flash_read_command, sizeof(flash_read_command));
423+
res = ReadFlashBlock(ctx, 0x130C0, calibration_data);
406424
if (res < 0) {
407-
SDL_LogWarn(SDL_LOG_CATEGORY_INPUT, "Couldn't request factory calibration data: %d", res);
425+
SDL_LogWarn(SDL_LOG_CATEGORY_INPUT, "Couldn't read factory calibration data: %d", res);
408426
} else {
409-
res = RecvBulkData(ctx, calibration_data, sizeof(calibration_data));
410-
if (res < 0) {
411-
SDL_LogWarn(SDL_LOG_CATEGORY_INPUT, "Couldn't read factory calibration data: %d", res);
412-
} else {
413-
ParseStickCalibration(&ctx->right_stick, &calibration_data[0x38]);
414-
}
427+
ParseStickCalibration(&ctx->right_stick, &calibration_data[0x28]);
415428
}
416429

417430
if (device->product_id == USB_PRODUCT_NINTENDO_SWITCH2_GAMECUBE_CONTROLLER) {
418-
flash_read_command[12] = 0x40;
419-
flash_read_command[13] = 0x31;
420-
res = SendBulkData(ctx, flash_read_command, sizeof(flash_read_command));
431+
res = ReadFlashBlock(ctx, 0x13140, calibration_data);
421432
if (res < 0) {
422-
SDL_LogWarn(SDL_LOG_CATEGORY_INPUT, "Couldn't request factory calibration data: %d", res);
433+
SDL_LogWarn(SDL_LOG_CATEGORY_INPUT, "Couldn't read factory calibration data: %d", res);
423434
} else {
424-
res = RecvBulkData(ctx, calibration_data, sizeof(calibration_data));
425-
if (res < 0) {
426-
SDL_LogWarn(SDL_LOG_CATEGORY_INPUT, "Couldn't read factory calibration data: %d", res);
427-
} else {
428-
ctx->left_trigger_zero = calibration_data[0x10];
429-
ctx->right_trigger_zero = calibration_data[0x11];
430-
}
435+
ctx->left_trigger_zero = calibration_data[0];
436+
ctx->right_trigger_zero = calibration_data[1];
431437
}
432438
}
433439

434-
flash_read_command[12] = 0x40;
435-
flash_read_command[13] = 0xC0;
436-
flash_read_command[14] = 0x1F;
437-
res = SendBulkData(ctx, flash_read_command, sizeof(flash_read_command));
440+
res = ReadFlashBlock(ctx, 0x1FC040, calibration_data);
438441
if (res < 0) {
439-
SDL_LogWarn(SDL_LOG_CATEGORY_INPUT, "Couldn't request user calibration data: %d", res);
440-
} else {
441-
res = RecvBulkData(ctx, calibration_data, sizeof(calibration_data));
442-
if (res < 0) {
443-
SDL_LogWarn(SDL_LOG_CATEGORY_INPUT, "Couldn't read user calibration data: %d", res);
444-
} else if (calibration_data[0x10] == 0xb2 && calibration_data[0x11] == 0xa1) {
445-
ParseStickCalibration(&ctx->left_stick, &calibration_data[0x12]);
446-
}
442+
SDL_LogWarn(SDL_LOG_CATEGORY_INPUT, "Couldn't read user calibration data: %d", res);
443+
} else if (calibration_data[0] == 0xb2 && calibration_data[1] == 0xa1) {
444+
ParseStickCalibration(&ctx->left_stick, &calibration_data[2]);
447445
}
448446

449-
flash_read_command[12] = 0x80;
450-
res = SendBulkData(ctx, flash_read_command, sizeof(flash_read_command));
447+
res = ReadFlashBlock(ctx, 0x1FC080, calibration_data);
451448
if (res < 0) {
452-
SDL_LogWarn(SDL_LOG_CATEGORY_INPUT, "Couldn't request user calibration data: %d", res);
453-
} else {
454-
res = RecvBulkData(ctx, calibration_data, sizeof(calibration_data));
455-
if (res < 0) {
456-
SDL_LogWarn(SDL_LOG_CATEGORY_INPUT, "Couldn't read user calibration data: %d", res);
457-
} else if (calibration_data[0x10] == 0xb2 && calibration_data[0x11] == 0xa1) {
458-
ParseStickCalibration(&ctx->right_stick, &calibration_data[0x12]);
459-
}
449+
SDL_LogWarn(SDL_LOG_CATEGORY_INPUT, "Couldn't read user calibration data: %d", res);
450+
} else if (calibration_data[0] == 0xb2 && calibration_data[1] == 0xa1) {
451+
ParseStickCalibration(&ctx->right_stick, &calibration_data[2]);
460452
}
461453

462454
for (int i = 0; init_sequence[i]; i++) {

0 commit comments

Comments
 (0)