Skip to content

Commit eaff253

Browse files
authored
Merge pull request #1 from dherrada/adding-ams
Adding ams (I actually tested this one, believe it or not)
2 parents cc028b6 + 5666c46 commit eaff253

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

CircuitPython_Pyloton/code.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,29 @@
1818
pyloton = pyloton.Pyloton(ble, display, 84.229)
1919
pyloton.show_splash()
2020

21+
pyloton.ams_connect()
22+
2123
hr_connection = None
22-
# Start with a fresh connection.
23-
if ble.connected:
24-
for connection in ble.connections:
25-
if HeartRateService in connection:
26-
connection.disconnect()
27-
break
2824

2925
start = time.time()
3026
hr_connection = None
31-
speed_cad_connection = []
27+
speed_cad_connections = []
28+
radio = None
3229
while True:
3330
if not hr_connection:
3431
print("Running hr_connection")
3532
hr_connection = pyloton.heart_connect()
3633
ble.stop_scan()
37-
if not speed_cad_connection:
34+
if not speed_cad_connections:
3835
print("Running speed_cad_connection")
39-
speed_cad_connection = pyloton.speed_cad_connect()
36+
speed_cad_connections = pyloton.speed_cad_connect()
4037

4138
if time.time()-start >= 45:
4239
pyloton.timeout()
4340
break
4441
# Stop scanning whether or not we are connected.
4542
ble.stop_scan()
46-
if hr_connection and hr_connection.connected and speed_cad_connection:
43+
if hr_connection and hr_connection.connected and speed_cad_connections:
4744
print("Fetch connection")
4845
hr_service = hr_connection[HeartRateService]
4946
print("Location:", hr_service.location)

CircuitPython_Pyloton/pyloton.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import time
2+
import adafruit_ble
23
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
4+
from adafruit_ble.advertising.standard import SolicitServicesAdvertisement
35
import displayio
46
import adafruit_imageload
57
from adafruit_ble_cycling_speed_and_cadence import CyclingSpeedAndCadenceService
@@ -8,6 +10,8 @@
810
from adafruit_display_shapes.rect import Rect
911
from adafruit_display_text import label
1012

13+
from adafruit_ble_apple_media import AppleMediaService
14+
1115

1216
class Pyloton:
1317

@@ -148,6 +152,29 @@ def heart_connect(self):
148152
break
149153
return self.hr_connection
150154

155+
def ams_connect(self):
156+
self._status_update("Connect your phone now")
157+
radio = adafruit_ble.BLERadio()
158+
a = SolicitServicesAdvertisement()
159+
a.solicited_services.append(AppleMediaService)
160+
radio.start_advertising(a)
161+
162+
while not radio.connected:
163+
pass
164+
165+
self._status_update("Connected")
166+
167+
known_notifications = set()
168+
169+
for connection in radio.connections:
170+
if not connection.paired:
171+
connection.pair()
172+
self._status_update("paired")
173+
174+
self.ams = connection[AppleMediaService]
175+
176+
self.radio = radio
177+
151178

152179
def speed_cad_connect(self):
153180
"""
@@ -246,11 +273,13 @@ def icon_maker(self, n, icon_x, icon_y):
246273
return sprite
247274

248275

249-
def _label_maker(self, text, x, y):
276+
def _label_maker(self, text, x, y, font=None):
250277
"""
251278
Generates labels
252279
"""
253-
return label.Label(font=self.arial24, x=x, y=y, text=text, color=self.WHITE)
280+
if not font:
281+
font = self.arial24
282+
return label.Label(font=font, x=x, y=y, text=text, color=self.WHITE)
254283

255284

256285
def _get_y(self):
@@ -338,13 +367,15 @@ def update_display(self, hr_service):
338367
self.splash.append(cad_label)
339368

340369
if self.ams_enabled:
341-
ams_label = self._label_maker('None', 50, 210) # 210
370+
ams_label = self._label_maker('{}'.format(self.ams.title), 50, 210, font=self.arial16) # 210
371+
342372
if self.setup:
343373
self.splash[6] = ams_label
344374
else:
345375
self.splash.append(ams_label)
346376

347377

348378
self.setup=True
379+
time.sleep(0.1)
349380

350381
self.display.show(self.splash)

0 commit comments

Comments
 (0)