|
| 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 TextPackets. |
| 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 in in a string. |
| 8 | +# (Do NOT use a "!" as part 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.text_packet import TextPacket |
| 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, TextPacket): |
| 34 | + print("Received Text Packet:") |
| 35 | + print(packet.text) |
| 36 | + |
0 commit comments