From adf5c09ca27b45282ab5b792c80dfb27399643e3 Mon Sep 17 00:00:00 2001 From: Dmitry Fink Date: Mon, 14 Sep 2020 01:18:47 -0700 Subject: [PATCH] Handle failures on port out commands Port out commands can fail (e.x obstruction of the movement), and it will get the while executon stuck. The diff handles failures and reports errors to the caller while at it, its useful to know when the command fails. --- pylgbst/messages.py | 5 ++++- pylgbst/peripherals.py | 37 +++++++++++++++++++++++++++---------- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/pylgbst/messages.py b/pylgbst/messages.py index 733db90..9169df0 100644 --- a/pylgbst/messages.py +++ b/pylgbst/messages.py @@ -704,7 +704,7 @@ def bytes(self): def is_reply(self, msg): return isinstance(msg, MsgPortOutputFeedback) and msg.port == self.port \ - and (msg.is_completed() or self.is_buffered) + and not (msg.is_in_progress() or msg.is_busy()) class MsgPortOutputFeedback(UpstreamMsg): @@ -736,6 +736,9 @@ def is_discarded(self): def is_idle(self): return self.status & 0b1000 + def is_busy(self): + return self.status & 0b10000 + UPSTREAM_MSGS = ( MsgHubProperties, MsgHubAction, MsgHubAlert, MsgHubAttachedIO, MsgGenericError, diff --git a/pylgbst/peripherals.py b/pylgbst/peripherals.py index 2f352ec..5f8052b 100644 --- a/pylgbst/peripherals.py +++ b/pylgbst/peripherals.py @@ -101,7 +101,7 @@ def set_port_mode(self, mode, send_updates=None, update_delta=None): def _send_output(self, msg): assert isinstance(msg, MsgPortOutput) msg.is_buffered = self.is_buffered # TODO: support buffering - self.hub.send(msg) + return self.hub.send(msg) def get_sensor_data(self, mode): self.set_port_mode(mode) @@ -275,12 +275,15 @@ def _write_direct_mode(self, subcmd, params): msg = MsgPortOutput(self.port, MsgPortOutput.WRITE_DIRECT_MODE_DATA, params) self._send_output(msg) + def _is_successful(self, msg): + return not msg.is_discarded() + def _send_cmd(self, subcmd, params): if self.virtual_ports: subcmd += 1 # de-facto rule msg = MsgPortOutput(self.port, subcmd, params) - self._send_output(msg) + return self._send_output(msg) def start_power(self, power_primary=1.0, power_secondary=None): """ @@ -299,10 +302,12 @@ def start_power(self, power_primary=1.0, power_secondary=None): if self.virtual_ports: params += pack("