1
0
mirror of https://github.com/undera/pylgbst.git synced 2020-11-18 19:37:26 -08:00

Fix tests

This commit is contained in:
Andrey Pohilko 2017-09-14 14:51:13 +03:00
parent 539715af8b
commit cea34b292a
3 changed files with 22 additions and 17 deletions

View File

@ -15,6 +15,7 @@ from pylgbst.constants import LEGO_MOVE_HUB
log = logging.getLogger('transport')
# could use `six` here, but just for 1 function
if sys.version_info[0] == 2:
def str2hex(data):
return data.encode("hex")
@ -26,6 +27,10 @@ if sys.version_info[0] == 2:
def get_byte(seq, index):
return ord(seq[index])
def int2byte(val):
return chr(val)
else:
import binascii
@ -42,6 +47,10 @@ else:
return seq[index]
def int2byte(val):
return bytes(val, )
# noinspection PyMethodOverriding
class Requester(GATTRequester):
"""

View File

@ -7,7 +7,7 @@ MOVE_HUB_HARDWARE_UUID = '00001624-1212-efde-1623-785feabcd123'
ENABLE_NOTIFICATIONS_HANDLE = 0x000f
ENABLE_NOTIFICATIONS_VALUE = b'\x01\x00'
PACKET_VER = b'\x01'
PACKET_VER = 0x01
# COLORS
COLOR_OFF = 0x00

View File

@ -2,7 +2,7 @@ import logging
import struct
import time
from pylgbst import get_byte
from pylgbst import get_byte, int2byte
from pylgbst.constants import *
log = logging.getLogger('peripherals')
@ -28,9 +28,10 @@ class Peripheral(object):
return "%s on port %s" % (self.__class__.__name__, PORTS[self.port] if self.port in PORTS else 'N/A')
def _write_to_hub(self, msg_type, params):
cmd = PACKET_VER + chr(msg_type) + chr(self.port)
cmd = int2byte(PACKET_VER) + int2byte(msg_type) + int2byte(self.port)
cmd += params
self.parent.connection.write(MOVE_HUB_HARDWARE_HANDLE, chr(len(cmd) + 1) + cmd) # should we +1 cmd len here?
self.parent.connection.write(MOVE_HUB_HARDWARE_HANDLE,
int2byte(len(cmd) + 1) + cmd) # should we +1 cmd len here?
def _set_port_val(self, value):
# FIXME: became obsolete
@ -56,7 +57,7 @@ class LED(Peripheral):
if color not in COLORS:
raise ValueError("Color %s is not in list of available colors" % color)
cmd = '\x11\x51\x00' + chr(color)
cmd = '\x11\x51\x00' + int2byte(color)
self._set_port_val(cmd)
@ -86,9 +87,9 @@ class EncodedMotor(Peripheral):
# set for port
command = self.MOVEMENT_TYPE + command
command += chr(self._speed_abs(speed_primary))
command += int2byte(self._speed_abs(speed_primary))
if self.port == PORT_AB:
command += chr(self._speed_abs(speed_secondary))
command += int2byte(self._speed_abs(speed_secondary))
command += self.TRAILER
@ -124,10 +125,6 @@ class EncodedMotor(Peripheral):
# TODO: how to tell when motor has stopped?
class ColorDistanceSensor(Peripheral):
pass
class TiltSensor(Peripheral):
def __init__(self, parent, port):
super(TiltSensor, self).__init__(parent, port)
@ -135,7 +132,7 @@ class TiltSensor(Peripheral):
def _switch_mode(self, mode):
self.mode = mode
self._subscribe_on_port(chr(mode) + b'\x01\x00\x00\x00\x01')
self._subscribe_on_port(int2byte(mode) + b'\x01\x00\x00\x00\x01')
def subscribe(self, callback, mode=TILT_SENSOR_MODE_BASIC):
if mode not in (TILT_SENSOR_MODE_BASIC, TILT_SENSOR_MODE_2AXIS, TILT_SENSOR_MODE_FULL):
@ -144,11 +141,6 @@ class TiltSensor(Peripheral):
self._switch_mode(mode)
self._subscribers.add(callback) # TODO: maybe join it into `_subscribe_on_port`
# 1b0e00 0a00 47 3a020100000001
# 1b0e00 0a00 47 3a020100000001
# 1b0e000a00 47 3a030100000001 - sent finish?
def unsubscribe(self, callback):
self._subscribers.remove(callback)
if not self._subscribers:
@ -174,6 +166,10 @@ class TiltSensor(Peripheral):
return val
class ColorDistanceSensor(Peripheral):
pass
class Button(Peripheral):
def __init__(self, parent):
super(Button, self).__init__(parent, 0)