diff --git a/demo.py b/demo.py index 7a7b68c..f7a76f9 100644 --- a/demo.py +++ b/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) diff --git a/pylgbst/__init__.py b/pylgbst/__init__.py index ea6ee25..53cfd4e 100644 --- a/pylgbst/__init__.py +++ b/pylgbst/__init__.py @@ -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) diff --git a/pylgbst/constants.py b/pylgbst/constants.py index 51067f8..39d6a2b 100644 --- a/pylgbst/constants.py +++ b/pylgbst/constants.py @@ -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 diff --git a/pylgbst/peripherals.py b/pylgbst/peripherals.py index ccb332b..34eee1f 100644 --- a/pylgbst/peripherals.py +++ b/pylgbst/peripherals.py @@ -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("