diff --git a/joycontrol/controller_state.py b/joycontrol/controller_state.py index dcc0460..7eef5b7 100644 --- a/joycontrol/controller_state.py +++ b/joycontrol/controller_state.py @@ -27,6 +27,8 @@ class ControllerState: calibration = LeftStickCalibration.from_bytes(calibration_data) self.l_stick_state = StickState(calibration=calibration) + if calibration is not None: + self.l_stick_state.set_center() # create right stick state if controller in (Controller.PRO_CONTROLLER, Controller.JOYCON_R): @@ -39,6 +41,8 @@ class ControllerState: calibration = RightStickCalibration.from_bytes(calibration_data) self.r_stick_state = StickState(calibration=calibration) + if calibration is not None: + self.r_stick_state.set_center() self.sig_is_send = asyncio.Event() diff --git a/joycontrol/memory.py b/joycontrol/memory.py index 272cb50..50b7788 100644 --- a/joycontrol/memory.py +++ b/joycontrol/memory.py @@ -1,14 +1,28 @@ class FlashMemory: - def __init__(self, spi_flash_memory_data=None, size=0x80000): + def __init__(self, spi_flash_memory_data=None, default_stick_cal=False, size=0x80000): + """ + :param spi_flash_memory_data: data from a memory dump (can be created using dump_spi_flash.py). + :param default_stick_cal: If True, override stick calibration bytes with factory default + :param size of the memory dump, should be constant + """ if spi_flash_memory_data is None: - self.data = size * [0x00] - else: - if len(spi_flash_memory_data) != size: - raise ValueError(f'Given data size {len(spi_flash_memory_data)} does not match size {size}.') - if isinstance(spi_flash_memory_data, bytes): - spi_flash_memory_data = list(spi_flash_memory_data) - self.data = spi_flash_memory_data + spi_flash_memory_data = [0xFF] * size # Blank data is all 0xFF + default_stick_cal = True + + if len(spi_flash_memory_data) != size: + raise ValueError(f'Given data size {len(spi_flash_memory_data)} does not match size {size}.') + if isinstance(spi_flash_memory_data, bytes): + spi_flash_memory_data = list(spi_flash_memory_data) + + # set default controller stick calibration + if default_stick_cal: + # L-stick factory calibration + spi_flash_memory_data[0x603D:0x6046] = [0x00, 0x07, 0x70, 0x00, 0x08, 0x80, 0x00, 0x07, 0x70] + # R-stick factory calibration + spi_flash_memory_data[0x6046:0x604F] = [0x00, 0x08, 0x80, 0x00, 0x07, 0x70, 0x00, 0x07, 0x70] + + self.data = spi_flash_memory_data def __getitem__(self, item): return self.data[item] diff --git a/run_controller_cli.py b/run_controller_cli.py index 0f9365c..c129e1a 100644 --- a/run_controller_cli.py +++ b/run_controller_cli.py @@ -147,10 +147,12 @@ async def set_amiibo(controller_state, file_path): async def _main(args): # parse the spi flash - spi_flash = None if args.spi_flash: with open(args.spi_flash, 'rb') as spi_flash_file: spi_flash = FlashMemory(spi_flash_file.read()) + else: + # Create memory containing default controller stick calibration + spi_flash = FlashMemory() # Get controller name to emulate from arguments controller = Controller.from_arg(args.controller)