Skip to content

Commit

Permalink
Cosmetics
Browse files Browse the repository at this point in the history
  • Loading branch information
undera committed Dec 27, 2019
1 parent c955820 commit 907a2dd
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 40 deletions.
1 change: 0 additions & 1 deletion examples/automata/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,3 @@ def read_color():
action_by_color(color)

print(number)

9 changes: 6 additions & 3 deletions examples/bb8joystick/bb8.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class _SpheroImproved(spheropy.Sphero):
async def connect(self, search_name=None, address=None, port=None, bluetooth_interface=None, use_ble=False,
num_retry_attempts=1):
gattool = BLEInterfaceGattool(search_name)
return await super().connect(search_name, address, port, gattool, use_ble, num_retry_attempts)
return await
super().connect(search_name, address, port, gattool, use_ble, num_retry_attempts)

async def sleep(self, sleeptime, reset_inactivity_timeout=True, response_timeout_in_seconds=None):
# port from https://github.com/jchadwhite/SpheroBB8-python/blob/master/BB8_driver.py#L394
Expand All @@ -33,7 +34,8 @@ async def sleep(self, sleeptime, reset_inactivity_timeout=True, response_timeout
wait_for_response=False,
reset_inactivity_timeout=reset_inactivity_timeout)

return await self._send_command(command, response_timeout_in_seconds)
return await
self._send_command(command, response_timeout_in_seconds)

async def set_rotation_rate(self, rate, reset_inactivity_timeout=True, response_timeout_in_seconds=None):
# port from https://github.com/jchadwhite/SpheroBB8-python/blob/master/BB8_driver.py
Expand All @@ -44,7 +46,8 @@ async def set_rotation_rate(self, rate, reset_inactivity_timeout=True, response_
wait_for_response=False,
reset_inactivity_timeout=reset_inactivity_timeout)

return await self._send_command(command, response_timeout_in_seconds)
return await
self._send_command(command, response_timeout_in_seconds)


class BB8(object):
Expand Down
59 changes: 32 additions & 27 deletions examples/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
from time import sleep

from pylgbst import *
from pylgbst.comms import DebugServerConnection
from pylgbst.hub import MoveHub, math
from pylgbst.hub import MoveHub
from pylgbst.peripherals import EncodedMotor, TiltSensor, Current, Voltage, COLORS, COLOR_BLACK

log = logging.getLogger("demo")
Expand All @@ -13,9 +12,11 @@
def demo_led_colors(movehub):
# LED colors demo
log.info("LED colors demo")

# We get a response with payload and port, not x and y here...
def colour_callback(**named):
log.info("LED Color callback: %s", named)

movehub.led.subscribe(colour_callback)
for color in list(COLORS.keys())[1:] + [COLOR_BLACK]:
log.info("Setting LED color to: %s", COLORS[color])
Expand Down Expand Up @@ -183,55 +184,59 @@ def demo_all(movehub):
demo_color_sensor(movehub)
demo_motor_sensors(movehub)


DEMO_CHOICES = {
'all':demo_all,
'voltage':demo_voltage,
'led_colors':demo_led_colors,
'motors_timed':demo_motors_timed,
'motors_angled':demo_motors_angled,
'port_cd_motor':demo_port_cd_motor,
'tilt_sensor':demo_tilt_sensor_simple,
'tilt_sensor_precise':demo_tilt_sensor_precise,
'color_sensor':demo_color_sensor,
'motor_sensors':demo_motor_sensors,
'all': demo_all,
'voltage': demo_voltage,
'led_colors': demo_led_colors,
'motors_timed': demo_motors_timed,
'motors_angled': demo_motors_angled,
'port_cd_motor': demo_port_cd_motor,
'tilt_sensor': demo_tilt_sensor_simple,
'tilt_sensor_precise': demo_tilt_sensor_precise,
'color_sensor': demo_color_sensor,
'motor_sensors': demo_motor_sensors,
}


