cleanup register_commands_with_controller_state

This commit is contained in:
Robert Martin
2020-08-15 13:11:16 +02:00
parent ee22b8d013
commit bf2e7e52fb
+50 -54
View File
@@ -136,19 +136,6 @@ async def test_controller_buttons(controller_state: ControllerState):
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):
"""
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
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():
def _register_commands_with_controller_state(controller_state, cli):
"""
Commands registered here can use the given controller state.
The doc string of commands will be printed by the CLI when calling "help"
:param cli:
:param controller_state:
"""
async def test_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)
cli.add_command(test_buttons.__name__, test_buttons)
# Mash a button command
async def call_mash_button(*args):
async def mash(*args):
"""
mash - Mash a specified button at a set interval
@@ -226,8 +193,7 @@ async def _main(args):
button, interval = args
await mash_button(controller_state, button, interval)
# add the script from above
cli.add_command('mash', call_mash_button)
cli.add_command(mash.__name__, mash)
# Hold a button command
async def hold(*args):
@@ -249,8 +215,7 @@ async def _main(args):
await controller_state.connect()
await button_press(controller_state, *args)
# add the script from above
cli.add_command('hold', hold)
cli.add_command(hold.__name__, hold)
# Release a button command
async def release(*args):
@@ -272,8 +237,7 @@ async def _main(args):
await controller_state.connect()
await button_release(controller_state, *args)
# add the script from above
cli.add_command('release', release)
cli.add_command(release.__name__, release)
# Create nfc command
async def nfc(*args):
@@ -292,15 +256,47 @@ async def _main(args):
controller_state.set_nfc(None)
print('Removed nfc content.')
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', nfc)
cli.add_command('amiibo', ControllerCLI.deprecated('Command is deprecated - use "nfc" instead!'))
cli.add_command(nfc.__name__, nfc)
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:
await nfc(args.nfc)
await cli.commands['nfc'](args.nfc)
# run the cli
try:
await cli.run()
finally: