mirror of
https://github.com/undera/pylgbst.git
synced 2020-11-18 19:37:26 -08:00
Add battery demo
This commit is contained in:
parent
c4aaa9fd6d
commit
b518e42d53
10
demo.py
10
demo.py
@ -150,7 +150,17 @@ def demo_motor_sensors(movehub):
|
|||||||
movehub.motor_external.unsubscribe(callback_e)
|
movehub.motor_external.unsubscribe(callback_e)
|
||||||
|
|
||||||
|
|
||||||
|
def demo_battery(movehub):
|
||||||
|
def callback(value):
|
||||||
|
log.info("Battery voltage: %s", value)
|
||||||
|
|
||||||
|
movehub.battery.subscribe(callback, mode=Battery.MODE2)
|
||||||
|
time.sleep(1)
|
||||||
|
movehub.battery.unsubscribe(callback)
|
||||||
|
|
||||||
|
|
||||||
def demo_all(movehub):
|
def demo_all(movehub):
|
||||||
|
demo_battery(movehub)
|
||||||
demo_led_colors(movehub)
|
demo_led_colors(movehub)
|
||||||
demo_motors_timed(movehub)
|
demo_motors_timed(movehub)
|
||||||
demo_motors_angled(movehub)
|
demo_motors_angled(movehub)
|
||||||
|
@ -76,7 +76,7 @@ class Peripheral(object):
|
|||||||
if callback in self._subscribers:
|
if callback in self._subscribers:
|
||||||
self._subscribers.remove(callback)
|
self._subscribers.remove(callback)
|
||||||
|
|
||||||
if not self._port_subscription_mode:
|
if self._port_subscription_mode is None:
|
||||||
log.warning("Attempt to unsubscribe while never subscribed: %s", self)
|
log.warning("Attempt to unsubscribe while never subscribed: %s", self)
|
||||||
elif not self._subscribers:
|
elif not self._subscribers:
|
||||||
self._port_subscribe(self._port_subscription_mode, 0, False)
|
self._port_subscribe(self._port_subscription_mode, 0, False)
|
||||||
@ -378,12 +378,14 @@ class ColorDistanceSensor(Peripheral):
|
|||||||
|
|
||||||
|
|
||||||
class Battery(Peripheral):
|
class Battery(Peripheral):
|
||||||
|
MODE1 = 0x00 # give less frequent notifications
|
||||||
|
MODE2 = 0x01 # give more frequent notifications, maybe different voltage (cpu vs board?)
|
||||||
|
|
||||||
def __init__(self, parent, port):
|
def __init__(self, parent, port):
|
||||||
super(Battery, self).__init__(parent, port)
|
super(Battery, self).__init__(parent, port)
|
||||||
self.last_value = None
|
self.last_value = None
|
||||||
|
|
||||||
def subscribe(self, callback, mode=0, granularity=1, async=False):
|
def subscribe(self, callback, mode=MODE1, granularity=1, async=False):
|
||||||
# TODO: investigate `mode` parameter for battery
|
|
||||||
super(Battery, self).subscribe(callback, mode, granularity)
|
super(Battery, self).subscribe(callback, mode, granularity)
|
||||||
|
|
||||||
# we know only voltage subscription from it. is it really battery or just onboard voltage?
|
# we know only voltage subscription from it. is it really battery or just onboard voltage?
|
||||||
@ -392,8 +394,8 @@ class Battery(Peripheral):
|
|||||||
# good 7.5v ~= 3892
|
# good 7.5v ~= 3892
|
||||||
# liion 5v ~= 2100
|
# liion 5v ~= 2100
|
||||||
def handle_port_data(self, data):
|
def handle_port_data(self, data):
|
||||||
self.last_value = unpack("<H", data[4:6])[0]
|
val = unpack("<H", data[4:6])[0]
|
||||||
log.warning("Battery: %s"), self.last_value
|
self.last_value = val
|
||||||
self._notify_subscribers(self.last_value)
|
self._notify_subscribers(self.last_value)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user