|
1 | 1 | # BLE Crickit Light Switch
|
2 | 2 | # Use with the Adafruit BlueFruit LE Connect app
|
3 |
| -# Works with CircuitPython 4.0.0-beta.1 and later |
| 3 | +# Works with CircuitPython 5.0.0-beta.0 and later |
4 | 4 | # running on an nRF52840 Feather board and Crickit FeatherWing
|
5 | 5 | # micro servo, 3D printed switch actuator
|
6 | 6 |
|
|
10 | 10 | import digitalio
|
11 | 11 |
|
12 | 12 | from adafruit_crickit import crickit
|
13 |
| -from adafruit_ble.uart import UARTServer |
| 13 | + |
| 14 | +from adafruit_ble import BLERadio |
| 15 | +from adafruit_ble.advertising.standard import ProvideServicesAdvertisement |
| 16 | +from adafruit_ble.services.nordic import UARTService |
14 | 17 |
|
15 | 18 | from adafruit_bluefruit_connect.packet import Packet
|
16 | 19 | # Only the packet classes that are imported will be known to Packet.
|
|
22 | 25 | blue_led.direction = digitalio.Direction.OUTPUT
|
23 | 26 | red_led.direction = digitalio.Direction.OUTPUT
|
24 | 27 |
|
25 |
| -uart_server = UARTServer() |
| 28 | +ble = BLERadio() |
| 29 | +uart_service = UARTService() |
| 30 | +advertisement = ProvideServicesAdvertisement(uart_service) |
26 | 31 |
|
27 | 32 | UP_ANGLE = 180
|
28 | 33 | NEUTRAL_ANGLE = 120
|
|
35 | 40 | print("Use Adafruit Bluefruit app to connect")
|
36 | 41 | while True:
|
37 | 42 | blue_led.value = False
|
38 |
| - uart_server.start_advertising() |
| 43 | + ble.start_advertising(advertisement) |
39 | 44 |
|
40 |
| - while not uart_server.connected: |
| 45 | + while not ble.connected: |
41 | 46 | # Wait for a connection.
|
42 | 47 | pass
|
43 | 48 | blue_led.value = True # turn on blue LED when connected
|
44 |
| - while uart_server.connected: |
45 |
| - if uart_server.in_waiting: |
| 49 | + while ble.connected: |
| 50 | + if uart_service.in_waiting: |
46 | 51 | # Packet is arriving.
|
47 | 52 | red_led.value = False # turn off red LED
|
48 |
| - packet = Packet.from_stream(uart_server) |
| 53 | + packet = Packet.from_stream(uart_service) |
49 | 54 | if isinstance(packet, ButtonPacket) and packet.pressed:
|
50 | 55 | red_led.value = True # blink to show a packet has been received
|
51 | 56 | if packet.button == ButtonPacket.UP and angle != UP_ANGLE: # UP button pressed
|
|
0 commit comments