forked from mirror/joycontrol
cleanup register_commands_with_controller_state
This commit is contained in:
+50
-54
@@ -136,19 +136,6 @@ async def test_controller_buttons(controller_state: ControllerState):
|
|||||||
await button_push(controller_state, 'home')
|
await button_push(controller_state, 'home')
|
||||||
|
|
||||||
|
|
||||||
async def set_nfc(controller_state, file_path):
|
|
||||||
"""
|
|
||||||
Sets nfc content of the controller state to contents of the given file.
|
|
||||||
:param controller_state: Emulated controller state
|
|
||||||
:param file_path: Path to nfc dump file
|
|
||||||
"""
|
|
||||||
loop = asyncio.get_event_loop()
|
|
||||||
|
|
||||||
with open(file_path, 'rb') as nfc_file:
|
|
||||||
content = await loop.run_in_executor(None, nfc_file.read)
|
|
||||||
controller_state.set_nfc(content)
|
|
||||||
|
|
||||||
|
|
||||||
def ensure_valid_button(controller_state, *buttons):
|
def ensure_valid_button(controller_state, *buttons):
|
||||||
"""
|
"""
|
||||||
Raise ValueError if any of the given buttons os not part of the controller state.
|
Raise ValueError if any of the given buttons os not part of the controller state.
|
||||||
@@ -177,43 +164,23 @@ async def mash_button(controller_state, button, interval):
|
|||||||
await user_input
|
await user_input
|
||||||
|
|
||||||
|
|
||||||
async def _main(args):
|
def _register_commands_with_controller_state(controller_state, cli):
|
||||||
# parse the spi flash
|
"""
|
||||||
if args.spi_flash:
|
Commands registered here can use the given controller state.
|
||||||
with open(args.spi_flash, 'rb') as spi_flash_file:
|
The doc string of commands will be printed by the CLI when calling "help"
|
||||||
spi_flash = FlashMemory(spi_flash_file.read())
|
:param cli:
|
||||||
else:
|
:param controller_state:
|
||||||
# Create memory containing default controller stick calibration
|
"""
|
||||||
spi_flash = FlashMemory()
|
async def test_buttons():
|
||||||
|
|
||||||
# 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.
|
test_buttons - Navigates to the "Test Controller Buttons" menu and presses all buttons.
|
||||||
"""
|
"""
|
||||||
await test_controller_buttons(controller_state)
|
await test_controller_buttons(controller_state)
|
||||||
|
|
||||||
# add the script from above
|
cli.add_command(test_buttons.__name__, test_buttons)
|
||||||
cli.add_command('test_buttons', _run_test_controller_buttons)
|
|
||||||
|
|
||||||
# Mash a button command
|
# Mash a button command
|
||||||
async def call_mash_button(*args):
|
async def mash(*args):
|
||||||
"""
|
"""
|
||||||
mash - Mash a specified button at a set interval
|
mash - Mash a specified button at a set interval
|
||||||
|
|
||||||
@@ -226,8 +193,7 @@ async def _main(args):
|
|||||||
button, interval = args
|
button, interval = args
|
||||||
await mash_button(controller_state, button, interval)
|
await mash_button(controller_state, button, interval)
|
||||||
|
|
||||||
# add the script from above
|
cli.add_command(mash.__name__, mash)
|
||||||
cli.add_command('mash', call_mash_button)
|
|
||||||
|
|
||||||
# Hold a button command
|
# Hold a button command
|
||||||
async def hold(*args):
|
async def hold(*args):
|
||||||
@@ -249,8 +215,7 @@ async def _main(args):
|
|||||||
await controller_state.connect()
|
await controller_state.connect()
|
||||||
await button_press(controller_state, *args)
|
await button_press(controller_state, *args)
|
||||||
|
|
||||||
# add the script from above
|
cli.add_command(hold.__name__, hold)
|
||||||
cli.add_command('hold', hold)
|
|
||||||
|
|
||||||
# Release a button command
|
# Release a button command
|
||||||
async def release(*args):
|
async def release(*args):
|
||||||
@@ -272,8 +237,7 @@ async def _main(args):
|
|||||||
await controller_state.connect()
|
await controller_state.connect()
|
||||||
await button_release(controller_state, *args)
|
await button_release(controller_state, *args)
|
||||||
|
|
||||||
# add the script from above
|
cli.add_command(release.__name__, release)
|
||||||
cli.add_command('release', release)
|
|
||||||
|
|
||||||
# Create nfc command
|
# Create nfc command
|
||||||
async def nfc(*args):
|
async def nfc(*args):
|
||||||
@@ -292,15 +256,47 @@ async def _main(args):
|
|||||||
controller_state.set_nfc(None)
|
controller_state.set_nfc(None)
|
||||||
print('Removed nfc content.')
|
print('Removed nfc content.')
|
||||||
else:
|
else:
|
||||||
await set_nfc(controller_state, args[0])
|
_loop = asyncio.get_event_loop()
|
||||||
|
with open(args[0], 'rb') as nfc_file:
|
||||||
|
content = await _loop.run_in_executor(None, nfc_file.read)
|
||||||
|
controller_state.set_nfc(content)
|
||||||
|
|
||||||
# add the script from above
|
cli.add_command(nfc.__name__, nfc)
|
||||||
cli.add_command('nfc', nfc)
|
|
||||||
cli.add_command('amiibo', ControllerCLI.deprecated('Command is deprecated - use "nfc" instead!'))
|
|
||||||
|
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
# Create command line interface and add some extra commands
|
||||||
|
cli = ControllerCLI(controller_state)
|
||||||
|
_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:
|
if args.nfc is not None:
|
||||||
await nfc(args.nfc)
|
await cli.commands['nfc'](args.nfc)
|
||||||
|
|
||||||
|
# run the cli
|
||||||
try:
|
try:
|
||||||
await cli.run()
|
await cli.run()
|
||||||
finally:
|
finally:
|
||||||
|
|||||||
Reference in New Issue
Block a user