@@ -444,18 +444,18 @@ static void handle_command_response(Controller* controller, SwitchCommandRespons
444444{
445445 SwitchData * sdata = (SwitchData * ) controller -> additionalData ;
446446
447- DEBUG_PRINT ("subcmd respone %d \n" , resp -> command );
447+ DEBUG_PRINT ("subcmd respone 0x%x \n" , resp -> command );
448448
449449 if ((resp -> ack & 0x80 ) == 0 ) {
450- DEBUG_PRINT ("switch subcmd %d failed\n" , resp -> command );
450+ DEBUG_PRINT ("switch subcmd 0x%x failed\n" , resp -> command );
451451 // if we failed during one of the stages, just fall back to simple input
452452 controller -> isReady = 1 ;
453453 return ;
454454 }
455455
456456 if (resp -> command == SWITCH_COMMAND_REQUEST_DEVICE_INFO ) {
457457 sdata -> device = resp -> device_info .device_type ;
458- DEBUG_PRINT ("device type %d \n" , sdata -> device );
458+ DEBUG_PRINT ("device type 0x%x \n" , sdata -> device );
459459
460460 // Get the configuration again, now that we have the actual device type
461461 controller -> type = switchDeviceToControllerType (sdata -> device );
@@ -474,8 +474,8 @@ static void handle_command_response(Controller* controller, SwitchCommandRespons
474474 sdata -> device == SWITCH_DEVICE_JOYCON_RIGHT || /*sdata->device == SWITCH_DEVICE_TP_JOYCON_RIGHT ||*/
475475 sdata -> device == SWITCH_DEVICE_PRO || /*sdata->device == SWITCH_DEVICE_TP_PRO ||*/
476476 sdata -> device == SWITCH_DEVICE_N64 ) && switchConfigCalibrationEnabled (controller )) {
477- // Start by reading the left user calibration magic to check if user calibration exists
478- readSpiFlash (controller , SWITCH_LEFT_USER_CALIBRATION_MAGIC_ADDRESS , 2 );
477+ // Start by reading the user calibration
478+ readSpiFlash (controller , SWITCH_USER_CALIBRATION_ADDRESS , sizeof ( SwitchRawUserStickCalibration ) );
479479 } else {
480480 // Don't need to wait for calibration, controller is ready now
481481 controller -> isReady = 1 ;
@@ -484,42 +484,42 @@ static void handle_command_response(Controller* controller, SwitchCommandRespons
484484 uint32_t address = bswap32 (resp -> spi_flash_read .address );
485485
486486 switch (address ) {
487- case SWITCH_LEFT_USER_CALIBRATION_MAGIC_ADDRESS :
488- // Check for user calibration
489- if (resp -> spi_flash_read .data [0 ] == 0xb2 && resp -> spi_flash_read .data [1 ] == 0xa1 ) {
490- // Read user calibration
491- readSpiFlash (controller , SWITCH_LEFT_USER_CALIBRATION_ADDRESS , sizeof (SwitchRawStickCalibrationLeft ));
492- } else {
493- // Fall back to factory calibration
494- readSpiFlash (controller , SWITCH_LEFT_FACTORY_CALIBRATION_ADDRESS , sizeof (SwitchRawStickCalibrationLeft ));
487+ case SWITCH_USER_CALIBRATION_ADDRESS : {
488+ SwitchRawUserStickCalibration * calibration = (SwitchRawUserStickCalibration * ) resp -> spi_flash_read .data ;
489+
490+ if (calibration -> left_magic == SWITCH_USER_CALIBRATION_MAGIC ) {
491+ parseLeftRawStickCalibration (sdata , & calibration -> left_calibration );
492+ sdata -> has_left_calib = 1 ;
493+ }
494+ if (calibration -> right_magic == SWITCH_USER_CALIBRATION_MAGIC ) {
495+ parseRightRawStickCalibration (sdata , & calibration -> right_calibration );
496+ sdata -> has_right_calib = 1 ;
495497 }
496- break ;
497- case SWITCH_LEFT_FACTORY_CALIBRATION_ADDRESS :
498- case SWITCH_LEFT_USER_CALIBRATION_ADDRESS :
499- // parse the calibration data
500- parseLeftRawStickCalibration (sdata , (SwitchRawStickCalibrationLeft * ) resp -> spi_flash_read .data );
501498
502- // continue with reading the right calibration magic
503- readSpiFlash (controller , SWITCH_RIGHT_USER_CALIBRATION_MAGIC_ADDRESS , 2 );
504- break ;
505- case SWITCH_RIGHT_USER_CALIBRATION_MAGIC_ADDRESS :
506- // Check for user calibration
507- if (resp -> spi_flash_read .data [0 ] == 0xb2 && resp -> spi_flash_read .data [1 ] == 0xa1 ) {
508- // Read user calibration
509- readSpiFlash (controller , SWITCH_RIGHT_USER_CALIBRATION_ADDRESS , sizeof (SwitchRawStickCalibrationRight ));
499+ // Check if we have both calibrations already or if we need to read factory calibrations
500+ if (sdata -> has_left_calib && sdata -> has_right_calib ) {
501+ setInputReportMode (controller , SWITCH_INPUT_REPORT_ID );
510502 } else {
511- // Fall back to factory calibration
512- readSpiFlash (controller , SWITCH_RIGHT_FACTORY_CALIBRATION_ADDRESS , sizeof (SwitchRawStickCalibrationRight ));
503+ readSpiFlash (controller , SWITCH_FACTORY_CALIBRATION_ADDRESS , sizeof (SwitchRawFactoryStickCalibration ));
513504 }
514505 break ;
515- case SWITCH_RIGHT_FACTORY_CALIBRATION_ADDRESS :
516- case SWITCH_RIGHT_USER_CALIBRATION_ADDRESS :
517- // parse the calibration data
518- parseRightRawStickCalibration (sdata , (SwitchRawStickCalibrationRight * ) resp -> spi_flash_read .data );
506+ }
507+ case SWITCH_FACTORY_CALIBRATION_ADDRESS : {
508+ SwitchRawFactoryStickCalibration * calibration = (SwitchRawFactoryStickCalibration * ) resp -> spi_flash_read .data ;
509+
510+ if (!sdata -> has_left_calib ) {
511+ parseLeftRawStickCalibration (sdata , & calibration -> left_calibration );
512+ sdata -> has_left_calib = 1 ;
513+ }
514+ if (!sdata -> has_right_calib ) {
515+ parseRightRawStickCalibration (sdata , & calibration -> right_calibration );
516+ sdata -> has_right_calib = 1 ;
517+ }
519518
520519 // we can now enable full reports
521520 setInputReportMode (controller , SWITCH_INPUT_REPORT_ID );
522521 break ;
522+ }
523523 default :
524524 DEBUG_PRINT ("switch: unknown SPI read from %lx size %d\n" , address , resp -> spi_flash_read .size );
525525 break ;
0 commit comments