def get_options():
import argparse
parser = argparse.ArgumentParser(
import argparse
arg_parser = argparse.ArgumentParser(
description='Demonstrate move-hub communications',
)
parser.add_argument(
'-c','--connection',
arg_parser.add_argument(
'-c', '--connection',
default='auto://',
help='''Specify connection URL to use, `protocol://mac?param=X` with protocol in:
"gatt","pygatt","gattlib","gattool", "bluepy","bluegiga"'''
)
parser.add_argument(
'-d','--demo',
arg_parser.add_argument(
'-d', '--demo',
default='all',
choices = sorted(DEMO_CHOICES.keys()),
choices=sorted(DEMO_CHOICES.keys()),
help="Run a particular demo, default all"
)
return parser
return arg_parser


def connection_from_url(url):
import pylgbst
if url == 'auto://':
return None
return None
try:
from urllib.parse import urlparse, parse_qs
except ImportError:
from urlparse import urlparse, parse_qs
parsed = urlparse(url)
name = 'get_connection_%s'%(parsed.scheme)
factory = getattr( pylgbst, name, None)
name = 'get_connection_%s' % parsed.scheme
factory = getattr(pylgbst, name, None)
if not factory:
raise ValueError("Unrecognised URL scheme/protocol, expect a get_connection_<protocol> in pylgbst: %s"%(parsed.protocol))
msg = "Unrecognised URL scheme/protocol, expect a get_connection_<protocol> in pylgbst: %s"
raise ValueError(msg % parsed.protocol)
params = {}
if parsed.netloc.strip():
params['hub_mac'] = parsed.netloc
for key,value in parse_qs(parsed.query).items():
for key, value in parse_qs(parsed.query).items():
if len(value) == 1:
params[key] = value[0]
else:
Expand All @@ -240,6 +245,7 @@ def connection_from_url(url):
**params
)


if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
parser = get_options()
Expand All @@ -249,8 +255,7 @@ def connection_from_url(url):
connection = connection_from_url(options.connection)
parameters['connection'] = connection
except ValueError as err:
parser.error(error.args[0])
connection
parser.error(err.args[0])

hub = MoveHub(**parameters)
try:
Expand Down
12 changes: 10 additions & 2 deletions examples/tracker/color.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
[
[125, 64, 64],
[145, 255, 250]
[
125,
64,
64
],
[
145,
255,
250
]
]
1 change: 0 additions & 1 deletion pylgbst/comms/cpygatt.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ def is_alive(self):
return True



class BlueGigaConnection(GattoolConnection):
def __init__(self):
super(BlueGigaConnection, self).__init__()
Expand Down
2 changes: 1 addition & 1 deletion pylgbst/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def bytes(self):

def is_reply(self, msg):
if not isinstance(msg, MsgHubAction):
raise TypeError("Unexpected message type: %s"%(msg.__class__,))
raise TypeError("Unexpected message type: %s" % (msg.__class__,))
if self.action == self.DISCONNECT and msg.action == self.UPSTREAM_DISCONNECT:
return True

Expand Down
2 changes: 1 addition & 1 deletion pylgbst/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

def check_unpack(seq, index, pattern, size):
"""Check that we got size bytes, if so, unpack using pattern"""
data = seq[index : index + size]
data = seq[index: index + size]
if len(data) == size:
return unpack(pattern, data)[0]
else:
Expand Down
4 changes: 0 additions & 4 deletions tests/descr.json
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,6 @@
]
]
},

"LEDRGB on port 0x32": {
"mode_count": 2,
"input_modes": [],
Expand Down Expand Up @@ -694,7 +693,6 @@
"can_input": false
}
},

"ColorDistanceSensor on port 0x1": {
"mode_count": 11,
"input_modes": [
Expand Down Expand Up @@ -1010,7 +1008,6 @@
]
]
},

"TiltSensor on port 0x3a": {
"mode_count": 8,
"input_modes": [
Expand Down Expand Up @@ -1266,7 +1263,6 @@
]
]
},

"Current on port 0x3b": {
"mode_count": 2,
"input_modes": [
Expand Down

0 comments on commit 907a2dd

Please sign in to comment.