Skip to content

Commit 59fa8a2

Browse files
committed
add button constants; improve examples and doc
1 parent 4c90cc1 commit 59fa8a2

File tree

6 files changed

+45
-67
lines changed

6 files changed

+45
-67
lines changed

README.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,20 @@ This is easily achieved by downloading
2828
Usage Example
2929
=============
3030

31+
Normally this library is used with the
32+
`Adafruit_CircuitPython_BluefruitConnect
33+
<https://github.com/adafruit/Adafruit_CircuitPython_BluefruitConnnect>`_
34+
library
35+
(``adafruit_bluefruit_connect``). The included examples use that library.
36+
Below is a simple standalone example.
37+
3138
.. code-block:: python
3239
3340
from adafruit_bluefruit_connect.color_packet import ColorPacket
3441
from adafruit_bluefruit_connect.gyro_packet import GyroPacket
3542
43+
# [uart setup omitted]
44+
3645
color_packet = ColorPacket((70,75,80))
3746
gyro_packet = GyroPacket.from_bytes(packet_buf)
3847
uart.write(gyro_packet.to_bytes())

adafruit_bluefruit_connect/button_packet.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,25 @@
3535
from .packet import Packet
3636

3737
class ButtonPacket(Packet):
38-
"""A packet containing a button name and its state"""
38+
"""A packet containing a button name and its state."""
39+
40+
B_1 = '1'
41+
"""Code for Button 1 on the Bluefruit LE Connect app Control Pad screen."""
42+
B_2 = '2'
43+
"""Button 2."""
44+
B_3 = '3'
45+
"""Button 3."""
46+
B_4 = '4'
47+
"""Button 4"""
48+
#pylint: disable= invalid-name
49+
UP = '5'
50+
"""Up Button."""
51+
DOWN = '6'
52+
"""Down Button."""
53+
LEFT = '7'
54+
"""Left Button."""
55+
RIGHT = '8'
56+
"""Right Button."""
3957

4058
_FMT_PARSE = '<xxssx'
4159
PACKET_LENGTH = struct.calcsize(_FMT_PARSE)
@@ -76,7 +94,7 @@ def to_bytes(self):
7694

7795
@property
7896
def button(self):
79-
"""Button designator: a single character string (not bytes)."""
97+
"""A single character string (not bytes) specifying the button."""
8098
return self._button
8199

82100
@property

adafruit_bluefruit_connect/color_packet.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ def __init__(self, color):
5858

5959
@classmethod
6060
def parse_private(cls, packet):
61-
"""Construct a ColorPacket from an incoming packet."""
61+
"""Construct a ColorPacket from an incoming packet.
62+
Do not call this directly; call Packet.from_bytes() instead.
63+
"""
6264
return cls(struct.unpack(cls._FMT_PARSE, packet))
6365

6466
def to_bytes(self):

docs/examples.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ Ensure your device works with this simple test.
99

1010
This test demonstrates controlling an Adafruit Crickit board with Bluetooth.
1111

12-
.. literalinclude:: ../examples/bluefruit_connect_crickit_test.py
13-
:caption: examples/bluefruit_connect_crickit_test.py
12+
.. literalinclude:: ../examples/bluefruit_connect_accelerometer_packet_test.py
13+
:caption: examples/bluefruit_connect_accelerometer_packet_test.py
1414
:linenos:

examples/bluefruit_connect_crickit_test.py

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# Print out the color data from a ColorPacket.
1+
# Print out the color data from ColorPackets.
2+
# To use, start this program, and start the Adafruit Bluefruit LE Connect app.
3+
# Connect, and then select colors on the Controller->Color Picker screen.
24

35
from adafruit_ble.uart import UARTServer
46
from adafruit_bluefruit_connect.packet import Packet
@@ -7,18 +9,13 @@
79

810
uart_server = UARTServer()
911

10-
advertising_now = False
11-
1212
while True:
13-
if not uart_server.connected:
14-
if not advertising_now:
15-
uart_server.start_advertising()
16-
advertising_now = True
17-
continue
18-
19-
# Connected, so no longer advertising
20-
advertising_now = False
13+
# Advertise when not connected.
14+
uart_server.start_advertising()
15+
while not uart_server.connected:
16+
pass
2117

22-
packet = Packet.from_stream(uart_server)
23-
if isinstance(packet, ColorPacket):
24-
print(packet.color)
18+
while uart_server.connected:
19+
packet = Packet.from_stream(uart_server)
20+
if isinstance(packet, ColorPacket):
21+
print(packet.color)

0 commit comments

Comments
 (0)