Use asyncio instead of thread in pycon.joycon.py

This commit is contained in:
redphx 2022-04-30 10:10:45 +07:00
parent 3c32efd6cf
commit 9e0a8ec584

View file

@ -1,4 +1,4 @@
import threading import asyncio
import time import time
from typing import Optional from typing import Optional
@ -47,10 +47,7 @@ class JoyCon:
self._setup_sensors() self._setup_sensors()
# start talking with the joycon in a daemon thread # start talking with the joycon in a daemon thread
self._update_input_report_thread \ asyncio.run(self._update_input_report())
= threading.Thread(target=self._update_input_report)
self._update_input_report_thread.setDaemon(True)
self._update_input_report_thread.start()
def _open(self, vendor_id, product_id, serial): def _open(self, vendor_id, product_id, serial):
try: try:
@ -92,7 +89,7 @@ class JoyCon:
# TODO: handle subcmd when daemon is running # TODO: handle subcmd when daemon is running
self._write_output_report(b'\x01', subcommand, argument) self._write_output_report(b'\x01', subcommand, argument)
report = self._read_input_report() report = [0]
while report[0] != 0x21: # TODO, avoid this, await daemon instead while report[0] != 0x21: # TODO, avoid this, await daemon instead
report = self._read_input_report() report = self._read_input_report()
@ -114,12 +111,12 @@ class JoyCon:
raise IOError("Something else than the expected ACK was recieved!") raise IOError("Something else than the expected ACK was recieved!")
assert report[2:7] == argument, (report[2:5], argument) assert report[2:7] == argument, (report[2:5], argument)
return report[7:size+7] return report[7:size + 7]
def _update_input_report(self): # daemon thread async def _update_input_report(self): # daemon thread
try: try:
while self._joycon_device: while self._joycon_device:
report = self._read_input_report() report = [0]
# TODO, handle input reports of type 0x21 and 0x3f # TODO, handle input reports of type 0x21 and 0x3f
while report[0] != 0x30: while report[0] != 0x30:
report = self._read_input_report() report = self._read_input_report()