Skip to content

Add support for a TextPacket, as sent using the UART module in the Ad… #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion adafruit_bluefruit_connect/packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,24 @@ 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)
if not packet_type:
# 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)
Expand Down
47 changes: 47 additions & 0 deletions adafruit_bluefruit_connect/raw_text_packet.py
Original file line number Diff line number Diff line change
@@ -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()
7 changes: 7 additions & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
35 changes: 35 additions & 0 deletions examples/bluefruitconnect_simpletest2.py
Original file line number Diff line number Diff line change
@@ -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)