|
1 | 1 | import time
|
| 2 | +import adafruit_ble |
2 | 3 | from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
|
| 4 | +from adafruit_ble.advertising.standard import SolicitServicesAdvertisement |
3 | 5 | import displayio
|
4 | 6 | import adafruit_imageload
|
5 | 7 | from adafruit_ble_cycling_speed_and_cadence import CyclingSpeedAndCadenceService
|
|
8 | 10 | from adafruit_display_shapes.rect import Rect
|
9 | 11 | from adafruit_display_text import label
|
10 | 12 |
|
| 13 | +from adafruit_ble_apple_media import AppleMediaService |
| 14 | + |
11 | 15 |
|
12 | 16 | class Pyloton:
|
13 | 17 |
|
@@ -148,6 +152,29 @@ def heart_connect(self):
|
148 | 152 | break
|
149 | 153 | return self.hr_connection
|
150 | 154 |
|
| 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 | + |
151 | 178 |
|
152 | 179 | def speed_cad_connect(self):
|
153 | 180 | """
|
@@ -246,11 +273,13 @@ def icon_maker(self, n, icon_x, icon_y):
|
246 | 273 | return sprite
|
247 | 274 |
|
248 | 275 |
|
249 |
| - def _label_maker(self, text, x, y): |
| 276 | + def _label_maker(self, text, x, y, font=None): |
250 | 277 | """
|
251 | 278 | Generates labels
|
252 | 279 | """
|
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) |
254 | 283 |
|
255 | 284 |
|
256 | 285 | def _get_y(self):
|
@@ -338,13 +367,15 @@ def update_display(self, hr_service):
|
338 | 367 | self.splash.append(cad_label)
|
339 | 368 |
|
340 | 369 | 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 | + |
342 | 372 | if self.setup:
|
343 | 373 | self.splash[6] = ams_label
|
344 | 374 | else:
|
345 | 375 | self.splash.append(ams_label)
|
346 | 376 |
|
347 | 377 |
|
348 | 378 | self.setup=True
|
| 379 | + time.sleep(0.1) |
349 | 380 |
|
350 | 381 | self.display.show(self.splash)
|
0 commit comments