diff --git a/tuhi/protocol.py b/tuhi/protocol.py index ea31648..747ad1b 100644 --- a/tuhi/protocol.py +++ b/tuhi/protocol.py @@ -119,6 +119,7 @@ class Interactions(enum.Enum): AVAILABLE_FILES_COUNT = enum.auto() DOWNLOAD_OLDEST_FILE = enum.auto() DELETE_OLDEST_FILE = enum.auto() + WAIT_FOR_END_READ = enum.auto() REGISTER_PRESS_BUTTON = enum.auto() REGISTER_WAIT_FOR_BUTTON = enum.auto() REGISTER_COMPLETE = enum.auto() @@ -1134,6 +1135,62 @@ class MsgDownloadOldestFile(Msg): raise UnexpectedDataError(reply) +class MsgWaitForEndRead(Msg): + ''' + .. attribute:: crc + + The checksum provided for the (out of band) pen data. + ''' + interaction = Interactions.WAIT_FOR_END_READ + requires_request = False + opcode = 0x00 # unused + protocol = ProtocolVersion.ANY + + def __init__(self, *args, **kwargs): + super().__init__(timeout=5, *args, **kwargs) + + def _handle_reply(self, reply): + if reply.opcode == 0xc8: + if reply[0] != 0xed: + raise UnexpectedDataError(reply, 'Expected c8 ed') + pass # nothing to do here + elif reply.opcode == 0xc9: + self.crc = int(binascii.hexlify(bytes(reply)), 16) + else: + raise UnexpectedReply(reply) + + def execute(self): + # This is a double-reply , once c8, then c9 + super().execute() + super().execute() + return self + + +class MsgWaitForEndReadSlate(Msg): + ''' + .. attribute:: crc + + The checksum provided for the (out of band) pen data. + ''' + interaction = Interactions.WAIT_FOR_END_READ + requires_request = False + opcode = 0x00 # unused + protocol = ProtocolVersion.SLATE + + def __init__(self, *args, **kwargs): + super().__init__(timeout=5, *args, **kwargs) + + def _handle_reply(self, reply): + if reply.opcode == 0xc8: + if reply[0] != 0xed: + raise UnexpectedDataError(reply, 'Expected c8 ed') + crc = reply[1:] + crc.reverse() + self.crc = int(binascii.hexlify(bytes(crc)), 16) + else: + raise UnexpectedReply(reply) + + class MsgDeleteOldestFile(Msg): interaction = Interactions.DELETE_OLDEST_FILE opcode = 0xca diff --git a/tuhi/wacom.py b/tuhi/wacom.py index ee99132..80f72ab 100644 --- a/tuhi/wacom.py +++ b/tuhi/wacom.py @@ -633,17 +633,21 @@ class WacomProtocolBase(WacomProtocolLowLevelComm): return data - def wait_for_end_read(self): - data = self.wait_nordic_unless_pen_data(0xc8, 5) - if data[0] != 0xed: - raise WacomException(f'unexpected answer: {data[0]:02x}') - data = self.wait_nordic_data(0xc9, 5) - crc = data - crc = int(binascii.hexlify(bytes(crc)), 16) + def wait_for_end_read(self, timeout=5): + msg = None + while True: + try: + msg = self.p.execute(Interactions.WAIT_FOR_END_READ) + break + except WacomTimeoutException as e: + # if we're still reading pen data, try again + if time.time() - self._last_pen_data_time > timeout: + raise e + pen_data = self.pen_data_buffer self.pen_data_buffer = [] - if crc != binascii.crc32(bytes(pen_data)): - logger.error("CRCs don't match") + if msg.crc != binascii.crc32(bytes(pen_data)): + raise UnexpectedDataError(pen_data, message='CRCs do not match') return pen_data def retrieve_data(self): @@ -857,19 +861,6 @@ class WacomProtocolSlate(WacomProtocolSpark): else: raise e - def wait_for_end_read(self): - data = self.wait_nordic_unless_pen_data(0xc8, timeout=5) - if data[0] != 0xed: - raise WacomException(f'unexpected answer: {data[0]:02x}') - crc = data[1:] - crc.reverse() - crc = int(binascii.hexlify(bytes(crc)), 16) - pen_data = self.pen_data_buffer - self.pen_data_buffer = [] - if crc != binascii.crc32(bytes(pen_data)): - raise UnexpectedDataError(pen_data, message='CRCs do not match') - return pen_data - class WacomProtocolIntuosPro(WacomProtocolSlate): '''