Skip to content

Commit 63375ed

Browse files
committed
update BLE_snowglobe for CP 5/0-beta.0 BLE API
1 parent 0b6b216 commit 63375ed

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

Snow_Globe_BLE_CPB/ble_snowglobe.py

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,24 @@
55
import neopixel
66
import adafruit_lis3dh
77

8-
from adafruit_ble.uart_server import UARTServer
98
from adafruit_bluefruit_connect.packet import Packet
109
from adafruit_bluefruit_connect.color_packet import ColorPacket
1110
from adafruit_bluefruit_connect.button_packet import ButtonPacket
1211

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+
1319
#===| User Config |==================================================
1420
SNOWGLOBE_NAME = "SNOWGLOBE" # name that will show up on smart device
1521
DEFAULT_ANIMATION = 0 # 0-3, index in ANIMATIONS list
1622
DEFAULT_DURATION = 5 # total seconds to play animation
1723
DEFAULT_SPEED = 0.1 # delay in seconds between updates
1824
DEFAULT_COLOR = 0xFF0000 # hex color value
19-
DEFAULT_SHAKE = 27 # lower number is more sensitive
25+
DEFAULT_SHAKE = 20 # lower number is more sensitive
2026
# you can define more animation functions below
2127
# here, specify the four to be used
2228
ANIMATIONS = ('spin', 'pulse', 'strobe', 'sparkle')
@@ -39,7 +45,10 @@
3945
accelo = adafruit_lis3dh.LIS3DH_I2C(accelo_i2c, address=0x19)
4046

4147
# Setup BLE
42-
uart_server = UARTServer(name=SNOWGLOBE_NAME)
48+
ble = BLERadio()
49+
uart = UARTService()
50+
advertisement = ProvideServicesAdvertisement(uart)
51+
4352

4453
#--| ANIMATIONS |----------------------------------------------------
4554
def spin(config):
@@ -124,26 +133,36 @@ def indicate(event=None):
124133
time.sleep(0.1)
125134

126135
indicate('START')
127-
while True:
128-
uart_server.start_advertising()
129136

130-
# wait for connection
131-
while not uart_server.connected:
132-
# check for shake while waiting
137+
138+
139+
# Are we already advertising?
140+
advertising = False
141+
142+
143+
while True:
144+
# While BLE is *not* connected
145+
while not ble.connected:
133146
if accelo.shake(snow_config['shake'], 5, 0):
134147
play_animation(snow_config)
148+
if not advertising:
149+
ble.start_advertising(advertisement)
150+
advertising = True
135151

136152
# connected
137153
indicate('CONNECTED')
138154

139-
while uart_server.connected:
155+
156+
while ble.connected:
157+
# Once we're connected, we're not advertising any more.
158+
advertising = False
140159

141160
if accelo.shake(snow_config['shake'], 5, 0):
142161
play_animation(snow_config)
143162

144-
if uart_server.in_waiting:
163+
if uart.in_waiting:
145164
try:
146-
packet = Packet.from_stream(uart_server)
165+
packet = Packet.from_stream(uart)
147166
except ValueError:
148167
continue
149168

0 commit comments

Comments
 (0)