diff --git a/tuhi/protocol.py b/tuhi/protocol.py index e6f4732..1a9cc61 100644 --- a/tuhi/protocol.py +++ b/tuhi/protocol.py @@ -72,6 +72,7 @@ class Interactions(enum.Enum): START_READING = enum.auto() ACK_TRANSACTION = enum.auto() REGISTER_PRESS_BUTTON = enum.auto() + REGISTER_WAIT_FOR_BUTTON = enum.auto() REGISTER_COMPLETE = enum.auto() UNKNOWN_B1 = enum.auto() @@ -996,29 +997,76 @@ class MsgRegisterCompleteSlate(Msg): class MsgRegisterPressButtonSpark(Msg): interaction = Interactions.REGISTER_PRESS_BUTTON opcode = 0xe3 - protocol = ProtocolVersion.SLATE + protocol = ProtocolVersion.ANY + # Does not require a reply, the reply is sent in response to the + # physical button press. + requires_reply = False - # FIXME: this is almost certainly incomplete - def _handle_reply(self, reply): - if reply.opcode != 0xb3: - raise UnexpectedReply(reply) + # uuid is unused, just there so it's compatible with the slate message + def __init__(self, uuid=None, *args, **kwargs): + super().__init__(*args, **kwargs) + self.args = [0x01] + self.uuid = uuid -class MsgRegisterPressButtonSlate(Msg): +class MsgRegisterPressButtonSlateOrIntuosPro(Msg): interaction = Interactions.REGISTER_PRESS_BUTTON opcode = 0xe7 protocol = ProtocolVersion.SLATE + # Does not require a reply, the reply is sent in response to the + # physical button press. + requires_reply = False + + def __init__(self, uuid, *args, **kwargs): + super().__init__(*args, **kwargs) + self.uuid = uuid + self.args = [int(i) for i in binascii.unhexlify(uuid)] + + +class MsgRegisterWaitForButtonSpark(Msg): + ''' + .. attribute:: protocol_version + + The protocol version used by this device, according to this message. + + ''' + interaction = Interactions.REGISTER_WAIT_FOR_BUTTON + requires_request = False + opcode = 0x00 # unused + protocol = ProtocolVersion.ANY + + def __init__(self, *args, **kwargs): + kwargs['timeout'] = 10 + super().__init__(*args, **kwargs) + self.protocol_version = ProtocolVersion.ANY - # FIXME: this is almost certainly incomplete def _handle_reply(self, reply): if reply.opcode != 0xe4: raise UnexpectedReply(reply) + self.protocol_version = ProtocolVersion.SPARK -class MsgRegisterPressButtonIntuosPro(Msg): - interaction = Interactions.REGISTER_PRESS_BUTTON - opcode = 0xe7 +class MsgRegisterWaitForButtonSlateOrIntuosPro(Msg): + ''' + .. attribute:: protocol_version + + The protocol version used by this device, according to this message. + + ''' + interaction = Interactions.REGISTER_WAIT_FOR_BUTTON + requires_request = False + opcode = 0x00 # unused + protocol = ProtocolVersion.SLATE + + def __init__(self, *args, **kwargs): + kwargs['timeout'] = 10 + super().__init__(*args, **kwargs) + self.protocol_version = ProtocolVersion.ANY def _handle_reply(self, reply): - if reply.opcode != 0x53: + if reply.opcode == 0xe4: + self.protocol_version = ProtocolVersion.SLATE + elif reply.opcode == 0x53: + self.protocol_version = ProtocolVersion.INTUOS_PRO + else: raise UnexpectedReply(reply) diff --git a/tuhi/wacom.py b/tuhi/wacom.py index 65153b7..2b70467 100644 --- a/tuhi/wacom.py +++ b/tuhi/wacom.py @@ -564,41 +564,41 @@ class WacomRegisterHelper(WacomProtocolLowLevelComm): def register_device(self, uuid): protocol = Protocol.UNKNOWN - args = [int(i) for i in binascii.unhexlify(uuid)] if self.is_spark(self.device): + self.p = tuhi.protocol.Protocol(ProtocolVersion.SPARK, self.nordic_data_exchange) # The spark replies with b3 01 01 when in pairing mode # Usually that triggers a WacomWrongModeException but here it's # expected try: - self.send_nordic_command_sync(command=0xe6, arguments=args) - except WacomWrongModeException: + self.p.execute(Interactions.CONNECT, uuid) + except tuhi.protocol.DeviceError: # this is expected pass # The "press button now command" on the spark - self.send_nordic_command(command=0xe3, - arguments=[0x01]) - protocol = Protocol.SPARK + self.p.execute(Interactions.REGISTER_PRESS_BUTTON) else: - # Slate requires a button press in response to e7 directly - self.send_nordic_command(command=0xe7, arguments=args) + # Default to Slate for now, it will handle the IntuosPro too + self.p = tuhi.protocol.Protocol(ProtocolVersion.SLATE, self.nordic_data_exchange) + self.p.execute(Interactions.REGISTER_PRESS_BUTTON, uuid) logger.info('Press the button now to confirm') self.emit('button-press-required') # Wait for the button confirmation event, or any error - data = self.wait_nordic_data([0xe4, 0xb3, 0x53], 10) + protocol_version = self.p.execute(Interactions.REGISTER_WAIT_FOR_BUTTON).protocol_version - if protocol == Protocol.UNKNOWN: - if data.opcode == 0xe4: - protocol = Protocol.SLATE - elif data.opcode == 0x53: - protocol = Protocol.INTUOS_PRO - else: - raise WacomException(f'unexpected opcode to register reply: {data.opcode:02x}') + if protocol_version == ProtocolVersion.ANY: + raise WacomException(f'Unknown protocol version: {protocol_version}') - return protocol + pvmap = { + ProtocolVersion.SPARK: Protocol.SPARK, + ProtocolVersion.SLATE: Protocol.SLATE, + ProtocolVersion.INTUOS_PRO: Protocol.INTUOS_PRO, + } + + return pvmap[protocol_version] class WacomProtocolBase(WacomProtocolLowLevelComm):