Skip to content

Commit c048d1a

Browse files
authored
Merge pull request #950 from jerryneedell/jerryn_snowglobe
Update Snow_Globe_BLE_CPB for CP 5.0 Beta _bleio
2 parents 7b2dd56 + 15537e2 commit c048d1a

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

Snow_Globe_BLE_CPB/ble_snowglobe.py

Lines changed: 29 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+
ble._adapter.name = SNOWGLOBE_NAME #pylint: disable=protected-access
4352

4453
#--| ANIMATIONS |----------------------------------------------------
4554
def spin(config):
@@ -124,26 +133,35 @@ 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+
# Are we already advertising?
139+
advertising = False
140+
141+
142+
while True:
143+
# While BLE is *not* connected
144+
while not ble.connected:
133145
if accelo.shake(snow_config['shake'], 5, 0):
134146
play_animation(snow_config)
147+
if not advertising:
148+
ble.start_advertising(advertisement)
149+
advertising = True
135150

136151
# connected
137152
indicate('CONNECTED')
138153

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

141159
if accelo.shake(snow_config['shake'], 5, 0):
142160
play_animation(snow_config)
143161

144-
if uart_server.in_waiting:
162+
if uart.in_waiting:
145163
try:
146-
packet = Packet.from_stream(uart_server)
164+
packet = Packet.from_stream(uart)
147165
except ValueError:
148166
continue
149167

0 commit comments

Comments
 (0)