mirror of
https://github.com/undera/pylgbst.git
synced 2020-11-18 19:37:26 -08:00
refactoring
This commit is contained in:
parent
cded44a3b2
commit
3e1c5c4892
@ -66,10 +66,10 @@ class MoveHub(object):
|
||||
Using https://github.com/JorgePe/BOOSTreveng/blob/master/Notifications.md
|
||||
"""
|
||||
orig = data
|
||||
log.debug("Notification on %s: %s", handle, str2hex(orig))
|
||||
data = data[3:]
|
||||
log.debug("Notification on %s: %s", handle, str2hex(orig))
|
||||
|
||||
msg_type = ord(data[2])
|
||||
msg_type = get_byte(data, 2)
|
||||
|
||||
if msg_type == MSG_PORT_INFO:
|
||||
self._handle_port_info(data)
|
||||
@ -81,8 +81,8 @@ class MoveHub(object):
|
||||
pass
|
||||
|
||||
def _handle_port_status(self, data):
|
||||
port = ord(data[3])
|
||||
status = ord(data[4])
|
||||
port = get_byte(data, 3)
|
||||
status = get_byte(data, 4)
|
||||
|
||||
if status == STATUS_STARTED:
|
||||
self.devices[port].started()
|
||||
@ -94,8 +94,8 @@ class MoveHub(object):
|
||||
log.warning("Unhandled status value: 0x%x", status)
|
||||
|
||||
def _handle_port_info(self, data):
|
||||
port = ord(data[3])
|
||||
dev_type = ord(data[5])
|
||||
port = get_byte(data, 3)
|
||||
dev_type = get_byte(data, 5)
|
||||
|
||||
if port in PORTS and dev_type in DEVICE_TYPES:
|
||||
log.debug("Device %s at port %s", DEVICE_TYPES[dev_type], PORTS[port])
|
||||
|
@ -8,11 +8,10 @@ import sys
|
||||
import time
|
||||
import traceback
|
||||
from abc import abstractmethod
|
||||
from gattlib import DiscoveryService, GATTRequester
|
||||
from threading import Thread
|
||||
|
||||
from gattlib import DiscoveryService, GATTRequester
|
||||
|
||||
from pylgbst.constants import DEVICE_NAME, LEGO_MOVE_HUB
|
||||
from pylgbst.constants import LEGO_MOVE_HUB
|
||||
|
||||
log = logging.getLogger('transport')
|
||||
|
||||
@ -23,6 +22,10 @@ if sys.version_info[0] == 2:
|
||||
|
||||
def hex2str(data):
|
||||
return data.decode("hex")
|
||||
|
||||
|
||||
def get_byte(seq, index):
|
||||
return ord(seq[index])
|
||||
else:
|
||||
import binascii
|
||||
|
||||
@ -35,6 +38,10 @@ else:
|
||||
return binascii.unhexlify(data)
|
||||
|
||||
|
||||
def get_byte(seq, index):
|
||||
return seq[index]
|
||||
|
||||
|
||||
# noinspection PyMethodOverriding
|
||||
class Requester(GATTRequester):
|
||||
"""
|
||||
@ -108,7 +115,7 @@ class BLEConnection(Connection):
|
||||
raise RuntimeError("No requester available")
|
||||
|
||||
def read(self, handle):
|
||||
# FIXME: repeating reads hang...
|
||||
# FIXME: repeating reads hangs it...
|
||||
log.debug("Reading from: %s", handle)
|
||||
data = self.requester.read_by_handle(handle)
|
||||
log.debug("Result: %s", data)
|
||||
|
@ -62,6 +62,7 @@ PORTS = {
|
||||
MSG_PORT_INFO = 0x04
|
||||
MSG_PORT_STATUS = 0x82
|
||||
MSG_SET_PORT_VAL = 0x81
|
||||
MSG_PORT_SUBSCRIBE = 0x41
|
||||
|
||||
# NOTIFICATIONS
|
||||
TYPE_DISTANCE_COLOR_SENSOR = 0x25
|
||||
|
@ -15,11 +15,21 @@ class Peripheral(object):
|
||||
self.port = port
|
||||
self.working = False
|
||||
|
||||
def _set_port_val(self, value):
|
||||
cmd = PACKET_VER + chr(MSG_SET_PORT_VAL) + chr(self.port)
|
||||
cmd += value
|
||||
def __repr__(self):
|
||||
return "%s on port %s" % (self.__class__.__name__, PORTS[self.port] if self.port in PORTS else 'N/A')
|
||||
|
||||
self.parent.connection.write(MOVE_HUB_HARDWARE_HANDLE, chr(len(cmd)) + cmd) # should we +1 cmd len here?
|
||||
def _write_to_hub(self, msg_type, params):
|
||||
cmd = PACKET_VER + chr(msg_type) + chr(self.port)
|
||||
cmd += params
|
||||
self.parent.connection.write(MOVE_HUB_HARDWARE_HANDLE, chr(len(cmd) + 1) + cmd) # should we +1 cmd len here?
|
||||
|
||||
def _set_port_val(self, value):
|
||||
# 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_PORT_SUBSCRIBE, params)
|
||||
|
||||
def started(self):
|
||||
self.working = True
|
||||
@ -27,9 +37,6 @@ class Peripheral(object):
|
||||
def finished(self):
|
||||
self.working = False
|
||||
|
||||
def __repr__(self):
|
||||
return "%s on port %s" % (self.__class__.__name__, PORTS[self.port] if self.port in PORTS else 'N/A')
|
||||
|
||||
|
||||
class LED(Peripheral):
|
||||
def set_color(self, color):
|
||||
@ -109,7 +116,9 @@ class ColorDistanceSensor(Peripheral):
|
||||
|
||||
|
||||
class TiltSensor(Peripheral):
|
||||
pass
|
||||
def subscribe(self, callback):
|
||||
self._subscribe_on_port(params)
|
||||
self._subscribers.append(callback)
|
||||
|
||||
|
||||
class Button(Peripheral):
|
||||
|
Loading…
x
Reference in New Issue
Block a user