Simple test¶
Ensure your device works with this simple test.
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4# Print out the color data from ColorPackets.
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
8from adafruit_ble import BLERadio
9from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
10from adafruit_ble.services.nordic import UARTService
11from adafruit_bluefruit_connect.packet import Packet
12
13# Only the packet classes that are imported will be known to Packet.
14from adafruit_bluefruit_connect.color_packet import ColorPacket
15
16ble = BLERadio()
17uart_server = UARTService()
18advertisement = ProvideServicesAdvertisement(uart_server)
19
20while True:
21 # Advertise when not connected.
22 ble.start_advertising(advertisement)
23 while not ble.connected:
24 pass
25
26 while ble.connected:
27 packet = Packet.from_stream(uart_server)
28 if isinstance(packet, ColorPacket):
29 print(packet.color)
This example demonstrates receiving button presses from the Control Pad.
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4# Basic structure example for using the BLE Connect Control Pad
5# To use, start this program, and start the Adafruit Bluefruit LE Connect app.
6# Connect, and then select Controller-> Control Pad.
7
8from adafruit_ble import BLERadio
9from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
10from adafruit_ble.services.nordic import UARTService
11from adafruit_bluefruit_connect.packet import Packet
12
13# Only the packet classes that are imported will be known to Packet.
14from adafruit_bluefruit_connect.button_packet import ButtonPacket
15
16ble = BLERadio()
17uart_server = UARTService()
18advertisement = ProvideServicesAdvertisement(uart_server)
19
20while 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 # Keeping trying until a good packet is received
34 try:
35 packet = Packet.from_stream(uart_server)
36 except ValueError:
37 continue
38
39 # Only handle button packets
40 if isinstance(packet, ButtonPacket) and packet.pressed:
41 if packet.button == ButtonPacket.UP:
42 print("Button UP")
43 if packet.button == ButtonPacket.DOWN:
44 print("Button DOWN")
45 if packet.button == ButtonPacket.LEFT:
46 print("Button LEFT")
47 if packet.button == ButtonPacket.RIGHT:
48 print("Button RIGHT")
49 if packet.button == ButtonPacket.BUTTON_1:
50 print("Button 1")
51 if packet.button == ButtonPacket.BUTTON_2:
52 print("Button 2")
53 if packet.button == ButtonPacket.BUTTON_3:
54 print("Button 3")
55 if packet.button == ButtonPacket.BUTTON_4:
56 print("Button 4")
57
58 # Disconnected
59 print("DISCONNECTED")
This example demonstrates receiving sensor data from the Controller.
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4# Basic structure example for using the BLE Connect Controller sensors
5# To use, start this program, and start the Adafruit Bluefruit LE Connect app.
6# Connect, and then select Controller and enable the sensors
7
8from adafruit_ble import BLERadio
9from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
10from adafruit_ble.services.nordic import UARTService
11from adafruit_bluefruit_connect.packet import Packet
12
13# Only the packet classes that are imported will be known to Packet.
14from adafruit_bluefruit_connect.accelerometer_packet import AccelerometerPacket
15from adafruit_bluefruit_connect.gyro_packet import GyroPacket
16from adafruit_bluefruit_connect.location_packet import LocationPacket
17from adafruit_bluefruit_connect.magnetometer_packet import MagnetometerPacket
18from adafruit_bluefruit_connect.quaternion_packet import QuaternionPacket
19
20ble = BLERadio()
21uart_server = UARTService()
22advertisement = ProvideServicesAdvertisement(uart_server)
23
24while True:
25 print("WAITING...")
26 # Advertise when not connected.
27 ble.start_advertising(advertisement)
28 while not ble.connected:
29 pass
30
31 # Connected
32 ble.stop_advertising()
33 print("CONNECTED")
34
35 # Loop and read packets
36 while ble.connected:
37 # Keeping trying until a good packet is received
38 try:
39 packet = Packet.from_stream(uart_server)
40 except ValueError:
41 continue
42
43 # Accelerometer
44 if isinstance(packet, AccelerometerPacket):
45 print("Accelerometer:", packet.x, packet.y, packet.z)
46
47 # Gyro
48 if isinstance(packet, GyroPacket):
49 print("Gyro:", packet.x, packet.y, packet.z)
50
51 # Location
52 if isinstance(packet, LocationPacket):
53 print("Location:", packet.latitude, packet.longitude, packet.altitude)
54
55 # Magnetometer
56 if isinstance(packet, MagnetometerPacket):
57 print("Magnetometer", packet.x, packet.y, packet.z)
58
59 # Quaternion
60 if isinstance(packet, QuaternionPacket):
61 print("Quaternion:", packet.x, packet.y, packet.z, packet.w)
62
63 # Disconnected
64 print("DISCONNECTED")
This example demonstrates receiving text from the UART interface.
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4# Basic example for using the BLE Connect UART
5# To use, start this program, and start the Adafruit Bluefruit LE Connect app.
6# Connect, and then select UART. Any text received FROM the connected device
7# will be displayed. Periodically, text is sent TO the connected device.
8
9import time
10from adafruit_ble import BLERadio
11from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
12from adafruit_ble.services.nordic import UARTService
13
14SEND_RATE = 10 # how often in seconds to send text
15
16ble = BLERadio()
17uart_server = UARTService()
18advertisement = ProvideServicesAdvertisement(uart_server)
19
20count = 0
21while True:
22 print("WAITING...")
23 # Advertise when not connected.
24 ble.start_advertising(advertisement)
25 while not ble.connected:
26 pass
27
28 # Connected
29 ble.stop_advertising()
30 print("CONNECTED")
31
32 # Loop and read packets
33 last_send = time.monotonic()
34 while ble.connected:
35 # INCOMING (RX) check for incoming text
36 if uart_server.in_waiting:
37 raw_bytes = uart_server.read(uart_server.in_waiting)
38 text = raw_bytes.decode().strip()
39 # print("raw bytes =", raw_bytes)
40 print("RX:", text)
41 # OUTGOING (TX) periodically send text
42 if time.monotonic() - last_send > SEND_RATE:
43 text = "COUNT = {}\r\n".format(count)
44 print("TX:", text.strip())
45 uart_server.write(text.encode())
46 count += 1
47 last_send = time.monotonic()
48
49 # Disconnected
50 print("DISCONNECTED")
This example demonstrates receiving both a color (as in simpletest above) and raw text (using RawTextPacket).
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4# Print out the color data from ColorPackets.
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
8from adafruit_ble import BLERadio
9from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
10from adafruit_ble.services.nordic import UARTService
11from adafruit_bluefruit_connect.packet import Packet
12
13# Only the packet classes that are imported will be known to Packet.
14from adafruit_bluefruit_connect.color_packet import ColorPacket
15
16ble = BLERadio()
17uart_server = UARTService()
18advertisement = ProvideServicesAdvertisement(uart_server)
19
20while True:
21 # Advertise when not connected.
22 ble.start_advertising(advertisement)
23 while not ble.connected:
24 pass
25
26 while ble.connected:
27 packet = Packet.from_stream(uart_server)
28 if isinstance(packet, ColorPacket):
29 print(packet.color)