Ejemplo 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:
        # prepare the the emulated controller
        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()

        joycon_server = JoyConRestfull()

        th = threading.Thread(target=joycon_server.run)
        th.start()

        # Create command line interface and add some extra commands
        cli = ControllerCLI(controller_state, queue)
        _register_commands_with_controller_state(controller_state, cli)
        cli.add_command(
            'amiibo',
            ControllerCLI.deprecated(
                'Command was removed - use "nfc" instead!'))

        # set default nfc content supplied by argument
        if args.nfc is not None:
            await cli.commands['nfc'](args.nfc)

        #asyncio.ensure_future(cli.run())
        #asyncio.ensure_future(app.run(port='5002'))
        # run the cli
        try:
            await cli.run()
        finally:
            logger.info('Stopping communication...')
            await transport.close()
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_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 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])

        # add the script from above
        cli.add_command('nfc', nfc)
        cli.add_command(
            'amiibo',
            ControllerCLI.deprecated(
                'Command is deprecated - use "nfc" instead!'))

        if args.nfc is not None:
            await nfc(args.nfc)
        eventToButton = {
            304: 'b',
            305: 'a',
            307: 'y',
            308: 'x',
            704: 'left',
            705: 'right',
            706: 'up',
            707: 'down',
            311: 'r',
            310: 'l',
            312: 'zl',
            313: 'zr',
            314: 'minus',
            315: 'plus',
            317: 'l_stick',
            318: 'r_stick',
        }

        device = evdev.InputDevice(
            "/dev/input/by-id/usb-045e_0291-event-joystick")
        caps = device.capabilities()
        try:
            t1 = time.perf_counter()
            cnt = 0
            async for event in device.async_read_loop():
                if event.type == ecodes.EV_SYN:
                    lasttask = asyncio.ensure_future(
                        sync_controller(controller_state))
                if event.type == ecodes.EV_ABS:
                    if event.code == 0:
                        controller_state.l_stick_state.set_h(event.value //
                                                             22 + 2048)
                    elif event.code == 1:
                        controller_state.l_stick_state.set_v(
                            -(event.value - 15) // 22 + 2047)
                    elif event.code == 3:
                        controller_state.r_stick_state.set_h(event.value //
                                                             22 + 2048)
                    elif event.code == 4:
                        controller_state.r_stick_state.set_v(
                            -(event.value - 15) // 22 + 2047)
                elif event.type == ecodes.EV_KEY:
                    if event.code in eventToButton:
                        buttonCode = eventToButton[event.code]
                        button_set_state(controller_state, buttonCode, event)
                        if event.code == 305:
                            if event.value == 1:
                                if controller_state.button_state.get_button(
                                        'zr'):
                                    button_set_state(controller_state, 'home',
                                                     event)
                            else:
                                button_set_state(controller_state, 'home',
                                                 event)
                        lasttask = asyncio.ensure_future(
                            sync_controller(controller_state))
                cnt = cnt + 1
                if cnt > 50:
                    cnt = 0
                    t2 = time.perf_counter()
                    deltaT = t2 - t1
                    freq = 50.00 / deltaT
                    str = " 50 event takes  " + format(
                        deltaT, '.2f') + " secs freq = " + format(freq, '.2f')
                    logger.info(str)
                    t1 = t2
        except:
            logger.info('unexpected')
            logger.info(sys.exc_info()[0])
        finally:
            logger.info('Stopping communication...')
            await transport.close()
Ejemplo n.º 3
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_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 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])

        # add the script from above
        cli.add_command('nfc', nfc)
        cli.add_command(
            'amiibo',
            ControllerCLI.deprecated(
                'Command is deprecated - use "nfc" instead!'))

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

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