From 62ce93a8e58e285467d8da78a791b9b628e41d29 Mon Sep 17 00:00:00 2001 From: Andrey Pohilko Date: Wed, 20 Sep 2017 09:56:25 +0300 Subject: [PATCH] documenting peripherals --- README.md | 44 +++++++++++++++++++++++++++++++++++++----- pylgbst/peripherals.py | 4 ++-- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 014d84c..cf3ff7d 100644 --- a/README.md +++ b/README.md @@ -41,20 +41,56 @@ TODO: more usage instructions connection params hub's devices detect process & fields to access them general subscription modes & granularity info - +good practice is to unsubscribe, especially when used with `DebugServer` ### Motors ### Motor Rotation Sensors ### Tilt Sensor ### Color & Distance Sensor Tip: laser pointer pointing to sensor makes it trigger distance sensor ### LED + ### Push Button + +Hub object has field `button` to subscribe to button press and release events. Note that `Button` class is not real `Peripheral`, we just mimic it. Subscribing to button uses internal mechanics which is slightly different from other peripherals. + +Still, subscribing to button is done usual way: + +```python +from pylgbst import MoveHub + +def callback(is_pressed): + print("Btn pressed: %s" % is_pressed) + +hub = MoveHub() +hub.button.subscribe(callback) +``` + ### Power Voltage & Battery +Hub object has field `battery` to subscribe to battery voltage status. Callback accepts single parameter with current value. The range of values is unknown, it's 2-byte integer. Every time data is received, value is also written into `last_value` field of Battery object. + +```python +from pylgbst import MoveHub +import time + +def callback(value): + print("Voltage: %s" % value) + +hub = MoveHub() +hub.battery.subscribe(callback) +time.sleep(1) +print ("Value: " % hub.battery.last_value) +``` + +TODO: investigate `mode` parameter for battery ## Debug Server +Running debug server opens permanent BLE connection to Hub and listening on TCP port for communications. This avoids the need to re-start Hub all the time. -``` +There is `DebugServerConnection` class that you can use with it, instead of `BLEConnection`. + +Starting debug server is done like this: +```bash sudo python -c "from pylgbst.comms import *; \ import logging; logging.basicConfig(level=logging.DEBUG); \ DebugServer(BLEConnection().connect()).start()" @@ -62,13 +98,11 @@ sudo python -c "from pylgbst.comms import *; \ ## TODO -- handle device detach and device attach events on ports C/D -- experiment with motor commands, find what is hidden there - Give nice documentation examples, don't forget to mention logging - document all API methods - make sure unit tests cover all important code +- handle device detach and device attach events on ports C/D - generalize getting device info + give constants (low priority) -- can we subscribe to LED? - organize requesting and printing device info on startup - firmware version at least - make debug server to re-establish BLE connection on loss diff --git a/pylgbst/peripherals.py b/pylgbst/peripherals.py index c5b84ad..96f765d 100644 --- a/pylgbst/peripherals.py +++ b/pylgbst/peripherals.py @@ -380,7 +380,7 @@ class Battery(Peripheral): # good 7.5v ~= 3892 # liion 5v ~= 2100 def handle_port_data(self, data): - self.last_value = unpack("