From 753d22d13db55a6b6814f14409b1d6fc90a87875 Mon Sep 17 00:00:00 2001 From: Andrey Pohilko Date: Thu, 14 Sep 2017 17:06:57 +0300 Subject: [PATCH] Fix tests --- pylgbst/comms.py | 2 +- pylgbst/constants.py | 2 +- pylgbst/peripherals.py | 17 ++++++++--------- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/pylgbst/comms.py b/pylgbst/comms.py index 0901c9d..ca10381 100644 --- a/pylgbst/comms.py +++ b/pylgbst/comms.py @@ -36,7 +36,7 @@ else: def str2hex(data): - return binascii.hexlify(data) + return binascii.hexlify(data).decode("utf8") def hex2str(data): diff --git a/pylgbst/constants.py b/pylgbst/constants.py index 84d361b..ff121b1 100644 --- a/pylgbst/constants.py +++ b/pylgbst/constants.py @@ -66,7 +66,7 @@ MSG_PORT_CMD_ERROR = 0x05 MSG_SET_PORT_VAL = 0x81 MSG_PORT_STATUS = 0x82 MSG_SENSOR_SUBSCRIBE = 0x41 -#MSG_SENSOR_UNSUBSCRIBE = 0x42 +MSG_SENSOR_SOMETHING = 0x42 MSG_SENSOR_DATA = 0x45 MSG_SENSOR_SUBSCRIBE_ACK = 0x47 diff --git a/pylgbst/peripherals.py b/pylgbst/peripherals.py index acb9d9b..e6b9551 100644 --- a/pylgbst/peripherals.py +++ b/pylgbst/peripherals.py @@ -37,10 +37,6 @@ class Peripheral(object): # FIXME: became obsolete self._write_to_hub(MSG_SET_PORT_VAL, value) - def _subscribe_on_port(self, params): - # FIXME: became obsolete - self._write_to_hub(MSG_SENSOR_SUBSCRIBE, params) - def started(self): self.working = True @@ -126,22 +122,25 @@ class EncodedMotor(Peripheral): class TiltSensor(Peripheral): - TRAILER = b'\x01\x00\x00\x00' + TRAILER = b'\x00\x00\x00' def __init__(self, parent, port): super(TiltSensor, self).__init__(parent, port) self.mode = None - def subscribe(self, callback, mode=TILT_SENSOR_MODE_BASIC): - # TODO: check input for valid mode + def subscribe(self, callback, mode=TILT_SENSOR_MODE_BASIC, threshold=1): self.mode = mode - self._subscribe_on_port(int2byte(self.mode) + self.TRAILER + int2byte(1)) + params = int2byte(self.mode) + params += int2byte(threshold) + params += self.TRAILER + params += int2byte(1) # enable + self._write_to_hub(MSG_SENSOR_SUBSCRIBE, params + self.TRAILER) self._subscribers.add(callback) def unsubscribe(self, callback): self._subscribers.remove(callback) if not self._subscribers: - self._subscribe_on_port(int2byte(self.mode) + self.TRAILER + int2byte(0)) + self._write_to_hub(MSG_SENSOR_SUBSCRIBE, int2byte(self.mode) + self.TRAILER + int2byte(0)) def handle_notification(self, data): if self.mode == TILT_SENSOR_MODE_BASIC: