Skip to content

Commit 01acd5c

Browse files
authored
Merge pull request #17 from caternuson/controller_example
Update examples
2 parents c5a144b + 8595da1 commit 01acd5c

5 files changed

+165
-30
lines changed

docs/examples.rst

+15-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,20 @@ Ensure your device works with this simple test.
77
:caption: examples/bluefruitconnect_simpletest.py
88
:linenos:
99

10-
This test demonstrates controlling an Adafruit Crickit board with Bluetooth.
10+
This example demonstrates receiving button presses from the Control Pad.
1111

12-
.. literalinclude:: ../examples/bluefruitconnect_accelerometer_packet_test.py
13-
:caption: examples/bluefruitconnect_accelerometer_packet_test.py
12+
.. literalinclude:: ../examples/bluefruitconnect_controlpad.py
13+
:caption: examples/bluefruitconnect_controlpad.py
1414
:linenos:
15+
16+
This example demonstrates receiving sensor data from the Controller.
17+
18+
.. literalinclude:: ../examples/bluefruitconnect_sensors.py
19+
:caption: examples/bluefruitconnect_sensors.py
20+
:linenos:
21+
22+
This example demonstrates receiving text from the UART interface.
23+
24+
.. literalinclude:: ../examples/bluefruitconnect_uart.py
25+
:caption: examples/bluefruitconnect_uart.py
26+
:linenos:

examples/bluefruitconnect_accelerometer_packet_test.py

-27
This file was deleted.
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Basic structure example for using the BLE Connect Control Pad
2+
# To use, start this program, and start the Adafruit Bluefruit LE Connect app.
3+
# Connect, and then select Controller-> Control Pad.
4+
5+
from adafruit_ble import BLERadio
6+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
7+
from adafruit_ble.services.nordic import UARTService
8+
from adafruit_bluefruit_connect.packet import Packet
9+
# Only the packet classes that are imported will be known to Packet.
10+
from adafruit_bluefruit_connect.button_packet import ButtonPacket
11+
12+
ble = BLERadio()
13+
uart_server = UARTService()
14+
advertisement = ProvideServicesAdvertisement(uart_server)
15+
16+
while True:
17+
print("WAITING...")
18+
# Advertise when not connected.
19+
ble.start_advertising(advertisement)
20+
while not ble.connected:
21+
pass
22+
23+
# Connected
24+
ble.stop_advertising()
25+
print("CONNECTED")
26+
27+
# Loop and read packets
28+
while ble.connected:
29+
30+
# Keeping trying until a good packet is received
31+
try:
32+
packet = Packet.from_stream(uart_server)
33+
except ValueError:
34+
continue
35+
36+
# Only handle button packets
37+
if isinstance(packet, ButtonPacket) and packet.pressed:
38+
if packet.button == ButtonPacket.UP:
39+
print("Button UP")
40+
if packet.button == ButtonPacket.DOWN:
41+
print("Button DOWN")
42+
if packet.button == ButtonPacket.LEFT:
43+
print("Button LEFT")
44+
if packet.button == ButtonPacket.RIGHT:
45+
print("Button RIGHT")
46+
if packet.button == ButtonPacket.BUTTON_1:
47+
print("Button 1")
48+
if packet.button == ButtonPacket.BUTTON_2:
49+
print("Button 2")
50+
if packet.button == ButtonPacket.BUTTON_3:
51+
print("Button 3")
52+
if packet.button == ButtonPacket.BUTTON_4:
53+
print("Button 4")
54+
55+
# Disconnected
56+
print("DISCONNECTED")

examples/bluefruitconnect_sensors.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Basic structure example for using the BLE Connect Controller sensors
2+
# To use, start this program, and start the Adafruit Bluefruit LE Connect app.
3+
# Connect, and then select Controller and enable the sensors
4+
5+
from adafruit_ble import BLERadio
6+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
7+
from adafruit_ble.services.nordic import UARTService
8+
from adafruit_bluefruit_connect.packet import Packet
9+
# Only the packet classes that are imported will be known to Packet.
10+
from adafruit_bluefruit_connect.accelerometer_packet import AccelerometerPacket
11+
from adafruit_bluefruit_connect.gyro_packet import GyroPacket
12+
from adafruit_bluefruit_connect.location_packet import LocationPacket
13+
from adafruit_bluefruit_connect.magnetometer_packet import MagnetometerPacket
14+
from adafruit_bluefruit_connect.quaternion_packet import QuaternionPacket
15+
16+
ble = BLERadio()
17+
uart_server = UARTService()
18+
advertisement = ProvideServicesAdvertisement(uart_server)
19+
20+
while True:
21+
print("WAITING...")
22+
# Advertise when not connected.
23+
ble.start_advertising(advertisement)
24+
while not ble.connected:
25+
pass
26+
27+
# Connected
28+
ble.stop_advertising()
29+
print("CONNECTED")
30+
31+
# Loop and read packets
32+
while ble.connected:
33+
34+
# Keeping trying until a good packet is received
35+
try:
36+
packet = Packet.from_stream(uart_server)
37+
except ValueError:
38+
continue
39+
40+
# Accelerometer
41+
if isinstance(packet, AccelerometerPacket):
42+
print("Accelerometer:", packet.x, packet.y, packet.z)
43+
44+
# Gyro
45+
if isinstance(packet, GyroPacket):
46+
print("Gyro:", packet.x, packet.y, packet.z)
47+
48+
# Location
49+
if isinstance(packet, LocationPacket):
50+
print("Location:", packet.latitude, packet.longitude, packet.altitude)
51+
52+
# Magnetometer
53+
if isinstance(packet, MagnetometerPacket):
54+
print("Magnetometer", packet.x, packet.y, packet.z)
55+
56+
# Quaternion
57+
if isinstance(packet, QuaternionPacket):
58+
print("Quaternion:", packet.x, packet.y, packet.z, packet.w)
59+
60+
# Disconnected
61+
print("DISCONNECTED")

examples/bluefruitconnect_uart.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Basic example for using the BLE Connect UART
2+
# To use, start this program, and start the Adafruit Bluefruit LE Connect app.
3+
# Connect, and then select UART
4+
5+
from adafruit_ble import BLERadio
6+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
7+
from adafruit_ble.services.nordic import UARTService
8+
9+
ble = BLERadio()
10+
uart_server = UARTService()
11+
advertisement = ProvideServicesAdvertisement(uart_server)
12+
13+
while True:
14+
print("WAITING...")
15+
# Advertise when not connected.
16+
ble.start_advertising(advertisement)
17+
while not ble.connected:
18+
pass
19+
20+
# Connected
21+
ble.stop_advertising()
22+
print("CONNECTED")
23+
24+
# Loop and read packets
25+
while ble.connected:
26+
if uart_server.in_waiting:
27+
raw_bytes = uart_server.read(uart_server.in_waiting)
28+
text = raw_bytes.decode().strip()
29+
print("raw bytes =", raw_bytes)
30+
print("text =", text)
31+
32+
# Disconnected
33+
print("DISCONNECTED")

0 commit comments

Comments
 (0)