diff --git a/joycontrol/controller_state.py b/joycontrol/controller_state.py index dfd4a76..29722ca 100644 --- a/joycontrol/controller_state.py +++ b/joycontrol/controller_state.py @@ -187,7 +187,7 @@ class ButtonState: self._byte_1 = self._byte_2 = self._byte_3 = 0 -async def button_push(controller_state, *buttons, sec=0.1): +async def button_down(controller_state, *buttons): if not buttons: raise ValueError('No Buttons were given.') @@ -199,7 +199,13 @@ async def button_push(controller_state, *buttons, sec=0.1): # send report await controller_state.send() - await asyncio.sleep(sec) + + +async def button_up(controller_state, *buttons): + if not buttons: + raise ValueError('No Buttons were given.') + + button_state = controller_state.button_state for button in buttons: # release button @@ -209,6 +215,12 @@ async def button_push(controller_state, *buttons, sec=0.1): await controller_state.send() +async def button_push(controller_state, *buttons, sec=0.1): + await button_down(controller_state, *buttons) + await asyncio.sleep(sec) + await button_up(controller_state, *buttons) + + class _StickCalibration: def __init__(self, h_center, v_center, h_max_above_center, v_max_above_center, h_max_below_center, v_max_below_center): self.h_center = h_center diff --git a/run_controller_cli.py b/run_controller_cli.py index c90e506..504268c 100644 --- a/run_controller_cli.py +++ b/run_controller_cli.py @@ -10,7 +10,7 @@ from aioconsole import ainput from joycontrol import logging_default as log, utils from joycontrol.command_line_interface import ControllerCLI from joycontrol.controller import Controller -from joycontrol.controller_state import ControllerState, button_push +from joycontrol.controller_state import ControllerState, button_push, button_down, button_up from joycontrol.memory import FlashMemory from joycontrol.protocol import controller_protocol_factory from joycontrol.server import create_hid_server @@ -150,11 +150,7 @@ async def set_nfc(controller_state, file_path): async def mash_button(controller_state, button, interval): - # waits until controller is fully connected - await controller_state.connect() - - if button not in controller_state.button_state.get_available_buttons(): - raise ValueError(f'Button {button} does not exist on {controller_state.get_controller()}') + await ensure_valid_button(controller_state, button) user_input = asyncio.ensure_future( ainput(prompt=f'Pressing the {button} button every {interval} seconds... Press to stop.') @@ -168,6 +164,24 @@ async def mash_button(controller_state, button, interval): await user_input +async def hold_button(controller_state, button): + await ensure_valid_button(controller_state, button) + await button_down(controller_state, button) + + +async def release_button(controller_state, button): + await ensure_valid_button(controller_state, button) + await button_up(controller_state, button) + + +async def ensure_valid_button(controller_state, button): + # waits until controller is fully connected + await controller_state.connect() + + if button not in controller_state.button_state.get_available_buttons(): + raise ValueError(f'Button {button} does not exist on {controller_state.get_controller()}') + + async def _main(args): # parse the spi flash if args.spi_flash: @@ -220,6 +234,38 @@ async def _main(args): # add the script from above cli.add_command('mash', call_mash_button) + # Hold a button command + async def hold(*args): + """ + hold - Press and hold a specified button + + Usage: + hold