|
1 | 1 | import board
|
| 2 | +import neopixel |
| 3 | + |
| 4 | +from adafruit_bluefruit_connect.packet import Packet |
| 5 | +from adafruit_bluefruit_connect.color_packet import ColorPacket |
| 6 | + |
| 7 | + |
2 | 8 | from adafruit_ble import BLERadio
|
3 | 9 | from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
|
4 | 10 | from adafruit_ble.services.nordic import UARTService
|
| 11 | + |
5 | 12 | from adafruit_airlift.esp32 import ESP32
|
6 | 13 |
|
| 14 | + |
7 | 15 | # If you are using an AirLift FeatherWing or AirLift Bitsy Add-On,
|
8 |
| -# use the pin settings below: |
| 16 | +# use the pin settings below. |
| 17 | +# If you are using an AirLift Breakout, check that these |
| 18 | +# choices match the wiring to your microcontroller board, |
| 19 | +# or change them as appropriate. |
9 | 20 | esp32 = ESP32(
|
10 | 21 | reset=board.D12,
|
11 | 22 | gpio0=board.D10,
|
|
15 | 26 | rx=board.RX,
|
16 | 27 | )
|
17 | 28 |
|
18 |
| -# If you are using an AirLift Breakout, comment out the DEFAULT lines |
19 |
| -# above and uncomment the lines below: |
20 |
| -# esp32 = ESP32( |
21 |
| -# reset=board.D12, |
22 |
| -# gpio0=board.D10, |
23 |
| -# busy=board.D11, |
24 |
| -# chip_select=board.D13, |
25 |
| -# tx=board.TX, |
26 |
| -# rx=board.RX, |
27 |
| -# ) |
28 |
| - |
29 |
| - |
30 | 29 | adapter = esp32.start_bluetooth()
|
31 | 30 |
|
32 | 31 | ble = BLERadio(adapter)
|
33 |
| -uart = UARTService() |
34 |
| -advertisement = ProvideServicesAdvertisement(uart) |
| 32 | +uart_service = UARTService() |
| 33 | +advertisement = ProvideServicesAdvertisement(uart_service) |
| 34 | + |
| 35 | +pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.1) |
35 | 36 |
|
36 | 37 | while True:
|
| 38 | + # Advertise when not connected. |
37 | 39 | ble.start_advertising(advertisement)
|
38 |
| - print("waiting to connect") |
39 | 40 | while not ble.connected:
|
40 | 41 | pass
|
41 |
| - print("connected: trying to read input") |
| 42 | + |
42 | 43 | while ble.connected:
|
43 |
| - # Returns b'' if nothing was read. |
44 |
| - one_byte = uart.read(1) |
45 |
| - if one_byte: |
46 |
| - print(one_byte) |
47 |
| - uart.write(one_byte) |
| 44 | + if uart_service.in_waiting: |
| 45 | + packet = Packet.from_stream(uart_service) |
| 46 | + if isinstance(packet, ColorPacket): |
| 47 | + print(packet.color) |
| 48 | + pixels.fill(packet.color) |
0 commit comments