From c4aaa9fd6dc5ec1e9469b229c1da387262d33952 Mon Sep 17 00:00:00 2001 From: Andrey Pohilko Date: Thu, 21 Sep 2017 21:52:13 +0300 Subject: [PATCH] Clarify tilt sensor constants --- README.md | 18 ++++++++++++++++- demo.py | 7 ++++--- pylgbst/peripherals.py | 44 ++++++++++++++++++++++++++---------------- tests.py | 2 +- 4 files changed, 49 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index a30efc0..3a374ef 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ Demonstrational video: Install library like this: ```bash -pip install https://github.com/undera/pylgbst/archive/0.3.tar.gz +pip install https://github.com/undera/pylgbst/archive/0.4.tar.gz ``` Then instantiate MoveHub object and start invoking its methods. Following is example to just print peripherals detected on Hub: @@ -128,6 +128,22 @@ hub.tilt_sensor.unsubscribe(callback) - `MODE_3AXIS_FULL` - use `callback(roll, pitch)` for 2-axis roll&pitch degree values - `MODE_BUMP_COUNT` - use `callback(count)` to detect bumps +There are tilt sensor constants for "simple" states, for 2-axis mode their names are also available through `TiltSensor.DUO_STATES`: +- `DUO_HORIZ` - "HORIZONTAL" +- `DUO_DOWN` - "DOWN" +- `DUO_LEFT` - "LEFT" +- `DUO_RIGHT` - "RIGHT" +- `DUO_UP` - "UP" + +For 3-axis simple mode map name is `TiltSensor.TRI_STATES` with values: +- `TRI_BACK` - "BACK" +- `TRI_UP` - "UP" +- `TRI_DOWN` - "DOWN" +- `TRI_LEFT` - "LEFT" +- `TRI_RIGHT` - "RIGHT" +- `TRI_FRONT` - "FRONT" + + ### Color & Distance Sensor Field named `color_distance_sensor` holds instance of `ColorDistanceSensor`, if one is attached to MoveHub. Sensor has number of different modes to subscribe. diff --git a/demo.py b/demo.py index 6b3dbc5..02b3802 100644 --- a/demo.py +++ b/demo.py @@ -73,11 +73,11 @@ def demo_tilt_sensor_simple(movehub): demo_tilt_sensor_simple.cnt = 0 limit = 10 - def callback(param1): + def callback(state): demo_tilt_sensor_simple.cnt += 1 - log.info("Tilt #%s of %s: %s", demo_tilt_sensor_simple.cnt, limit, TiltSensor.TILT_STATES[param1]) + log.info("Tilt #%s of %s: %s=%s", demo_tilt_sensor_simple.cnt, limit, TiltSensor.TRI_STATES[state], state) - movehub.tilt_sensor.subscribe(callback, mode=TiltSensor.MODE_2AXIS_SIMPLE) + movehub.tilt_sensor.subscribe(callback, mode=TiltSensor.MODE_3AXIS_SIMPLE) while demo_tilt_sensor_simple.cnt < limit: time.sleep(1) @@ -171,4 +171,5 @@ if __name__ == '__main__': connection = BLEConnection().connect() hub = MoveHub(connection) + demo_all(hub) diff --git a/pylgbst/peripherals.py b/pylgbst/peripherals.py index 658f54e..55cb068 100644 --- a/pylgbst/peripherals.py +++ b/pylgbst/peripherals.py @@ -258,24 +258,34 @@ class TiltSensor(Peripheral): MODE_BUMP_COUNT = 0x03 MODE_3AXIS_FULL = 0x04 - HORIZONTAL = 0x00 - UP = 0x01 - DOWN = 0x02 - RIGHT = 0x03 - LEFT = 0x04 - FRONT = 0x05 - SOME1 = 0x07 # TODO - SOME2 = 0x09 # TODO + TRI_BACK = 0x00 + TRI_UP = 0x01 + TRI_DOWN = 0x02 + TRI_LEFT = 0x03 + TRI_RIGHT = 0x04 + TRI_FRONT = 0x05 - TILT_STATES = { - HORIZONTAL: "HORIZONTAL", - UP: "UP", - DOWN: "DOWN", - RIGHT: "RIGHT", - LEFT: "LEFT", - FRONT: "FRONT", - SOME1: "LEFT1", - SOME2: "RIGHT1", + DUO_HORIZ = 0x00 + DUO_DOWN = 0x03 + DUO_LEFT = 0x05 + DUO_RIGHT = 0x07 + DUO_UP = 0x09 + + DUO_STATES = { + DUO_HORIZ: "HORIZONTAL", + DUO_DOWN: "DOWN", + DUO_LEFT: "LEFT", + DUO_RIGHT: "RIGHT", + DUO_UP: "UP", + } + + TRI_STATES = { + TRI_BACK: "BACK", + TRI_UP: "UP", + TRI_DOWN: "DOWN", + TRI_LEFT: "LEFT", + TRI_RIGHT: "RIGHT", + TRI_FRONT: "FRONT", } def subscribe(self, callback, mode=MODE_3AXIS_SIMPLE, granularity=1, async=False): diff --git a/tests.py b/tests.py index 11c2723..1644f53 100644 --- a/tests.py +++ b/tests.py @@ -83,7 +83,7 @@ class GeneralTest(unittest.TestCase): def callback(param1, param2=None, param3=None): if param2 is None: - log.debug("Tilt: %s", TiltSensor.TILT_STATES[param1]) + log.debug("Tilt: %s", TiltSensor.TRI_STATES[param1]) else: log.debug("Tilt: %s %s %s", param1, param2, param3)