Skip to content

Commit d1c0a0f

Browse files
authored
Merge pull request #33 from TonyLHansen/main
Add support for a RawTextPacket, as sent using the UART module in the Ad…
2 parents 13a8e95 + 5f16ad6 commit d1c0a0f

File tree

4 files changed

+100
-1
lines changed

4 files changed

+100
-1
lines changed

adafruit_bluefruit_connect/packet.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,24 @@ def from_stream(cls, stream):
9494
if not start:
9595
# Timeout: nothing read.
9696
return None
97+
9798
if start == b"!":
9899
# Found start of packet.
99100
packet_type = stream.read(1)
100101
if not packet_type:
101102
# Timeout: nothing more read.
102103
return None
103104
break
104-
# Didn't find a packet start. Loop and try again.
105+
106+
# Didn't find a packet start.
107+
raw_text_packet_cls = cls._type_to_class.get(b"RT", None)
108+
# Is RawTextPacket registered?
109+
# If so, read an entire line and pass that to RawTextPacket.
110+
if raw_text_packet_cls:
111+
packet = bytes(start + stream.readline())
112+
return raw_text_packet_cls(packet)
113+
114+
# else loop and try again.
105115

106116
header = bytes(start + packet_type)
107117
packet_class = cls._type_to_class.get(header, None)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# SPDX-FileCopyrightText: 2021 Tony Hansen
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
`adafruit_bluefruit_connect.raw_text_packet`
7+
====================================================
8+
9+
Bluefruit Connect App raw text data packet.
10+
11+
Note that the raw text data packet is different from those used by the
12+
Controller module (e.g. Accelerometer, Control Pad, and Color Picker).
13+
Those use the bytes "!x" (where x is specific to the type of packet),
14+
followed by data specific to the packet, followed by a checksum.
15+
The UART text sender instead sends the bytes followed by a newline.
16+
There is no length indicator, no checksum, etc. Of course, that also
17+
precludes the use of an "!" at the beginning of the string.
18+
19+
Consequently, this packet type is MUCH simpler than the other packet types.
20+
21+
* Author(s): Tony Hansen
22+
23+
"""
24+
25+
from .packet import Packet
26+
27+
28+
class RawTextPacket(Packet):
29+
"""A packet containing a text string."""
30+
31+
_TYPE_HEADER = b"RT"
32+
33+
def __init__(self, text):
34+
"""Construct a RawTextPacket from a binary string."""
35+
if isinstance(text, bytes):
36+
self._text = text.strip()
37+
else:
38+
raise ValueError("Text must be a bytes string")
39+
40+
@property
41+
def text(self):
42+
"""Return the text associated with the object."""
43+
return self._text
44+
45+
46+
# Register this class with the superclass. This allows the user to import only what is needed.
47+
RawTextPacket.register_packet_type()

docs/examples.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,10 @@ This example demonstrates receiving text from the UART interface.
2424
.. literalinclude:: ../examples/bluefruitconnect_uart.py
2525
:caption: examples/bluefruitconnect_uart.py
2626
:linenos:
27+
28+
This example demonstrates receiving both a color (as in simpletest above)
29+
and raw text (using RawTextPacket).
30+
31+
.. literalinclude:: ../examples/bluefruitconnect_simpletest.py
32+
:caption: examples/bluefruitconnect_simpletest.py
33+
:linenos:
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
# Print out the color data from ColorPackets and text data from RawTextPackets.
5+
# To use, start this program, and start the Adafruit Bluefruit LE Connect app.
6+
# Connect, and then select colors on the Controller->Color Picker screen.
7+
# Select text by entering the UART screen and sending a string.
8+
# (Do NOT use a "!" at the start of your string.)
9+
10+
from adafruit_ble import BLERadio
11+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
12+
from adafruit_ble.services.nordic import UARTService
13+
from adafruit_bluefruit_connect.packet import Packet
14+
15+
# Only the packet classes that are imported will be known to Packet.
16+
from adafruit_bluefruit_connect.color_packet import ColorPacket
17+
from adafruit_bluefruit_connect.raw_text_packet import RawTextPacket
18+
19+
ble = BLERadio()
20+
uart_server = UARTService()
21+
advertisement = ProvideServicesAdvertisement(uart_server)
22+
23+
while True:
24+
# Advertise when not connected.
25+
ble.start_advertising(advertisement)
26+
while not ble.connected:
27+
pass
28+
29+
while ble.connected:
30+
packet = Packet.from_stream(uart_server)
31+
if isinstance(packet, ColorPacket):
32+
print(packet.color)
33+
elif isinstance(packet, RawTextPacket):
34+
print("Received Raw Text Packet:")
35+
print(packet.text)

0 commit comments

Comments
 (0)