diff --git a/adafruit_bluefruit_connect/packet.py b/adafruit_bluefruit_connect/packet.py index 5e8ead7..b3b8d3c 100644 --- a/adafruit_bluefruit_connect/packet.py +++ b/adafruit_bluefruit_connect/packet.py @@ -94,6 +94,7 @@ def from_stream(cls, stream): if not start: # Timeout: nothing read. return None + if start == b"!": # Found start of packet. packet_type = stream.read(1) @@ -101,7 +102,16 @@ def from_stream(cls, stream): # Timeout: nothing more read. return None break - # Didn't find a packet start. Loop and try again. + + # Didn't find a packet start. + raw_text_packet_cls = cls._type_to_class.get(b"RT", None) + # Is RawTextPacket registered? + # If so, read an entire line and pass that to RawTextPacket. + if raw_text_packet_cls: + packet = bytes(start + stream.readline()) + return raw_text_packet_cls(packet) + + # else loop and try again. header = bytes(start + packet_type) packet_class = cls._type_to_class.get(header, None) diff --git a/adafruit_bluefruit_connect/raw_text_packet.py b/adafruit_bluefruit_connect/raw_text_packet.py new file mode 100644 index 0000000..729eea0 --- /dev/null +++ b/adafruit_bluefruit_connect/raw_text_packet.py @@ -0,0 +1,47 @@ +# SPDX-FileCopyrightText: 2021 Tony Hansen +# +# SPDX-License-Identifier: MIT + +""" +`adafruit_bluefruit_connect.raw_text_packet` +==================================================== + +Bluefruit Connect App raw text data packet. + +Note that the raw text data packet is different from those used by the +Controller module (e.g. Accelerometer, Control Pad, and Color Picker). +Those use the bytes "!x" (where x is specific to the type of packet), +followed by data specific to the packet, followed by a checksum. +The UART text sender instead sends the bytes followed by a newline. +There is no length indicator, no checksum, etc. Of course, that also +precludes the use of an "!" at the beginning of the string. + +Consequently, this packet type is MUCH simpler than the other packet types. + +* Author(s): Tony Hansen + +""" + +from .packet import Packet + + +class RawTextPacket(Packet): + """A packet containing a text string.""" + + _TYPE_HEADER = b"RT" + + def __init__(self, text): + """Construct a RawTextPacket from a binary string.""" + if isinstance(text, bytes): + self._text = text.strip() + else: + raise ValueError("Text must be a bytes string") + + @property + def text(self): + """Return the text associated with the object.""" + return self._text + + +# Register this class with the superclass. This allows the user to import only what is needed. +RawTextPacket.register_packet_type() diff --git a/docs/examples.rst b/docs/examples.rst index a672dc8..2e58955 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -24,3 +24,10 @@ This example demonstrates receiving text from the UART interface. .. literalinclude:: ../examples/bluefruitconnect_uart.py :caption: examples/bluefruitconnect_uart.py :linenos: + +This example demonstrates receiving both a color (as in simpletest above) +and raw text (using RawTextPacket). + +.. literalinclude:: ../examples/bluefruitconnect_simpletest.py + :caption: examples/bluefruitconnect_simpletest.py + :linenos: diff --git a/examples/bluefruitconnect_simpletest2.py b/examples/bluefruitconnect_simpletest2.py new file mode 100644 index 0000000..ecb2172 --- /dev/null +++ b/examples/bluefruitconnect_simpletest2.py @@ -0,0 +1,35 @@ +# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries +# SPDX-License-Identifier: MIT + +# Print out the color data from ColorPackets and text data from RawTextPackets. +# To use, start this program, and start the Adafruit Bluefruit LE Connect app. +# Connect, and then select colors on the Controller->Color Picker screen. +# Select text by entering the UART screen and sending a string. +# (Do NOT use a "!" at the start of your string.) + +from adafruit_ble import BLERadio +from adafruit_ble.advertising.standard import ProvideServicesAdvertisement +from adafruit_ble.services.nordic import UARTService +from adafruit_bluefruit_connect.packet import Packet + +# Only the packet classes that are imported will be known to Packet. +from adafruit_bluefruit_connect.color_packet import ColorPacket +from adafruit_bluefruit_connect.raw_text_packet import RawTextPacket + +ble = BLERadio() +uart_server = UARTService() +advertisement = ProvideServicesAdvertisement(uart_server) + +while True: + # Advertise when not connected. + ble.start_advertising(advertisement) + while not ble.connected: + pass + + while ble.connected: + packet = Packet.from_stream(uart_server) + if isinstance(packet, ColorPacket): + print(packet.color) + elif isinstance(packet, RawTextPacket): + print("Received Raw Text Packet:") + print(packet.text)