mirror of
https://github.com/undera/pylgbst.git
synced 2020-11-18 19:37:26 -08:00
Color-distance
This commit is contained in:
parent
5ca9a859c6
commit
2819dd4c3c
6
demo.py
6
demo.py
@ -117,10 +117,10 @@ def demo_color_sensor(movehub):
|
||||
|
||||
def callback(color, distance=None, param=None):
|
||||
demo_color_sensor.cnt += 1
|
||||
clr = COLORS[color] if color in COLORS else color
|
||||
log.info("#%s/%s: Color %s, distance %s, param %s", demo_color_sensor.cnt, limit, clr, distance, param)
|
||||
#color = COLORS[color] if color in COLORS else color
|
||||
log.info("#%s/%s: Color %s, distance %s, param %s", demo_color_sensor.cnt, limit, color, distance, param)
|
||||
|
||||
movehub.color_distance_sensor.subscribe(callback, 0x08)
|
||||
movehub.color_distance_sensor.subscribe(callback, CDS_MODE_STREAM_3_VALUES, granularity=3)
|
||||
while demo_color_sensor.cnt < limit:
|
||||
time.sleep(1)
|
||||
|
||||
|
@ -96,7 +96,7 @@ class MoveHub(object):
|
||||
return
|
||||
|
||||
device = self.devices[port]
|
||||
device.handle_notification(data)
|
||||
device.handle_sensor_data(data)
|
||||
|
||||
def _handle_port_status(self, data):
|
||||
port = get_byte(data, 3)
|
||||
|
@ -122,14 +122,14 @@ TILT_STATES = {
|
||||
}
|
||||
|
||||
# COLOR & DISTANCE SENSOR
|
||||
CLR_DIST_MODE_COLOR_ONLY = 0x00
|
||||
CLR_DIST_MODE_DISTANCE_INCHES = 0x01
|
||||
CLR_DIST_MODE_COUNT_2INCH = 0x02
|
||||
CLR_DIST_MODE_DISTANCE_HOW_CLOSE = 0x03
|
||||
CLR_DIST_MODE_DISTANCE_SUBINCH_HOW_CLOSE = 0x04
|
||||
CLR_DIST_MODE_OFF1 = 0x05
|
||||
CLR_DIST_MODE_STREAM_3_VALUES = 0x06
|
||||
CLR_DIST_MODE_OFF2 = 0x07
|
||||
CLR_DIST_MODE_COLOR_DISTANCE_INCHES_SUBINCHES = 0x08
|
||||
CLR_DIST_MODE_LUMINOSITY = 0x09
|
||||
CLR_DIST_MODE_SOME_14BYTES = 0x0a
|
||||
CDS_MODE_COLOR_ONLY = 0x00
|
||||
CDS_MODE_DISTANCE_INCHES = 0x01
|
||||
CDS_MODE_COUNT_2INCH = 0x02
|
||||
CDS_MODE_DISTANCE_HOW_CLOSE = 0x03
|
||||
CDS_MODE_DISTANCE_SUBINCH_HOW_CLOSE = 0x04
|
||||
CDS_MODE_OFF1 = 0x05
|
||||
CDS_MODE_STREAM_3_VALUES = 0x06
|
||||
CDS_MODE_OFF2 = 0x07
|
||||
CDS_MODE_COLOR_DISTANCE_INCHES_SUBINCHES = 0x08
|
||||
CDS_MODE_LUMINOSITY = 0x09
|
||||
CDS_MODE_SOME_20BYTES = 0x0a
|
||||
|
@ -43,7 +43,7 @@ class Peripheral(object):
|
||||
for subscriber in self._subscribers:
|
||||
subscriber(*args, **kwargs)
|
||||
|
||||
def handle_notification(self, data):
|
||||
def handle_sensor_data(self, data):
|
||||
log.warning("Unhandled device notification for %s: %s", self, str2hex(data))
|
||||
|
||||
|
||||
@ -52,7 +52,7 @@ class LED(Peripheral):
|
||||
if color not in COLORS:
|
||||
raise ValueError("Color %s is not in list of available colors" % color)
|
||||
|
||||
cmd = b'\xFF\x51\x00' + int2byte(color)
|
||||
cmd = b'\x01\x51\x00' + int2byte(color)
|
||||
self._write_to_hub(MSG_SET_PORT_VAL, cmd)
|
||||
|
||||
def finished(self):
|
||||
@ -149,7 +149,7 @@ class TiltSensor(Peripheral):
|
||||
self._write_to_hub(MSG_SENSOR_SUBSCRIBE, int2byte(self.mode) + b'\x00\x00\x00' + int2byte(0))
|
||||
self.mode = None
|
||||
|
||||
def handle_notification(self, data):
|
||||
def handle_sensor_data(self, data):
|
||||
if self.mode == TILT_SENSOR_MODE_BASIC:
|
||||
state = get_byte(data, 4)
|
||||
self._notify_subscribers(state)
|
||||
@ -184,7 +184,7 @@ class ColorDistanceSensor(Peripheral):
|
||||
super(ColorDistanceSensor, self).__init__(parent, port)
|
||||
self.mode = None
|
||||
|
||||
def subscribe(self, callback, mode=CLR_DIST_MODE_COLOR_DISTANCE_INCHES_SUBINCHES, granularity=1):
|
||||
def subscribe(self, callback, mode=CDS_MODE_COLOR_DISTANCE_INCHES_SUBINCHES, granularity=1):
|
||||
self.mode = mode
|
||||
params = int2byte(mode)
|
||||
params += int2byte(granularity)
|
||||
@ -201,41 +201,41 @@ class ColorDistanceSensor(Peripheral):
|
||||
self._write_to_hub(MSG_SENSOR_SUBSCRIBE, int2byte(self.mode) + b'\x01\x00\x00\x00' + int2byte(0))
|
||||
self.mode = None
|
||||
|
||||
def handle_notification(self, data):
|
||||
if self.mode == CLR_DIST_MODE_COLOR_DISTANCE_INCHES_SUBINCHES:
|
||||
def handle_sensor_data(self, data):
|
||||
if self.mode == CDS_MODE_COLOR_DISTANCE_INCHES_SUBINCHES:
|
||||
color = get_byte(data, 4)
|
||||
distance = get_byte(data, 5)
|
||||
partial = get_byte(data, 7)
|
||||
if partial:
|
||||
distance += 1.0 / partial
|
||||
self._notify_subscribers(color if color != 0xFF else None, float(distance))
|
||||
elif self.mode == CLR_DIST_MODE_COLOR_ONLY:
|
||||
elif self.mode == CDS_MODE_COLOR_ONLY:
|
||||
color = get_byte(data, 4)
|
||||
self._notify_subscribers(color if color != 0xFF else None)
|
||||
elif self.mode == CLR_DIST_MODE_DISTANCE_INCHES:
|
||||
elif self.mode == CDS_MODE_DISTANCE_INCHES:
|
||||
distance = get_byte(data, 4)
|
||||
self._notify_subscribers(float(distance))
|
||||
elif self.mode == CLR_DIST_MODE_DISTANCE_HOW_CLOSE:
|
||||
elif self.mode == CDS_MODE_DISTANCE_HOW_CLOSE:
|
||||
distance = get_byte(data, 4)
|
||||
self._notify_subscribers(float(distance))
|
||||
elif self.mode == CLR_DIST_MODE_DISTANCE_SUBINCH_HOW_CLOSE:
|
||||
elif self.mode == CDS_MODE_DISTANCE_SUBINCH_HOW_CLOSE:
|
||||
distance = get_byte(data, 4)
|
||||
self._notify_subscribers(float(distance))
|
||||
elif self.mode == CLR_DIST_MODE_OFF1 or self.mode == CLR_DIST_MODE_OFF2:
|
||||
elif self.mode == CDS_MODE_OFF1 or self.mode == CDS_MODE_OFF2:
|
||||
log.info("Turned off led on %s", self)
|
||||
elif self.mode == CLR_DIST_MODE_COUNT_2INCH:
|
||||
elif self.mode == CDS_MODE_COUNT_2INCH:
|
||||
count = struct.unpack("<L", data[4:8])[0] # is it all 4 bytes or just 2?
|
||||
self._notify_subscribers(count)
|
||||
elif self.mode == CLR_DIST_MODE_STREAM_3_VALUES:
|
||||
elif self.mode == CDS_MODE_STREAM_3_VALUES:
|
||||
# TODO: understand better meaning of these 3 values
|
||||
val1 = struct.unpack("<H", data[4:6])[0]
|
||||
val2 = struct.unpack("<H", data[6:8])[0]
|
||||
val3 = struct.unpack("<H", data[8:10])[0]
|
||||
self._notify_subscribers(val1, val2, val3)
|
||||
elif self.mode == CLR_DIST_MODE_LUMINOSITY:
|
||||
elif self.mode == CDS_MODE_LUMINOSITY:
|
||||
luminosity = struct.unpack("<H", data[4:6])[0]
|
||||
self._notify_subscribers(luminosity)
|
||||
else:
|
||||
else: # TODO: support whatever we forgot
|
||||
log.warning("Unhandled data in mode %s: %s", self.mode, str2hex(data))
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user