Skip to content

Commit 796961a

Browse files
Resolve bug for calibration Nintendo Switch Pro Controller (#13260)
Resolves a bug which prevents the stored calibration data from loading, only allowing loading of factory-installed calibration data
1 parent 3a6f9e0 commit 796961a

File tree

1 file changed

+36
-22
lines changed

1 file changed

+36
-22
lines changed

src/joystick/hidapi/SDL_hidapi_switch.c

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -928,13 +928,14 @@ static bool SetIMUEnabled(SDL_DriverSwitch_Context *ctx, bool enabled)
928928

929929
static bool LoadStickCalibration(SDL_DriverSwitch_Context *ctx)
930930
{
931-
Uint8 *pLeftStickCal;
932-
Uint8 *pRightStickCal;
931+
Uint8 *pLeftStickCal = NULL;
932+
Uint8 *pRightStickCal = NULL;
933933
size_t stick, axis;
934934
SwitchSubcommandInputPacket_t *user_reply = NULL;
935935
SwitchSubcommandInputPacket_t *factory_reply = NULL;
936936
SwitchSPIOpData_t readUserParams;
937937
SwitchSPIOpData_t readFactoryParams;
938+
Uint8 userParamsReadSuccessCount = 0;
938939

939940
// Read User Calibration Info
940941
readUserParams.unAddress = k_unSPIStickUserCalibrationStartOffset;
@@ -947,33 +948,46 @@ static bool LoadStickCalibration(SDL_DriverSwitch_Context *ctx)
947948
readFactoryParams.unAddress = k_unSPIStickFactoryCalibrationStartOffset;
948949
readFactoryParams.ucLength = k_unSPIStickFactoryCalibrationLength;
949950

950-
const int MAX_ATTEMPTS = 3;
951-
for (int attempt = 0; ; ++attempt) {
952-
if (!WriteSubcommand(ctx, k_eSwitchSubcommandIDs_SPIFlashRead, (uint8_t *)&readFactoryParams, sizeof(readFactoryParams), &factory_reply)) {
953-
return false;
954-
}
955-
956-
if (factory_reply->stickFactoryCalibration.opData.unAddress == k_unSPIStickFactoryCalibrationStartOffset) {
957-
// We successfully read the calibration data
958-
break;
959-
}
960-
961-
if (attempt == MAX_ATTEMPTS) {
962-
return false;
963-
}
964-
}
965-
966951
// Automatically select the user calibration if magic bytes are set
967952
if (user_reply && user_reply->stickUserCalibration.rgucLeftMagic[0] == 0xB2 && user_reply->stickUserCalibration.rgucLeftMagic[1] == 0xA1) {
953+
userParamsReadSuccessCount += 1;
968954
pLeftStickCal = user_reply->stickUserCalibration.rgucLeftCalibration;
969-
} else {
970-
pLeftStickCal = factory_reply->stickFactoryCalibration.rgucLeftCalibration;
971955
}
972956

973957
if (user_reply && user_reply->stickUserCalibration.rgucRightMagic[0] == 0xB2 && user_reply->stickUserCalibration.rgucRightMagic[1] == 0xA1) {
958+
userParamsReadSuccessCount += 1;
974959
pRightStickCal = user_reply->stickUserCalibration.rgucRightCalibration;
975-
} else {
976-
pRightStickCal = factory_reply->stickFactoryCalibration.rgucRightCalibration;
960+
}
961+
962+
// Only read the factory calibration info if we failed to receive the correct magic bytes
963+
if (userParamsReadSuccessCount < 2) {
964+
// Read Factory Calibration Info
965+
readFactoryParams.unAddress = k_unSPIStickFactoryCalibrationStartOffset;
966+
readFactoryParams.ucLength = k_unSPIStickFactoryCalibrationLength;
967+
968+
const int MAX_ATTEMPTS = 3;
969+
for (int attempt = 0;; ++attempt) {
970+
if (!WriteSubcommand(ctx, k_eSwitchSubcommandIDs_SPIFlashRead, (uint8_t *)&readFactoryParams, sizeof(readFactoryParams), &factory_reply)) {
971+
return false;
972+
}
973+
974+
if (factory_reply->stickFactoryCalibration.opData.unAddress == k_unSPIStickFactoryCalibrationStartOffset) {
975+
// We successfully read the calibration data
976+
pLeftStickCal = factory_reply->stickFactoryCalibration.rgucLeftCalibration;
977+
pRightStickCal = factory_reply->stickFactoryCalibration.rgucRightCalibration;
978+
break;
979+
}
980+
981+
if (attempt == MAX_ATTEMPTS) {
982+
return false;
983+
}
984+
}
985+
}
986+
987+
// If we still don't have calibration data, return false
988+
if (pLeftStickCal == NULL || pRightStickCal == NULL)
989+
{
990+
return false;
977991
}
978992

979993
/* Stick calibration values are 12-bits each and are packed by bit

0 commit comments

Comments
 (0)