feat: send amiibo report

This commit is contained in:
spacemeowx2
2020-04-23 23:07:21 +08:00
parent e90499393b
commit be8dce71a0
6 changed files with 387 additions and 37 deletions
+112 -33
View File
@@ -10,6 +10,8 @@ from joycontrol.controller_state import ControllerState
from joycontrol.memory import FlashMemory
from joycontrol.report import OutputReport, SubCommand, InputReport, OutputReportID
from joycontrol.transport import NotConnectedError
from joycontrol.mcu import Mcu, McuState, Action
from crc8 import crc8
logger = logging.getLogger(__name__)
@@ -39,6 +41,8 @@ class ControllerProtocol(BaseProtocol):
self._controller_state = ControllerState(self, controller, spi_flash=spi_flash)
self._controller_state_sender = None
self._mcu = Mcu()
# None = Just answer to sub commands
self._input_report_mode = None
@@ -89,6 +93,11 @@ class ControllerProtocol(BaseProtocol):
input_report.set_timer(self._input_report_timer)
self._input_report_timer = (self._input_report_timer + 1) % 0x100
if input_report.get_input_report_id() == 0x31:
self._mcu.set_nfc(self._controller_state._nfc_content)
self._mcu.update_nfc_report()
input_report.set_mcu(self._mcu)
await self.transport.write(input_report)
self._controller_state.sig_is_send.set()
@@ -120,15 +129,14 @@ class ControllerProtocol(BaseProtocol):
# TODO?
raise NotImplementedError()
async def input_report_mode_0x30(self):
async def input_report_mode_full(self):
"""
Continuously sends 0x30 input reports containing the controller state.
Continuously sends full input reports containing the controller state.
"""
if self.transport.is_reading():
raise ValueError('Transport must be paused in 0x30 input report mode')
raise ValueError('Transport must be paused in full input report mode')
input_report = InputReport()
input_report.set_input_report_id(0x30)
input_report.set_vibrator_input()
input_report.set_misc()
@@ -136,6 +144,7 @@ class ControllerProtocol(BaseProtocol):
try:
while True:
input_report.set_input_report_id(self._input_report_mode)
# TODO: improve timing
if self.controller == Controller.PRO_CONTROLLER:
# send state at 120Hz
@@ -159,6 +168,10 @@ class ControllerProtocol(BaseProtocol):
pass
elif output_report_id == OutputReportID.SUB_COMMAND:
reply_send = await self._reply_to_sub_command(report)
elif output_report_id == OutputReportID.REQUEST_MCU:
reply_send = await self._reply_to_mcu(report)
else:
logger.warning(f'Report unknown output report "{output_report_id}" - IGNORE')
except ValueError as v_err:
logger.warning(f'Report parsing error "{v_err}" - IGNORE')
except NotImplementedError as err:
@@ -207,6 +220,47 @@ class ControllerProtocol(BaseProtocol):
else:
logger.warning(f'Output report {output_report_id} not implemented - ignoring')
async def _reply_to_mcu(self, report):
sub_command = report.data[11]
sub_command_data = report.data[12:]
# logging.info(f'received output report - Request MCU sub command {sub_command}')
if self._mcu.get_action() == Action.READ_TAG or self._mcu.get_action() == Action.READ_TAG_2 or self._mcu.get_action() == Action.READ_FINISHED:
return
# Request mcu state
if sub_command == 0x01:
# input_report = InputReport()
# input_report.set_input_report_id(0x21)
# input_report.set_misc()
# input_report.set_ack(0xA0)
# input_report.reply_to_subcommand_id(0x21)
self._mcu.set_action(Action.REQUEST_STATUS)
# input_report.set_mcu(self._mcu)
# await self.write(input_report)
# Send Start tag discovery
elif sub_command == 0x02:
# 0: Cancel all, 4: StartWaitingReceive
if sub_command_data[0] == 0x04:
self._mcu.set_action(Action.START_TAG_DISCOVERY)
# 1: Start polling
elif sub_command_data[0] == 0x01:
self._mcu.set_action(Action.START_TAG_POLLING)
# 2: stop polling
elif sub_command_data[0] == 0x02:
self._mcu.set_action(Action.NON)
elif sub_command_data[0] == 0x06:
self._mcu.set_action(Action.READ_TAG)
else:
logging.info(f'Unknown sub_command_data arg {sub_command_data}')
else:
logging.info(f'Unknown MCU sub command {sub_command}')
async def _reply_to_sub_command(self, report):
# classify sub command
try:
@@ -317,34 +371,40 @@ class ControllerProtocol(BaseProtocol):
async def _command_set_input_report_mode(self, sub_command_data):
if sub_command_data[0] == 0x30:
logger.info('Setting input report mode to 0x30...')
input_report = InputReport()
input_report.set_input_report_id(0x21)
input_report.set_misc()
input_report.set_ack(0x80)
input_report.reply_to_subcommand_id(0x03)
await self.write(input_report)
# start sending 0x30 input reports
if self._input_report_mode != 0x30:
self._input_report_mode = 0x30
self.transport.pause_reading()
new_reader = asyncio.ensure_future(self.input_report_mode_0x30())
# We need to swap the reader in the future because this function was probably called by it
async def set_reader():
await self.transport.set_reader(new_reader)
self.transport.resume_reading()
asyncio.ensure_future(set_reader()).add_done_callback(
utils.create_error_check_callback()
)
pass
elif sub_command_data[0] == 0x31:
pass
else:
logger.error(f'input report mode {sub_command_data[0]} not implemented - ignoring request')
return
logger.info(f'Setting input report mode to {hex(sub_command_data[0])}...')
input_report = InputReport()
input_report.set_input_report_id(0x21)
input_report.set_misc()
input_report.set_ack(0x80)
input_report.reply_to_subcommand_id(0x03)
await self.write(input_report)
# start sending input reports
if self._input_report_mode is None:
self.transport.pause_reading()
new_reader = asyncio.ensure_future(self.input_report_mode_full())
# We need to swap the reader in the future because this function was probably called by it
async def set_reader():
await self.transport.set_reader(new_reader)
self.transport.resume_reading()
asyncio.ensure_future(set_reader()).add_done_callback(
utils.create_error_check_callback()
)
self._input_report_mode = sub_command_data[0]
async def _command_trigger_buttons_elapsed_time(self, sub_command_data):
input_report = InputReport()
@@ -392,10 +452,26 @@ class ControllerProtocol(BaseProtocol):
input_report.set_ack(0xA0)
input_report.reply_to_subcommand_id(SubCommand.SET_NFC_IR_MCU_CONFIG.value)
# TODO
data = [1, 0, 255, 0, 8, 0, 27, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 200]
self._mcu.update_status()
data = list(bytes(self._mcu)[0:34])
crc = crc8()
crc.update(bytes(data[:-1]))
checksum = crc.digest()
data[-1] = ord(checksum)
for i in range(len(data)):
input_report.data[16 + i] = data[i]
input_report.data[16+i] = data[i]
# Set MCU mode cmd
if sub_command_data[1] == 0:
if sub_command_data[2] == 0:
self._mcu.set_state(McuState.STAND_BY)
elif sub_command_data[2] == 4:
self._mcu.set_state(McuState.NFC)
else:
logger.info(f"unknown mcu state {sub_command_data[2]}")
else:
logger.info(f"unknown mcu config command {sub_command_data}")
await self.write(input_report)
@@ -408,10 +484,13 @@ class ControllerProtocol(BaseProtocol):
# 0x01 = Resume
input_report.set_ack(0x80)
input_report.reply_to_subcommand_id(SubCommand.SET_NFC_IR_MCU_STATE.value)
self._mcu.set_action(Action.NON)
self._mcu.set_state(McuState.STAND_BY)
elif sub_command_data[0] == 0x00:
# 0x00 = Suspend
input_report.set_ack(0x80)
input_report.reply_to_subcommand_id(SubCommand.SET_NFC_IR_MCU_STATE.value)
self._mcu.set_state(McuState.STAND_BY)
else:
raise NotImplementedError(f'Argument {sub_command_data[0]} of {SubCommand.SET_NFC_IR_MCU_STATE} '
f'not implemented.')