mirror of
https://github.com/undera/pylgbst.git
synced 2020-11-18 19:37:26 -08:00
Cosmetics
This commit is contained in:
parent
c955820521
commit
907a2dd561
@ -32,4 +32,3 @@ while color != COLOR_NONE:
|
|||||||
action_by_color(color)
|
action_by_color(color)
|
||||||
|
|
||||||
print(number)
|
print(number)
|
||||||
|
|
||||||
|
@ -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,
|
async def connect(self, search_name=None, address=None, port=None, bluetooth_interface=None, use_ble=False,
|
||||||
num_retry_attempts=1):
|
num_retry_attempts=1):
|
||||||
gattool = BLEInterfaceGattool(search_name)
|
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):
|
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
|
# port from https://github.com/jchadwhite/SpheroBB8-python/blob/master/BB8_driver.py#L394
|
||||||
@ -33,7 +34,8 @@ class _SpheroImproved(spheropy.Sphero):
|
|||||||
wait_for_response=False,
|
wait_for_response=False,
|
||||||
reset_inactivity_timeout=reset_inactivity_timeout)
|
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):
|
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
|
# port from https://github.com/jchadwhite/SpheroBB8-python/blob/master/BB8_driver.py
|
||||||
@ -44,7 +46,8 @@ class _SpheroImproved(spheropy.Sphero):
|
|||||||
wait_for_response=False,
|
wait_for_response=False,
|
||||||
reset_inactivity_timeout=reset_inactivity_timeout)
|
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):
|
class BB8(object):
|
||||||
|
@ -3,8 +3,7 @@ import time
|
|||||||
from time import sleep
|
from time import sleep
|
||||||
|
|
||||||
from pylgbst import *
|
from pylgbst import *
|
||||||
from pylgbst.comms import DebugServerConnection
|
from pylgbst.hub import MoveHub
|
||||||
from pylgbst.hub import MoveHub, math
|
|
||||||
from pylgbst.peripherals import EncodedMotor, TiltSensor, Current, Voltage, COLORS, COLOR_BLACK
|
from pylgbst.peripherals import EncodedMotor, TiltSensor, Current, Voltage, COLORS, COLOR_BLACK
|
||||||
|
|
||||||
log = logging.getLogger("demo")
|
log = logging.getLogger("demo")
|
||||||
@ -13,9 +12,11 @@ log = logging.getLogger("demo")
|
|||||||
def demo_led_colors(movehub):
|
def demo_led_colors(movehub):
|
||||||
# LED colors demo
|
# LED colors demo
|
||||||
log.info("LED colors demo")
|
log.info("LED colors demo")
|
||||||
|
|
||||||
# We get a response with payload and port, not x and y here...
|
# We get a response with payload and port, not x and y here...
|
||||||
def colour_callback(**named):
|
def colour_callback(**named):
|
||||||
log.info("LED Color callback: %s", named)
|
log.info("LED Color callback: %s", named)
|
||||||
|
|
||||||
movehub.led.subscribe(colour_callback)
|
movehub.led.subscribe(colour_callback)
|
||||||
for color in list(COLORS.keys())[1:] + [COLOR_BLACK]:
|
for color in list(COLORS.keys())[1:] + [COLOR_BLACK]:
|
||||||
log.info("Setting LED color to: %s", COLORS[color])
|
log.info("Setting LED color to: %s", COLORS[color])
|
||||||
@ -183,55 +184,59 @@ def demo_all(movehub):
|
|||||||
demo_color_sensor(movehub)
|
demo_color_sensor(movehub)
|
||||||
demo_motor_sensors(movehub)
|
demo_motor_sensors(movehub)
|
||||||
|
|
||||||
|
|
||||||
DEMO_CHOICES = {
|
DEMO_CHOICES = {
|
||||||
'all':demo_all,
|
'all': demo_all,
|
||||||
'voltage':demo_voltage,
|
'voltage': demo_voltage,
|
||||||
'led_colors':demo_led_colors,
|
'led_colors': demo_led_colors,
|
||||||
'motors_timed':demo_motors_timed,
|
'motors_timed': demo_motors_timed,
|
||||||
'motors_angled':demo_motors_angled,
|
'motors_angled': demo_motors_angled,
|
||||||
'port_cd_motor':demo_port_cd_motor,
|
'port_cd_motor': demo_port_cd_motor,
|
||||||
'tilt_sensor':demo_tilt_sensor_simple,
|
'tilt_sensor': demo_tilt_sensor_simple,
|
||||||
'tilt_sensor_precise':demo_tilt_sensor_precise,
|
'tilt_sensor_precise': demo_tilt_sensor_precise,
|
||||||
'color_sensor':demo_color_sensor,
|
'color_sensor': demo_color_sensor,
|
||||||
'motor_sensors':demo_motor_sensors,
|
'motor_sensors': demo_motor_sensors,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def get_options():
|
def get_options():
|
||||||
import argparse
|
import argparse
|
||||||
parser = argparse.ArgumentParser(
|
arg_parser = argparse.ArgumentParser(
|
||||||
description='Demonstrate move-hub communications',
|
description='Demonstrate move-hub communications',
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
arg_parser.add_argument(
|
||||||
'-c','--connection',
|
'-c', '--connection',
|
||||||
default='auto://',
|
default='auto://',
|
||||||
help='''Specify connection URL to use, `protocol://mac?param=X` with protocol in:
|
help='''Specify connection URL to use, `protocol://mac?param=X` with protocol in:
|
||||||
"gatt","pygatt","gattlib","gattool", "bluepy","bluegiga"'''
|
"gatt","pygatt","gattlib","gattool", "bluepy","bluegiga"'''
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
arg_parser.add_argument(
|
||||||
'-d','--demo',
|
'-d', '--demo',
|
||||||
default='all',
|
default='all',
|
||||||
choices = sorted(DEMO_CHOICES.keys()),
|
choices=sorted(DEMO_CHOICES.keys()),
|
||||||
help="Run a particular demo, default all"
|
help="Run a particular demo, default all"
|
||||||
)
|
)
|
||||||
return parser
|
return arg_parser
|
||||||
|
|
||||||
|
|
||||||
def connection_from_url(url):
|
def connection_from_url(url):
|
||||||
import pylgbst
|
import pylgbst
|
||||||
if url == 'auto://':
|
if url == 'auto://':
|
||||||
return None
|
return None
|
||||||
try:
|
try:
|
||||||
from urllib.parse import urlparse, parse_qs
|
from urllib.parse import urlparse, parse_qs
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from urlparse import urlparse, parse_qs
|
from urlparse import urlparse, parse_qs
|
||||||
parsed = urlparse(url)
|
parsed = urlparse(url)
|
||||||
name = 'get_connection_%s'%(parsed.scheme)
|
name = 'get_connection_%s' % parsed.scheme
|
||||||
factory = getattr( pylgbst, name, None)
|
factory = getattr(pylgbst, name, None)
|
||||||
if not factory:
|
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 = {}
|
params = {}
|
||||||
if parsed.netloc.strip():
|
if parsed.netloc.strip():
|
||||||
params['hub_mac'] = parsed.netloc
|
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:
|
if len(value) == 1:
|
||||||
params[key] = value[0]
|
params[key] = value[0]
|
||||||
else:
|
else:
|
||||||
@ -240,6 +245,7 @@ def connection_from_url(url):
|
|||||||
**params
|
**params
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
parser = get_options()
|
parser = get_options()
|
||||||
@ -249,8 +255,7 @@ if __name__ == '__main__':
|
|||||||
connection = connection_from_url(options.connection)
|
connection = connection_from_url(options.connection)
|
||||||
parameters['connection'] = connection
|
parameters['connection'] = connection
|
||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
parser.error(error.args[0])
|
parser.error(err.args[0])
|
||||||
connection
|
|
||||||
|
|
||||||
hub = MoveHub(**parameters)
|
hub = MoveHub(**parameters)
|
||||||
try:
|
try:
|
||||||
|
@ -1,4 +1,12 @@
|
|||||||
[
|
[
|
||||||
[125, 64, 64],
|
[
|
||||||
[145, 255, 250]
|
125,
|
||||||
|
64,
|
||||||
|
64
|
||||||
|
],
|
||||||
|
[
|
||||||
|
145,
|
||||||
|
255,
|
||||||
|
250
|
||||||
|
]
|
||||||
]
|
]
|
@ -56,7 +56,6 @@ class GattoolConnection(Connection):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class BlueGigaConnection(GattoolConnection):
|
class BlueGigaConnection(GattoolConnection):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(BlueGigaConnection, self).__init__()
|
super(BlueGigaConnection, self).__init__()
|
||||||
|
@ -174,7 +174,7 @@ class MsgHubAction(DownstreamMsg, UpstreamMsg):
|
|||||||
|
|
||||||
def is_reply(self, msg):
|
def is_reply(self, msg):
|
||||||
if not isinstance(msg, MsgHubAction):
|
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:
|
if self.action == self.DISCONNECT and msg.action == self.UPSTREAM_DISCONNECT:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ queue = queue # just to use it
|
|||||||
|
|
||||||
def check_unpack(seq, index, pattern, size):
|
def check_unpack(seq, index, pattern, size):
|
||||||
"""Check that we got size bytes, if so, unpack using pattern"""
|
"""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:
|
if len(data) == size:
|
||||||
return unpack(pattern, data)[0]
|
return unpack(pattern, data)[0]
|
||||||
else:
|
else:
|
||||||
|
@ -622,7 +622,6 @@
|
|||||||
]
|
]
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
||||||
"LEDRGB on port 0x32": {
|
"LEDRGB on port 0x32": {
|
||||||
"mode_count": 2,
|
"mode_count": 2,
|
||||||
"input_modes": [],
|
"input_modes": [],
|
||||||
@ -694,7 +693,6 @@
|
|||||||
"can_input": false
|
"can_input": false
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
"ColorDistanceSensor on port 0x1": {
|
"ColorDistanceSensor on port 0x1": {
|
||||||
"mode_count": 11,
|
"mode_count": 11,
|
||||||
"input_modes": [
|
"input_modes": [
|
||||||
@ -1010,7 +1008,6 @@
|
|||||||
]
|
]
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
||||||
"TiltSensor on port 0x3a": {
|
"TiltSensor on port 0x3a": {
|
||||||
"mode_count": 8,
|
"mode_count": 8,
|
||||||
"input_modes": [
|
"input_modes": [
|
||||||
@ -1266,7 +1263,6 @@
|
|||||||
]
|
]
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
||||||
"Current on port 0x3b": {
|
"Current on port 0x3b": {
|
||||||
"mode_count": 2,
|
"mode_count": 2,
|
||||||
"input_modes": [
|
"input_modes": [
|
||||||
|
Loading…
x
Reference in New Issue
Block a user