Esempio n. 1
0
async def _main(args):
    # parse the spi flash
    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)

    with utils.get_output(path=args.log, default=None) as capture_file:
        factory = controller_protocol_factory(controller, spi_flash=spi_flash)
        ctl_psm, itr_psm = 17, 19
        transport, protocol = await create_hid_server(
            factory,
            reconnect_bt_addr=args.reconnect_bt_addr,
            ctl_psm=ctl_psm,
            itr_psm=itr_psm,
            capture_file=capture_file,
            device_id=args.device_id)

        controller_state = protocol.get_controller_state()

        # Create command line interface and add some extra commands
        cli = ControllerCLI(controller_state)
        '''
        Custom Commands
        '''
        async def _run_drop():
            await drop(controller_state)

        cli.add_command('drop', _run_drop)

        async def _run_buy():
            await buy(controller_state)

        cli.add_command('buy', _run_buy)

        # Wrap the script so we can pass the controller state. The doc string will be printed when calling 'help'
        async def _run_test_controller_buttons():
            """
            test_buttons - Navigates to the "Test Controller Buttons" menu and presses all buttons.
            """
            await test_controller_buttons(controller_state)

        # add the script from above
        cli.add_command('test_buttons', _run_test_controller_buttons)

        # Mash a button command
        async def call_mash_button(*args):
            """
            mash - Mash a specified button at a set interval

            Usage:
                mash <button> <interval>
            """
            if not len(args) == 2:
                raise ValueError(
                    '"mash_button" command requires a button and interval as arguments!'
                )

            button, interval = args
            await mash_button(controller_state, button, interval)

        # add the script from above
        cli.add_command('mash', call_mash_button)

        # Create Custom Command:
        #async def digwalls(*args):
        #    _set_stick(l, up, 1)
        #    call_mash_button(a)

        # Create amiibo command
        async def amiibo(*args):
            """
            amiibo - Sets nfc content

            Usage:
                amiibo <file_name>          Set controller state NFC content to file
                amiibo remove               Remove NFC content from controller state
            """
            if controller_state.get_controller() == Controller.JOYCON_L:
                raise ValueError('NFC content cannot be set for JOYCON_L')
            elif not args:
                raise ValueError(
                    '"amiibo" command requires file path to an nfc dump as argument!'
                )
            elif args[0] == 'remove':
                controller_state.set_nfc(None)
                print('Removed nfc content.')
            else:
                await set_amiibo(controller_state, args[0])

        # add the script from above
        cli.add_command('amiibo', amiibo)

        try:
            await cli.run()
        finally:
            logger.info('Stopping communication...')
            await transport.close()
Esempio n. 2
0
async def _main(args):
    # parse the spi flash
    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)

    with utils.get_output(path=args.log, default=None) as capture_file:
        factory = controller_protocol_factory(controller, spi_flash=spi_flash)
        ctl_psm, itr_psm = 17, 19
        transport, protocol = await create_hid_server(
            factory,
            reconnect_bt_addr=args.reconnect_bt_addr,
            ctl_psm=ctl_psm,
            itr_psm=itr_psm,
            capture_file=capture_file,
            device_id=args.device_id)

        controller_state = protocol.get_controller_state()

        # Create command line interface and add some extra commands
        cli = ControllerCLI(controller_state)

        # Wrap the script so we can pass the controller state. The doc string will be printed when calling 'help'
        async def _run_test_control():
            """
            test_control - test method that will be removed later
            """
            await test_control(controller_state)

        async def _run_keyboard_control():
            """
            keyboard - binds controls to keyboard. Keybinding:
            q=LEFT w=LstickUP e=UP r=ZL t=L y=R u=ZR i=RstickUP
            a=LstickLEFT s=LstickDOWN d=LstickRIGHT f=RIGHT g=capture h=home j=RstickLEFT k=RStickDOWN l=RstickRIGHT
            c=DOWN up=X down=B left=Y right=A
            plus= + minus= -
            """
            await keyboard_control(controller_state)

        async def _run_recording_control():
            """
            recording - binds controls to keyboard, and records input until recording stopped.
            saved recordings can be replayed using cmd >> recording_playback
            Keybinding:
            q=LEFT w=LstickUP e=UP r=ZL t=L y=R u=ZR i=RstickUP
            a=LstickLEFT s=LstickDOWN d=LstickRIGHT f=RIGHT g=capture h=home j=RstickLEFT k=RStickDOWN l=RstickRIGHT
            c=DOWN up=X down=B left=Y right=A
            plus= + minus= -
            """
            await record_keyboard(controller_state)

        async def _run_recording_playback():
            """
            playback - select a saved recording and replay it
            """
            await recording_playback(controller_state)

        async def _run_delete_recording():
            """
            delete_rec - select a saved recording and delete it
            """
            await delete_recording(controller_state)

        async def _run_test_controller_buttons():
            """
            test_buttons - Navigates to the "Test Controller Buttons" menu and presses all buttons.
            """
            await test_controller_buttons(controller_state)

        # Mash a button command
        async def call_mash_button(*args):
            """
            mash - Mash a specified button at a set interval
            Usage:
                mash <button> <interval>
            """
            if not len(args) == 2:
                raise ValueError(
                    '"mash_button" command requires a button and interval as arguments!'
                )

            button, interval = args
            await mash_button(controller_state, button, interval)

        # Create nfc command
        async def nfc(*args):
            """
            nfc - Sets nfc content

            Usage:
                nfc <file_name>          Set controller state NFC content to file
                nfc remove               Remove NFC content from controller state
            """
            if controller_state.get_controller() == Controller.JOYCON_L:
                raise ValueError('NFC content cannot be set for JOYCON_L')
            elif not args:
                raise ValueError(
                    '"nfc" command requires file path to an nfc dump as argument!'
                )
            elif args[0] == 'remove':
                controller_state.set_nfc(None)
                print('Removed nfc content.')
            else:
                await set_nfc(controller_state, args[0])

        cli.add_command('test_buttons', _run_test_controller_buttons)
        cli.add_command('keyboard', _run_keyboard_control)
        cli.add_command('recording', _run_recording_control)
        cli.add_command('playback', _run_recording_playback)
        cli.add_command('delete_rec', _run_delete_recording)
        cli.add_command('mash', call_mash_button)
        # add the script from above
        cli.add_command('nfc', nfc)

        if args.nfc is not None:
            await nfc(args.nfc)

        try:
            await cli.run()
        finally:
            logger.info('Stopping communication...')
            await transport.close()