wacom: do not timeout on long drawing retrievals

It can easily take more than 5 seconds when reading the data. The current
timeout just cuts the connection while it is still reading.

We *could* just remove the timeout here as the device will gracefully
send us a 0xc8 opcode with a blank CRC if we shut the device down while
retrieving the data.

However, better being on the safe side and keep a timeout, but restart it
if we are still getting pen data during the timeout.

Fixes #91
This commit is contained in:
Benjamin Tissoires 2018-02-15 17:08:20 +01:00
parent a5cb084a31
commit 789c098e82
1 changed files with 18 additions and 2 deletions

View File

@ -478,6 +478,7 @@ class WacomProtocolBase(WacomProtocolLowLevelComm):
self._timestamp = 0 self._timestamp = 0
self.pen_data_buffer = [] self.pen_data_buffer = []
self._uhid_device = None self._uhid_device = None
self._last_pen_data_time = time.time() - 5 # initialize this in the past
device.connect_gatt_value(WACOM_CHRC_LIVE_PEN_DATA_UUID, device.connect_gatt_value(WACOM_CHRC_LIVE_PEN_DATA_UUID,
self._on_pen_data_changed) self._on_pen_data_changed)
@ -547,6 +548,7 @@ class WacomProtocolBase(WacomProtocolLowLevelComm):
def _on_pen_data_received(self, name, data): def _on_pen_data_received(self, name, data):
self.fw_logger.debug(f'RX Pen <-- {list2hex(data)}') self.fw_logger.debug(f'RX Pen <-- {list2hex(data)}')
self.pen_data_buffer.extend(data) self.pen_data_buffer.extend(data)
self._last_pen_data_time = time.time()
def check_connection(self): def check_connection(self):
args = [int(i) for i in binascii.unhexlify(self._uuid)] args = [int(i) for i in binascii.unhexlify(self._uuid)]
@ -680,8 +682,22 @@ class WacomProtocolBase(WacomProtocolLowLevelComm):
if data[0] != 0xbe: if data[0] != 0xbe:
raise WacomException(f'unexpected answer: {data[0]:02x}') raise WacomException(f'unexpected answer: {data[0]:02x}')
def wait_nordic_unless_pen_data(self, opcode, timeout=None):
data = None
while data is None:
try:
data = self.wait_nordic_data(opcode, timeout)
except WacomTimeoutException:
# timeout is a time in seconds here
if time.time() - self._last_pen_data_time < timeout:
# we timed out, but we are still fetching offline pen data
continue
raise
return data
def wait_for_end_read(self): def wait_for_end_read(self):
data = self.wait_nordic_data(0xc8, 5) data = self.wait_nordic_unless_pen_data(0xc8, 5)
if data[0] != 0xed: if data[0] != 0xed:
raise WacomException(f'unexpected answer: {data[0]:02x}') raise WacomException(f'unexpected answer: {data[0]:02x}')
data = self.wait_nordic_data(0xc9, 5) data = self.wait_nordic_data(0xc9, 5)
@ -971,7 +987,7 @@ class WacomProtocolSlate(WacomProtocolSpark):
logger.warning('no data, please make sure the LED is blue and the button is pressed to switch it back to green') logger.warning('no data, please make sure the LED is blue and the button is pressed to switch it back to green')
def wait_for_end_read(self): def wait_for_end_read(self):
data = self.wait_nordic_data(0xc8, 5) data = self.wait_nordic_unless_pen_data(0xc8, 5)
if data[0] != 0xed: if data[0] != 0xed:
raise WacomException(f'unexpected answer: {data[0]:02x}') raise WacomException(f'unexpected answer: {data[0]:02x}')
crc = data[1:] crc = data[1:]