mirror of
https://github.com/undera/pylgbst.git
synced 2020-11-18 19:37:26 -08:00
Working on tilt sensor
This commit is contained in:
parent
3e1c5c4892
commit
8c1e8f61c5
@ -29,6 +29,7 @@ for device in hub.devices:
|
|||||||
- Make it 2/3 compatible
|
- Make it 2/3 compatible
|
||||||
- Add travis unit tests and coverage
|
- Add travis unit tests and coverage
|
||||||
- Give nice documentation examples, don't forget to mention logging
|
- Give nice documentation examples, don't forget to mention logging
|
||||||
|
- make angled motors to be synchronous by default
|
||||||
|
|
||||||
## Links
|
## Links
|
||||||
|
|
||||||
|
@ -10,10 +10,15 @@ class Peripheral(object):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, parent, port):
|
def __init__(self, parent, port):
|
||||||
|
"""
|
||||||
|
:type parent: pylgbst.MoveHub
|
||||||
|
:type port: int
|
||||||
|
"""
|
||||||
super(Peripheral, self).__init__()
|
super(Peripheral, self).__init__()
|
||||||
self.parent = parent
|
self.parent = parent
|
||||||
self.port = port
|
self.port = port
|
||||||
self.working = False
|
self.working = False
|
||||||
|
self._subscribers = []
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "%s on port %s" % (self.__class__.__name__, PORTS[self.port] if self.port in PORTS else 'N/A')
|
return "%s on port %s" % (self.__class__.__name__, PORTS[self.port] if self.port in PORTS else 'N/A')
|
||||||
@ -117,8 +122,10 @@ class ColorDistanceSensor(Peripheral):
|
|||||||
|
|
||||||
class TiltSensor(Peripheral):
|
class TiltSensor(Peripheral):
|
||||||
def subscribe(self, callback):
|
def subscribe(self, callback):
|
||||||
|
params = b'\x00\x01\x00\x00\x00\x01' # full
|
||||||
|
# params = b'\x02\x01\x00\x00\x00\x01' # basic
|
||||||
self._subscribe_on_port(params)
|
self._subscribe_on_port(params)
|
||||||
self._subscribers.append(callback)
|
self._subscribers.append(callback) # TODO: maybe join it into `_subscribe_on_port`
|
||||||
|
|
||||||
|
|
||||||
class Button(Peripheral):
|
class Button(Peripheral):
|
||||||
|
25
test.py
25
test.py
@ -3,9 +3,10 @@ import time
|
|||||||
import unittest
|
import unittest
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
|
|
||||||
from pylgbst import MoveHub, COLOR_RED, LED, EncodedMotor, PORT_AB, Peripheral
|
from pylgbst import MoveHub, COLOR_RED, LED, EncodedMotor, PORT_AB
|
||||||
from pylgbst.comms import Connection, str2hex, hex2str
|
from pylgbst.comms import Connection, str2hex, hex2str
|
||||||
from pylgbst.constants import PORT_LED
|
from pylgbst.constants import PORT_LED, PORT_TILT_SENSOR
|
||||||
|
from pylgbst.peripherals import TiltSensor
|
||||||
|
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
|
|
||||||
@ -51,29 +52,29 @@ class ConnectionMock(Connection):
|
|||||||
|
|
||||||
|
|
||||||
class HubMock(MoveHub):
|
class HubMock(MoveHub):
|
||||||
|
def __init__(self, connection=None):
|
||||||
|
super(HubMock, self).__init__(connection if connection else ConnectionMock())
|
||||||
|
|
||||||
def _wait_for_devices(self):
|
def _wait_for_devices(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class GeneralTest(unittest.TestCase):
|
class GeneralTest(unittest.TestCase):
|
||||||
def test_led(self):
|
def test_led(self):
|
||||||
conn = ConnectionMock()
|
hub = HubMock()
|
||||||
conn.notifications.append((14, '1b0e00 0900 04 39 0227003738'))
|
|
||||||
hub = HubMock(conn)
|
|
||||||
led = LED(hub, PORT_LED)
|
led = LED(hub, PORT_LED)
|
||||||
led.set_color(COLOR_RED)
|
led.set_color(COLOR_RED)
|
||||||
self.assertEquals("0701813211510009", conn.writes[0][1])
|
self.assertEquals("0801813211510009", hub.connection.writes[0][1])
|
||||||
|
|
||||||
def test_tilt_sensor(self):
|
def test_tilt_sensor(self):
|
||||||
conn = ConnectionMock()
|
hub = HubMock()
|
||||||
conn.notifications.append((14, '1b0e000f00043a0128000000000100000001'))
|
hub.tilt_sensor = TiltSensor(hub, PORT_TILT_SENSOR)
|
||||||
hub = HubMock(conn)
|
|
||||||
|
|
||||||
def callback():
|
def callback():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
hub.tilt_sensor.subscribe(callback)
|
hub.tilt_sensor.subscribe(callback)
|
||||||
self.assertEquals("0701813211510009", conn.writes[1][1])
|
self.assertEquals("0a01413a000100000001", hub.connection.writes[0][1])
|
||||||
|
|
||||||
def test_motor(self):
|
def test_motor(self):
|
||||||
conn = ConnectionMock()
|
conn = ConnectionMock()
|
||||||
@ -81,9 +82,9 @@ class GeneralTest(unittest.TestCase):
|
|||||||
hub = HubMock(conn)
|
hub = HubMock(conn)
|
||||||
motor = EncodedMotor(hub, PORT_AB)
|
motor = EncodedMotor(hub, PORT_AB)
|
||||||
motor.timed(1.5)
|
motor.timed(1.5)
|
||||||
self.assertEquals("0c018139110adc056464647f03", conn.writes[0][1])
|
self.assertEquals("0d018139110adc056464647f03", conn.writes[0][1])
|
||||||
motor.angled(90)
|
motor.angled(90)
|
||||||
self.assertEquals("0e018139110c5a0000006464647f03", conn.writes[1][1])
|
self.assertEquals("0f018139110c5a0000006464647f03", conn.writes[1][1])
|
||||||
|
|
||||||
def test_capabilities(self):
|
def test_capabilities(self):
|
||||||
conn = ConnectionMock()
|
conn = ConnectionMock()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user