1
0
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:
Andrey Pohilko 2017-09-15 22:50:54 +03:00
parent d52581c236
commit 33e97e474f
4 changed files with 15 additions and 8 deletions

View File

@ -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
- LED color change
- sensor data subscribe/unsubscribe
- battery voltage available
## Usage

View File

@ -172,9 +172,9 @@ if __name__ == '__main__':
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)
demo_port_cd_motor(hub)
sleep(60)
hub.devices[PORT_BATTERY].unsubscribe(cb_log)
sleep(10)
#hub.devices[PORT_SOMETHING1].unsubscribe(cb_log)
# demo_all(hub)

View File

@ -16,7 +16,7 @@ from threading import Thread
from pylgbst.constants import LEGO_MOVE_HUB, MSG_DEVICE_SHUTDOWN
log = logging.getLogger('transport')
log = logging.getLogger('comms')
def str2hex(data): # TODO: eliminate it
@ -171,7 +171,7 @@ class DebugServer(object):
self.sock.close()
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)
def _notify(self, conn, handle, data):
@ -291,7 +291,10 @@ class DebugServerConnection(Connection):
if line:
item = json.loads(line)
if item['type'] == 'notification' and self.notify_handler:
self.notify_handler(item['handle'], unhexlify(item['data']))
try:
self.notify_handler(item['handle'], unhexlify(item['data']))
except BaseException:
log.error("Failed to notify handler: %s", traceback.format_exc())
elif item['type'] == 'response':
self.incoming.append(item)
else:

View File

@ -61,7 +61,9 @@ class Peripheral(object):
if callback in self._subscribers:
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_subscription_mode = None
@ -273,8 +275,9 @@ class ColorDistanceSensor(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):
self._notify_subscribers(unpack("<H", data))
self._notify_subscribers(unpack("<H", data[4:6]))
class Button(Peripheral):