From 8c1e8f61c5b3395bee02f4ce17cac484258aa4b1 Mon Sep 17 00:00:00 2001 From: Andrey Pohilko Date: Thu, 14 Sep 2017 10:08:42 +0300 Subject: [PATCH] Working on tilt sensor --- README.md | 1 + pylgbst/peripherals.py | 9 ++++++++- test.py | 25 +++++++++++++------------ 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 81ffea3..e299969 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ for device in hub.devices: - Make it 2/3 compatible - Add travis unit tests and coverage - Give nice documentation examples, don't forget to mention logging +- make angled motors to be synchronous by default ## Links diff --git a/pylgbst/peripherals.py b/pylgbst/peripherals.py index 15960f8..9bc5dca 100644 --- a/pylgbst/peripherals.py +++ b/pylgbst/peripherals.py @@ -10,10 +10,15 @@ class Peripheral(object): """ def __init__(self, parent, port): + """ + :type parent: pylgbst.MoveHub + :type port: int + """ super(Peripheral, self).__init__() self.parent = parent self.port = port self.working = False + self._subscribers = [] def __repr__(self): return "%s on port %s" % (self.__class__.__name__, PORTS[self.port] if self.port in PORTS else 'N/A') @@ -117,8 +122,10 @@ class ColorDistanceSensor(Peripheral): class TiltSensor(Peripheral): def subscribe(self, callback): + params = b'\x00\x01\x00\x00\x00\x01' # full + # params = b'\x02\x01\x00\x00\x00\x01' # basic self._subscribe_on_port(params) - self._subscribers.append(callback) + self._subscribers.append(callback) # TODO: maybe join it into `_subscribe_on_port` class Button(Peripheral): diff --git a/test.py b/test.py index ddbd178..a8b00f5 100644 --- a/test.py +++ b/test.py @@ -3,9 +3,10 @@ import time import unittest from threading import Thread -from pylgbst import MoveHub, COLOR_RED, LED, EncodedMotor, PORT_AB, Peripheral +from pylgbst import MoveHub, COLOR_RED, LED, EncodedMotor, PORT_AB from pylgbst.comms import Connection, str2hex, hex2str -from pylgbst.constants import PORT_LED +from pylgbst.constants import PORT_LED, PORT_TILT_SENSOR +from pylgbst.peripherals import TiltSensor logging.basicConfig(level=logging.DEBUG) @@ -51,29 +52,29 @@ class ConnectionMock(Connection): class HubMock(MoveHub): + def __init__(self, connection=None): + super(HubMock, self).__init__(connection if connection else ConnectionMock()) + def _wait_for_devices(self): pass class GeneralTest(unittest.TestCase): def test_led(self): - conn = ConnectionMock() - conn.notifications.append((14, '1b0e00 0900 04 39 0227003738')) - hub = HubMock(conn) + hub = HubMock() led = LED(hub, PORT_LED) led.set_color(COLOR_RED) - self.assertEquals("0701813211510009", conn.writes[0][1]) + self.assertEquals("0801813211510009", hub.connection.writes[0][1]) def test_tilt_sensor(self): - conn = ConnectionMock() - conn.notifications.append((14, '1b0e000f00043a0128000000000100000001')) - hub = HubMock(conn) + hub = HubMock() + hub.tilt_sensor = TiltSensor(hub, PORT_TILT_SENSOR) def callback(): pass hub.tilt_sensor.subscribe(callback) - self.assertEquals("0701813211510009", conn.writes[1][1]) + self.assertEquals("0a01413a000100000001", hub.connection.writes[0][1]) def test_motor(self): conn = ConnectionMock() @@ -81,9 +82,9 @@ class GeneralTest(unittest.TestCase): hub = HubMock(conn) motor = EncodedMotor(hub, PORT_AB) motor.timed(1.5) - self.assertEquals("0c018139110adc056464647f03", conn.writes[0][1]) + self.assertEquals("0d018139110adc056464647f03", conn.writes[0][1]) motor.angled(90) - self.assertEquals("0e018139110c5a0000006464647f03", conn.writes[1][1]) + self.assertEquals("0f018139110c5a0000006464647f03", conn.writes[1][1]) def test_capabilities(self): conn = ConnectionMock()