From c55d796683aacc800ea70b80392a4250d64cc407 Mon Sep 17 00:00:00 2001 From: Andrey Pohilko Date: Thu, 14 Sep 2017 08:52:22 +0300 Subject: [PATCH] refactoring --- README.md | 7 ++++--- pylgbst/__init__.py | 22 +++++----------------- pylgbst/comms.py | 2 +- pylgbst/constants.py | 2 ++ pylgbst/peripherals.py | 26 +++++++++++++++++++++++--- 5 files changed, 35 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index bb44d55..81ffea3 100644 --- a/README.md +++ b/README.md @@ -17,10 +17,11 @@ Best way to start is to look at [demo.py](demo.py) file, and run it. ```python from pylgbst import MoveHub -hub=MoveHub() -print (hub.get_name()) +hub = MoveHub() +print(hub.get_name()) + for device in hub.devices: - print (device) + print(device) ``` ## Roadmap diff --git a/pylgbst/__init__.py b/pylgbst/__init__.py index 153fc7d..307b683 100644 --- a/pylgbst/__init__.py +++ b/pylgbst/__init__.py @@ -26,13 +26,14 @@ class MoveHub(object): self.motor_AB = None self.tilt_sensor = None self.color_distance_sensor = None - # self.button + self.external_motor = None + self.button = Button(self) # enables notifications reading self.connection.set_notify_handler(self._notify) self.connection.write(ENABLE_NOTIFICATIONS_HANDLE, ENABLE_NOTIFICATIONS_VALUE) - while len(self.devices): + while None in (self.led, self.motor_A, self.motor_B, self.motor_AB, self.tilt_sensor): log.debug("Waiting to be notified about devices...") time.sleep(0.1) @@ -90,6 +91,7 @@ class MoveHub(object): self.devices[port] = EncodedMotor(self, port) elif dev_type == TYPE_IMOTOR: self.devices[port] = EncodedMotor(self, port) + self.external_motor = self.devices[port] elif dev_type == TYPE_DISTANCE_COLOR_SENSOR: self.devices[port] = ColorDistanceSensor(self, port) self.color_distance_sensor = self.devices[port] @@ -99,6 +101,7 @@ class MoveHub(object): self.devices[port] = TiltSensor(self, port) else: log.warning("Unhandled peripheral type 0x%x on port 0x%x", dev_type, port) + self.devices[port] = Peripheral(self, port) if port == PORT_A: self.motor_A = self.devices[port] @@ -116,18 +119,3 @@ class MoveHub(object): self.tilt_sensor = self.devices[port] else: log.warning("Unhandled port: %s", PORTS[port]) - - -LISTEN_COLOR_SENSOR_ON_C = b'\x0a\x00 \x41\x01 \x08\x01\x00\x00\x00\x01' -LISTEN_COLOR_SENSOR_ON_D = b'\x0a\x00 \x41\x02 \x08\x01\x00\x00\x00\x01' - -LISTEN_DIST_SENSOR_ON_C = b'\x0a\x00 \x41\x01 \x08\x01\x00\x00\x00\x01' -LISTEN_DIST_SENSOR_ON_D = b'\x0a\x00 \x41\x02 \x08\x01\x00\x00\x00\x01' - -LISTEN_ENCODER_ON_A = b'\x0a\x00 \x41\x37 \x02\x01\x00\x00\x00\x01' -LISTEN_ENCODER_ON_B = b'\x0a\x00 \x41\x38 \x02\x01\x00\x00\x00\x01' -LISTEN_ENCODER_ON_C = b'\x0a\x00 \x41\x01 \x02\x01\x00\x00\x00\x01' -LISTEN_ENCODER_ON_D = b'\x0a\x00 \x41\x02 \x02\x01\x00\x00\x00\x01' - -LISTEN_TILT_BASIC = b'\x0a\x00 \x41\x3a \x02\x01\x00\x00\x00\x01' -LISTEN_TILT_FULL = b'\x0a\x00 \x41\x3a \x00\x01\x00\x00\x00\x01' diff --git a/pylgbst/comms.py b/pylgbst/comms.py index d236516..5e05b6a 100644 --- a/pylgbst/comms.py +++ b/pylgbst/comms.py @@ -95,7 +95,6 @@ class BLEConnection(Connection): self._get_requester(address, bt_iface_name) break - log.info("Device declares itself as: %s", self.read(DEVICE_NAME)) return self def _get_requester(self, address, bt_iface_name): @@ -109,6 +108,7 @@ class BLEConnection(Connection): raise RuntimeError("No requester available") def read(self, handle): + # FIXME: repeating reads hang... log.debug("Reading from: %s", handle) data = self.requester.read_by_handle(handle) log.debug("Result: %s", data) diff --git a/pylgbst/constants.py b/pylgbst/constants.py index 3528212..dcdd501 100644 --- a/pylgbst/constants.py +++ b/pylgbst/constants.py @@ -7,6 +7,8 @@ MOVE_HUB_HARDWARE_UUID = '00001624-1212-efde-1623-785feabcd123' ENABLE_NOTIFICATIONS_HANDLE = 0x000f ENABLE_NOTIFICATIONS_VALUE = b'\x01\x00' +PACKET_VER = b'\x01' + # COLORS COLOR_OFF = 0x00 COLOR_PINK = 0x01 diff --git a/pylgbst/peripherals.py b/pylgbst/peripherals.py index 84a0ab5..a0c2a58 100644 --- a/pylgbst/peripherals.py +++ b/pylgbst/peripherals.py @@ -8,7 +8,6 @@ class Peripheral(object): """ :type parent: MoveHub """ - PACKET_VER = b'\x01' def __init__(self, parent, port): super(Peripheral, self).__init__() @@ -17,7 +16,7 @@ class Peripheral(object): self.working = False def _set_port_val(self, value): - cmd = self.PACKET_VER + chr(MSG_SET_PORT_VAL) + chr(self.port) + cmd = PACKET_VER + chr(MSG_SET_PORT_VAL) + chr(self.port) cmd += value self.parent.connection.write(MOVE_HUB_HARDWARE_HANDLE, chr(len(cmd)) + cmd) # should we +1 cmd len here? @@ -29,7 +28,7 @@ class Peripheral(object): self.working = False def __repr__(self): - return "%s on port %s" % (self.__class__.__name__, PORTS[self.port]) + return "%s on port %s" % (self.__class__.__name__, PORTS[self.port] if self.port in PORTS else 'N/A') class LED(Peripheral): @@ -111,3 +110,24 @@ class ColorDistanceSensor(Peripheral): class TiltSensor(Peripheral): pass + + +class Button(Peripheral): + def __init__(self, parent, port): + del port + super(Button, self).__init__(parent, None) + + +LISTEN_COLOR_SENSOR_ON_C = b' \x0a\x00 \x41\x01 \x08\x01\x00\x00\x00\x01' +LISTEN_COLOR_SENSOR_ON_D = b' \x0a\x00 \x41\x02 \x08\x01\x00\x00\x00\x01' + +LISTEN_DIST_SENSOR_ON_C = b' \x0a\x00 \x41\x01 \x08\x01\x00\x00\x00\x01' +LISTEN_DIST_SENSOR_ON_D = b' \x0a\x00 \x41\x02 \x08\x01\x00\x00\x00\x01' + +LISTEN_ENCODER_ON_A = b' \x0a\x00 \x41\x37 \x02\x01\x00\x00\x00\x01' +LISTEN_ENCODER_ON_B = b' \x0a\x00 \x41\x38 \x02\x01\x00\x00\x00\x01' +LISTEN_ENCODER_ON_C = b' \x0a\x00 \x41\x01 \x02\x01\x00\x00\x00\x01' +LISTEN_ENCODER_ON_D = b' \x0a\x00 \x41\x02 \x02\x01\x00\x00\x00\x01' + +LISTEN_TILT_BASIC = b' \x0a\x00 \x41\x3a \x02\x01\x00\x00\x00\x01' +LISTEN_TILT_FULL = b' \x0a\x00 \x41\x3a \x00\x01\x00\x00\x00\x01'