From 59fa8a2e03103ab8209f4f8adf0274455af63c4d Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Fri, 1 Feb 2019 10:52:12 -0500 Subject: [PATCH 1/3] add button constants; improve examples and doc --- README.rst | 9 ++++ adafruit_bluefruit_connect/button_packet.py | 22 +++++++++- adafruit_bluefruit_connect/color_packet.py | 4 +- docs/examples.rst | 4 +- examples/bluefruit_connect_crickit_test.py | 48 --------------------- examples/bluefruit_connect_simpletest.py | 25 +++++------ 6 files changed, 45 insertions(+), 67 deletions(-) delete mode 100644 examples/bluefruit_connect_crickit_test.py diff --git a/README.rst b/README.rst index 92d857a..69faa2c 100644 --- a/README.rst +++ b/README.rst @@ -28,11 +28,20 @@ This is easily achieved by downloading Usage Example ============= +Normally this library is used with the +`Adafruit_CircuitPython_BluefruitConnect +`_ +library +(``adafruit_bluefruit_connect``). The included examples use that library. +Below is a simple standalone example. + .. code-block:: python from adafruit_bluefruit_connect.color_packet import ColorPacket from adafruit_bluefruit_connect.gyro_packet import GyroPacket + # [uart setup omitted] + color_packet = ColorPacket((70,75,80)) gyro_packet = GyroPacket.from_bytes(packet_buf) uart.write(gyro_packet.to_bytes()) diff --git a/adafruit_bluefruit_connect/button_packet.py b/adafruit_bluefruit_connect/button_packet.py index cead0ff..bb09477 100644 --- a/adafruit_bluefruit_connect/button_packet.py +++ b/adafruit_bluefruit_connect/button_packet.py @@ -35,7 +35,25 @@ from .packet import Packet class ButtonPacket(Packet): - """A packet containing a button name and its state""" + """A packet containing a button name and its state.""" + + B_1 = '1' + """Code for Button 1 on the Bluefruit LE Connect app Control Pad screen.""" + B_2 = '2' + """Button 2.""" + B_3 = '3' + """Button 3.""" + B_4 = '4' + """Button 4""" + #pylint: disable= invalid-name + UP = '5' + """Up Button.""" + DOWN = '6' + """Down Button.""" + LEFT = '7' + """Left Button.""" + RIGHT = '8' + """Right Button.""" _FMT_PARSE = 'Color Picker screen. from adafruit_ble.uart import UARTServer from adafruit_bluefruit_connect.packet import Packet @@ -7,18 +9,13 @@ uart_server = UARTServer() -advertising_now = False - while True: - if not uart_server.connected: - if not advertising_now: - uart_server.start_advertising() - advertising_now = True - continue - - # Connected, so no longer advertising - advertising_now = False + # Advertise when not connected. + uart_server.start_advertising() + while not uart_server.connected: + pass - packet = Packet.from_stream(uart_server) - if isinstance(packet, ColorPacket): - print(packet.color) + while uart_server.connected: + packet = Packet.from_stream(uart_server) + if isinstance(packet, ColorPacket): + print(packet.color) From 58cd9e6ce73c92a15289ac1f19ba6651ede7739e Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Wed, 20 Feb 2019 17:42:30 -0500 Subject: [PATCH 2/3] Make packet start finding more robust; fix bug in LocationPacket --- adafruit_bluefruit_connect/location_packet.py | 1 - adafruit_bluefruit_connect/packet.py | 31 +++++++++++++------ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/adafruit_bluefruit_connect/location_packet.py b/adafruit_bluefruit_connect/location_packet.py index c6d5932..c22954e 100644 --- a/adafruit_bluefruit_connect/location_packet.py +++ b/adafruit_bluefruit_connect/location_packet.py @@ -40,7 +40,6 @@ class LocationPacket(Packet): PACKET_LENGTH = struct.calcsize(_FMT_PARSE) # _FMT_CONSTRUCT doesn't include the trailing checksum byte. _FMT_CONSTRUCT = '<2sfff' - PACKET_LENGTH = struct.calcsize(_FMT_CONSTRUCT) _TYPE_HEADER = b'!L' def __init__(self, latitude, longitude, altitude): diff --git a/adafruit_bluefruit_connect/packet.py b/adafruit_bluefruit_connect/packet.py index 9222224..1176bee 100644 --- a/adafruit_bluefruit_connect/packet.py +++ b/adafruit_bluefruit_connect/packet.py @@ -73,10 +73,9 @@ def from_bytes(cls, packet): """ if len(packet) < 3: raise ValueError("Packet too short") - header = packet[0:2] - packet_class = cls._type_to_class.get(header, None) + packet_class = cls._type_to_class.get(packet[0:2], None) if not packet_class: - raise ValueError("Unknown packet header '{}'".format(header)) + raise ValueError("Unregistered packet type {}".format(header)) # In case this was called from a subclass, make sure the parsed # type matches up with the current class. @@ -103,14 +102,28 @@ def from_stream(cls, stream): :param stream stream: an input stream that provides standard stream read operations, such as ``ble.UARTServer`` or ``busio.UART``. """ - header = stream.read(2) - if len(header) != 2 or header[0] != ord(b'!'): - # Remove any other junk already read. - stream.reset_input_buffer() - return None + # Loop looking for a b'!' packet start. If the buffer has overflowed, + # or there's been some other problem, we may need to skip some characters + # to get to a packet start. + while True: + start = stream.read(1) + if not start: + # Timeout: nothing read. + return None + if start == b'!': + # Found start of packet. + packet_type = stream.read(1) + if not packet_type: + # Timeout: nothing more read. + return None + else: + break; + # Didn't find a packet start. Loop and try again. + + header = start + packet_type packet_class = cls._type_to_class.get(header, None) if not packet_class: - raise ValueError("Unknown packet header {}".format(header)) + raise ValueError("Unregistered packet type {}".format(header)) packet = header + stream.read(packet_class.PACKET_LENGTH - 2) return cls.from_bytes(packet) From 1347bbec83de75b65036c2b04ed07ccf3f5e36ac Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 21 Feb 2019 12:30:47 -0500 Subject: [PATCH 3/3] pylint noticed a real problem, for once --- adafruit_bluefruit_connect/packet.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_bluefruit_connect/packet.py b/adafruit_bluefruit_connect/packet.py index 1176bee..f118214 100644 --- a/adafruit_bluefruit_connect/packet.py +++ b/adafruit_bluefruit_connect/packet.py @@ -75,7 +75,7 @@ def from_bytes(cls, packet): raise ValueError("Packet too short") packet_class = cls._type_to_class.get(packet[0:2], None) if not packet_class: - raise ValueError("Unregistered packet type {}".format(header)) + raise ValueError("Unregistered packet type {}".format(packet[0:2])) # In case this was called from a subclass, make sure the parsed # type matches up with the current class. @@ -117,7 +117,7 @@ def from_stream(cls, stream): # Timeout: nothing more read. return None else: - break; + break # Didn't find a packet start. Loop and try again. header = start + packet_type