mirror of
https://github.com/undera/pylgbst.git
synced 2020-11-18 19:37:26 -08:00
Some minor fixes
This commit is contained in:
parent
d52581c236
commit
33e97e474f
@ -12,6 +12,7 @@ Best way to start is to look at [demo.py](demo.py) file, and run it.
|
|||||||
- angled and timed movement for motors
|
- angled and timed movement for motors
|
||||||
- LED color change
|
- LED color change
|
||||||
- sensor data subscribe/unsubscribe
|
- sensor data subscribe/unsubscribe
|
||||||
|
- battery voltage available
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
6
demo.py
6
demo.py
@ -172,9 +172,9 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
hub = MoveHub(connection)
|
hub = MoveHub(connection)
|
||||||
|
|
||||||
hub.devices[PORT_BATTERY].subscribe(cb_log, 0x00, granularity=2)
|
#hub.devices[PORT_SOMETHING1].subscribe(cb_log, 0x00, granularity=1)
|
||||||
sleep(10)
|
sleep(10)
|
||||||
demo_port_cd_motor(hub)
|
demo_port_cd_motor(hub)
|
||||||
sleep(60)
|
sleep(10)
|
||||||
hub.devices[PORT_BATTERY].unsubscribe(cb_log)
|
#hub.devices[PORT_SOMETHING1].unsubscribe(cb_log)
|
||||||
# demo_all(hub)
|
# demo_all(hub)
|
||||||
|
@ -16,7 +16,7 @@ from threading import Thread
|
|||||||
|
|
||||||
from pylgbst.constants import LEGO_MOVE_HUB, MSG_DEVICE_SHUTDOWN
|
from pylgbst.constants import LEGO_MOVE_HUB, MSG_DEVICE_SHUTDOWN
|
||||||
|
|
||||||
log = logging.getLogger('transport')
|
log = logging.getLogger('comms')
|
||||||
|
|
||||||
|
|
||||||
def str2hex(data): # TODO: eliminate it
|
def str2hex(data): # TODO: eliminate it
|
||||||
@ -171,7 +171,7 @@ class DebugServer(object):
|
|||||||
self.sock.close()
|
self.sock.close()
|
||||||
|
|
||||||
def _notify_dummy(self, handle, data):
|
def _notify_dummy(self, handle, data):
|
||||||
log.debug("Notification from handle %s: %s", handle, binascii.hexlify(data.strip()))
|
log.debug("Dropped notification from handle %s: %s", handle, binascii.hexlify(data.strip()))
|
||||||
self._check_shutdown(data)
|
self._check_shutdown(data)
|
||||||
|
|
||||||
def _notify(self, conn, handle, data):
|
def _notify(self, conn, handle, data):
|
||||||
@ -291,7 +291,10 @@ class DebugServerConnection(Connection):
|
|||||||
if line:
|
if line:
|
||||||
item = json.loads(line)
|
item = json.loads(line)
|
||||||
if item['type'] == 'notification' and self.notify_handler:
|
if item['type'] == 'notification' and self.notify_handler:
|
||||||
|
try:
|
||||||
self.notify_handler(item['handle'], unhexlify(item['data']))
|
self.notify_handler(item['handle'], unhexlify(item['data']))
|
||||||
|
except BaseException:
|
||||||
|
log.error("Failed to notify handler: %s", traceback.format_exc())
|
||||||
elif item['type'] == 'response':
|
elif item['type'] == 'response':
|
||||||
self.incoming.append(item)
|
self.incoming.append(item)
|
||||||
else:
|
else:
|
||||||
|
@ -61,7 +61,9 @@ class Peripheral(object):
|
|||||||
if callback in self._subscribers:
|
if callback in self._subscribers:
|
||||||
self._subscribers.remove(callback)
|
self._subscribers.remove(callback)
|
||||||
|
|
||||||
if not self._subscribers:
|
if not self._port_subscription_mode:
|
||||||
|
log.warning("Attempt to unsubscribe while never subscribed: %s", self)
|
||||||
|
elif not self._subscribers:
|
||||||
self._port_subscribe(self._port_subscription_mode, 0, False)
|
self._port_subscribe(self._port_subscription_mode, 0, False)
|
||||||
self._port_subscription_mode = None
|
self._port_subscription_mode = None
|
||||||
|
|
||||||
@ -273,8 +275,9 @@ class ColorDistanceSensor(Peripheral):
|
|||||||
|
|
||||||
|
|
||||||
class Battery(Peripheral):
|
class Battery(Peripheral):
|
||||||
|
# we know only voltage subscription from it. is it really battery or just onboard voltage?
|
||||||
def handle_port_data(self, data):
|
def handle_port_data(self, data):
|
||||||
self._notify_subscribers(unpack("<H", data))
|
self._notify_subscribers(unpack("<H", data[4:6]))
|
||||||
|
|
||||||
|
|
||||||
class Button(Peripheral):
|
class Button(Peripheral):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user