home button mash test

This commit is contained in:
Robert Martin
2020-01-29 20:51:59 +09:00
parent 07f539743d
commit 1fd9e2b655
4 changed files with 89 additions and 21 deletions
+30
View File
@@ -0,0 +1,30 @@
def get_bit(value, n):
return (value >> n & 1) != 0
def flip_bit(value, n):
return value ^ (1 << n)
class Buttons:
"""
Utility class to set buttons in the input report
TODO: More Buttons
"""
def __init__(self):
self.left = 0
self.middle = 0
self.right = 0
def home(self):
self.middle = flip_bit(self.middle, 4)
def home_is_set(self):
return get_bit(self.middle, 4)
def to_list(self):
return [self.left, self.middle, self.right]
def clear(self):
self.left = self.middle = self.right = 0
+12 -2
View File
@@ -47,7 +47,7 @@ class ControllerProtocol(BaseProtocol):
return
# classify sub command
sub_command = report.get_sub_command()
sc_byte, sub_command = report.get_sub_command()
logging.info(f'received output report - {sub_command}')
if sub_command == SubCommand.REQUEST_DEVICE_INFO:
await self._command_request_device_info(report)
@@ -65,7 +65,7 @@ class ControllerProtocol(BaseProtocol):
await self._command_trigger_buttons_elapsed_time(report)
elif sub_command == SubCommand.NOT_IMPLEMENTED:
logger.error(f'Sub command not implemented - ignoring')
logger.error(f'Sub command 0x{sc_byte:02x} not implemented - ignoring')
async def _command_request_device_info(self, output_report):
address = self.transport.get_extra_info('sockname')
@@ -119,3 +119,13 @@ class ControllerProtocol(BaseProtocol):
input_report.sub_0x04_trigger_buttons_elapsed_time()
asyncio.ensure_future(self.transport.write(input_report))
async def _enable_6axis_sensor(self, output_report):
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(0x40)
asyncio.ensure_future(self.transport.write(input_report))
+24 -19
View File
@@ -4,6 +4,10 @@ from controller import Controller
class InputReport:
"""
Class to create Input Reports. Reference:
https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering/blob/master/bluetooth_hid_notes.md
"""
def __init__(self):
self.data = [0x00] * 50
# all input reports are prepended with 0xA1
@@ -11,13 +15,14 @@ class InputReport:
def set_input_report_id(self, _id):
"""
:param _id: e.g. 0x21 Standard input reports used for subcommand replies, etc... (TODO)
:param _id: e.g. 0x21 Standard input reports used for sub command replies
etc... (TODO)
"""
self.data[1] = _id
def set_timer(self, timer):
"""
Input report timer, usually set by the transport
Input report timer (0x00-0xFF), usually set by the transport
"""
self.data[2] = timer % 256
@@ -25,13 +30,6 @@ class InputReport:
# battery level + connection info
self.data[3] = 0x8E
def set_ack(self, ack):
"""
ACK byte for subcmd reply
TODO
"""
self.data[14] = ack
def set_button_status(self):
"""
TODO
@@ -56,6 +54,13 @@ class InputReport:
"""
self.data[13] = 0x80
def set_ack(self, ack):
"""
ACK byte for subcmd reply
TODO
"""
self.data[14] = ack
def sub_0x02_device_info(self, mac, fm_version=(0x03, 0x48), controller=Controller.JOYCON_L):
"""
Sub command 0x02 request device info response.
@@ -81,22 +86,21 @@ class InputReport:
self.data[offset + 10] = 0x01
self.data[offset + 11] = 0x01
def reply_to_subcommand_id(self, id_):
self.data[15] = id_
def sub_0x08_shipment(self):
# reply to sub command ID
self.data[15] = 0x08
self.reply_to_subcommand_id(0x08)
def sub_0x10_spi_flash_read(self, output_report):
# reply to sub command ID
self.data[15] = 0x10
self.reply_to_subcommand_id(0x10)
self.data[16:18] = output_report.data[12:14]
def sub_0x03_set_input_report_mode(self):
# reply to sub command ID
self.data[15] = 0x03
self.reply_to_subcommand_id(0x03)
def sub_0x04_trigger_buttons_elapsed_time(self):
# reply to sub command ID
self.data[15] = 0x04
self.reply_to_subcommand_id(0x04)
# TODO
blub = [0x00, 0xCC, 0x00, 0xEE, 0x00, 0xFF]
@@ -112,6 +116,7 @@ class SubCommand(Enum):
TRIGGER_BUTTONS_ELAPSED_TIME = 0x04
SET_SHIPMENT_STATE = 0x08
SPI_FLASH_READ = 0x10
ENABLE_6AXIS_SENSOR = 0x40
NOT_IMPLEMENTED = 0xFF
@@ -123,9 +128,9 @@ class OutputReport:
def get_sub_command(self):
try:
return SubCommand(self.data[11])
return self.data[11], SubCommand(self.data[11])
except ValueError:
return SubCommand.NOT_IMPLEMENTED
return self.data[11], SubCommand.NOT_IMPLEMENTED
def __bytes__(self):
return bytes(self.data)
+23
View File
@@ -5,6 +5,7 @@ import socket
import logging_default as log
import utils
from buttons import Buttons
from device import HidDevice
from protocol import controller_protocol_factory, Controller
from report import InputReport
@@ -63,6 +64,24 @@ async def send_empty_input_reports(transport):
await asyncio.sleep(1)
async def mash_home_button(transport):
report = InputReport()
report.set_input_report_id(0x21)
report.set_misc()
buttons = Buttons()
for _ in range(30):
buttons.home()
report.data[4:7] = buttons.to_list()
await transport.write(report)
await asyncio.sleep(0.1)
buttons.home()
report.data[4:7] = buttons.to_list()
await transport.write(report)
await asyncio.sleep(.3)
async def main():
transport, protocol = await create_hid_server(controller_protocol_factory(Controller.PRO_CONTROLLER), 17, 19)
@@ -75,6 +94,10 @@ async def main():
except asyncio.CancelledError:
pass
await asyncio.sleep(5)
await mash_home_button(transport)
# stop communication after some time
await asyncio.sleep(60)
logger.info('Stopping communication...')