|
5 | 5 | import neopixel
|
6 | 6 | import adafruit_lis3dh
|
7 | 7 |
|
8 |
| -from adafruit_ble.uart_server import UARTServer |
9 | 8 | from adafruit_bluefruit_connect.packet import Packet
|
10 | 9 | from adafruit_bluefruit_connect.color_packet import ColorPacket
|
11 | 10 | from adafruit_bluefruit_connect.button_packet import ButtonPacket
|
12 | 11 |
|
| 12 | + |
| 13 | +from adafruit_ble import BLERadio |
| 14 | +from adafruit_ble.advertising.standard import ProvideServicesAdvertisement |
| 15 | +from adafruit_ble.services.nordic import UARTService |
| 16 | + |
| 17 | + |
| 18 | + |
13 | 19 | #===| User Config |==================================================
|
14 | 20 | SNOWGLOBE_NAME = "SNOWGLOBE" # name that will show up on smart device
|
15 | 21 | DEFAULT_ANIMATION = 0 # 0-3, index in ANIMATIONS list
|
16 | 22 | DEFAULT_DURATION = 5 # total seconds to play animation
|
17 | 23 | DEFAULT_SPEED = 0.1 # delay in seconds between updates
|
18 | 24 | DEFAULT_COLOR = 0xFF0000 # hex color value
|
19 |
| -DEFAULT_SHAKE = 27 # lower number is more sensitive |
| 25 | +DEFAULT_SHAKE = 20 # lower number is more sensitive |
20 | 26 | # you can define more animation functions below
|
21 | 27 | # here, specify the four to be used
|
22 | 28 | ANIMATIONS = ('spin', 'pulse', 'strobe', 'sparkle')
|
|
39 | 45 | accelo = adafruit_lis3dh.LIS3DH_I2C(accelo_i2c, address=0x19)
|
40 | 46 |
|
41 | 47 | # Setup BLE
|
42 |
| -uart_server = UARTServer(name=SNOWGLOBE_NAME) |
| 48 | +ble = BLERadio() |
| 49 | +uart = UARTService() |
| 50 | +advertisement = ProvideServicesAdvertisement(uart) |
| 51 | +ble._adapter.name = SNOWGLOBE_NAME #pylint: disable=protected-access |
43 | 52 |
|
44 | 53 | #--| ANIMATIONS |----------------------------------------------------
|
45 | 54 | def spin(config):
|
@@ -124,26 +133,35 @@ def indicate(event=None):
|
124 | 133 | time.sleep(0.1)
|
125 | 134 |
|
126 | 135 | indicate('START')
|
127 |
| -while True: |
128 |
| - uart_server.start_advertising() |
129 | 136 |
|
130 |
| - # wait for connection |
131 |
| - while not uart_server.connected: |
132 |
| - # check for shake while waiting |
| 137 | + |
| 138 | +# Are we already advertising? |
| 139 | +advertising = False |
| 140 | + |
| 141 | + |
| 142 | +while True: |
| 143 | + # While BLE is *not* connected |
| 144 | + while not ble.connected: |
133 | 145 | if accelo.shake(snow_config['shake'], 5, 0):
|
134 | 146 | play_animation(snow_config)
|
| 147 | + if not advertising: |
| 148 | + ble.start_advertising(advertisement) |
| 149 | + advertising = True |
135 | 150 |
|
136 | 151 | # connected
|
137 | 152 | indicate('CONNECTED')
|
138 | 153 |
|
139 |
| - while uart_server.connected: |
| 154 | + |
| 155 | + while ble.connected: |
| 156 | + # Once we're connected, we're not advertising any more. |
| 157 | + advertising = False |
140 | 158 |
|
141 | 159 | if accelo.shake(snow_config['shake'], 5, 0):
|
142 | 160 | play_animation(snow_config)
|
143 | 161 |
|
144 |
| - if uart_server.in_waiting: |
| 162 | + if uart.in_waiting: |
145 | 163 | try:
|
146 |
| - packet = Packet.from_stream(uart_server) |
| 164 | + packet = Packet.from_stream(uart) |
147 | 165 | except ValueError:
|
148 | 166 | continue
|
149 | 167 |
|
|
0 commit comments