diff --git a/pylgbst/peripherals.py b/pylgbst/peripherals.py index 26b010d..f0e861f 100644 --- a/pylgbst/peripherals.py +++ b/pylgbst/peripherals.py @@ -1,9 +1,9 @@ import logging import time +import traceback from struct import pack, unpack from threading import Thread -# noinspection PyUnresolvedReferences from six.moves import queue from pylgbst.comms import str2hex @@ -66,11 +66,12 @@ class Peripheral(object): self._port_subscription_mode = mode self.started() self._port_subscribe(self._port_subscription_mode, granularity, True) - if callback: - self._subscribers.add(callback) self._wait_sync(async) # having async=True leads to stuck notifications + if callback: + self._subscribers.add(callback) + def unsubscribe(self, callback=None): if callback in self._subscribers: self._subscribers.remove(callback) @@ -98,6 +99,7 @@ class Peripheral(object): try: self.handle_port_data(data) except BaseException: + log.warning("%s", traceback.format_exc()) log.warning("Failed to handle port data by %s: %s", self, str2hex(data)) def _wait_sync(self, async): @@ -330,8 +332,8 @@ class ColorDistanceSensor(Peripheral): def handle_port_data(self, data): if self._port_subscription_mode == self.COLOR_DISTANCE_FLOAT: color = unpack("