mirror of
https://github.com/undera/pylgbst.git
synced 2020-11-18 19:37:26 -08:00
refactoring
This commit is contained in:
parent
59e04b4472
commit
c55d796683
@ -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
|
||||
|
@ -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'
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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'
|
||||
|
Loading…
x
Reference in New Issue
Block a user