@@ -29,10 +29,8 @@ class Clue:
2929 """
3030 def __init__ (self ):
3131 self ._i2c = board .I2C ()
32-
3332 self ._touches = [board .D0 , board .D1 , board .D2 ]
3433 self ._touch_threshold_adjustment = 0
35-
3634 self ._a = digitalio .DigitalInOut (board .BUTTON_A )
3735 self ._a .switch_to_input (pull = digitalio .Pull .UP )
3836 self ._b = digitalio .DigitalInOut (board .BUTTON_B )
@@ -79,7 +77,6 @@ def touch_2(self):
7977 return self ._touch (2 )
8078
8179
82-
8380class Pyloton :
8481 """
8582 Contains the various functions necessary for doing the Pyloton learn guide.
@@ -94,18 +91,14 @@ class Pyloton:
9491
9592 def __init__ (self , ble , display , circ , heart = True , speed = True , cad = True , ams = True , debug = False ): #pylint: disable=too-many-arguments
9693 self .debug = debug
97-
9894 self .ble = ble
99-
10095 self .display = display
101-
10296 self .circumference = circ
10397
10498 self .heart_enabled = heart
10599 self .speed_enabled = speed
106100 self .cadence_enabled = cad
107101 self .ams_enabled = ams
108-
109102 self .hr_connection = None
110103
111104 self .num_enabled = heart + speed + cad + ams
@@ -129,13 +122,12 @@ def __init__(self, ble, display, circ, heart=True, speed=True, cad=True, ams=Tru
129122 self ._speed_y = None
130123 self ._cadence_y = None
131124 self ._ams_y = None
132-
133125 self .ams = None
134126 self .cyc_connections = None
135127 self .cyc_services = None
128+ self .track_artist = True
136129
137130 self .start = time .time ()
138- self .track_artist = True
139131
140132 self .splash = displayio .Group (max_size = 25 )
141133 self .loading_group = displayio .Group ()
@@ -164,25 +156,15 @@ def show_splash(self):
164156 return
165157 with open ('blinka-pyloton.bmp' , 'rb' ) as bitmap_file :
166158 bitmap1 = displayio .OnDiskBitmap (bitmap_file )
167-
168159 tile_grid = displayio .TileGrid (bitmap1 , pixel_shader = displayio .ColorConverter ())
169-
170-
171160 self .loading_group .append (tile_grid )
172-
173161 self .display .show (self .loading_group )
174-
175162 status_heading = label .Label (font = self .arial16 , x = 80 , y = 175 ,
176163 text = "Status" , color = self .YELLOW )
177-
178164 rect = Rect (0 , 165 , 240 , 75 , fill = self .PURPLE )
179-
180165 self .loading_group .append (rect )
181166 self .loading_group .append (status_heading )
182167
183- self .display .show (self .loading_group )
184- time .sleep (.01 )
185-
186168
187169 def _load_fonts (self ):
188170 """
@@ -191,6 +173,7 @@ def _load_fonts(self):
191173 self .arial12 = bitmap_font .load_font ("/fonts/Arial-12.bdf" )
192174 self .arial16 = bitmap_font .load_font ("/fonts/Arial-16.bdf" )
193175 self .arial24 = bitmap_font .load_font ("/fonts/Arial-Bold-24.bdf" )
176+
194177 glyphs = b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-!,. "\' ?!'
195178 self .arial12 .load_glyphs (glyphs )
196179 self .arial16 .load_glyphs (glyphs )
@@ -204,20 +187,15 @@ def _status_update(self, message):
204187 if self .debug :
205188 print (message )
206189 return
207-
208190 if self .text_group not in self .loading_group :
209191 self .loading_group .append (self .text_group )
210-
211192 if len (message ) > 25 :
212193 self .status .text = message [:25 ]
213194 self .status1 .text = message [25 :50 ]
214-
215195 else :
216196 self .status .text = message
217197 self .status1 .text = ''
218198
219- #self.display.show(self.loading_group)
220-
221199
222200 def timeout (self ):
223201 """
@@ -243,6 +221,7 @@ def heart_connect(self):
243221 self ._hr_service = self .hr_connection [HeartRateService ]
244222 return self .hr_connection
245223
224+
246225 @staticmethod
247226 def _has_timed_out (start , timeout ):
248227 if time .time () - start >= timeout :
@@ -263,14 +242,10 @@ def ams_connect(self, start=time.time(), timeout=30):
263242 pass
264243
265244 self ._status_update ("AppleMediaService: Connected" )
266-
267- #known_notifications = set()
268-
269245 for connection in radio .connections :
270246 if not connection .paired :
271247 connection .pair ()
272248 self ._status_update ("AppleMediaService: Paired" )
273-
274249 self .ams = connection [AppleMediaService ]
275250
276251 return radio
@@ -301,19 +276,17 @@ def speed_cadence_connect(self):
301276 self .cyc_connections .append (self .ble .connect (adv ))
302277 self ._status_update ("Speed and Cadence: Connected {}" .format (len (self .cyc_connections )))
303278
304-
305279 self .cyc_services = []
306280 for conn in self .cyc_connections :
307-
308281 self .cyc_services .append (conn [CyclingSpeedAndCadenceService ])
309282 self ._status_update ("Pyloton: Finishing up..." )
283+
310284 return self .cyc_connections
311285
312286
313287 def _compute_speed (self , values , speed ):
314288 wheel_diff = values .last_wheel_event_time - self ._previous_wheel
315289 rev_diff = values .cumulative_wheel_revolutions - self ._previous_revolutions
316-
317290 if wheel_diff :
318291 # Rotations per minute is 60 times the amount of revolutions since
319292 # the last update over the time since the last update
@@ -330,6 +303,7 @@ def _compute_speed(self, values, speed):
330303 if self ._speed_failed >= 3 :
331304 speed = 0
332305 self ._previous_wheel = values .last_wheel_event_time
306+
333307 return speed
334308
335309
@@ -351,6 +325,7 @@ def _compute_cadence(self, values, cadence):
351325 if self ._cadence_failed >= 3 :
352326 cadence = 0
353327 self ._previous_crank = values .last_crank_event_time
328+
354329 return cadence
355330
356331
@@ -364,32 +339,26 @@ def read_s_and_c(self):
364339 if not conn .connected :
365340 speed = cadence = 0
366341 continue
367-
368342 values = svc .measurement_values
369-
370343 if not values :
371344 if self ._cadence_failed >= 3 or self ._speed_failed >= 3 :
372345 if self ._cadence_failed > 3 :
373346 cadence = 0
374347 if self ._speed_failed > 3 :
375348 speed = 0
376349 continue
377-
378350 if not values .last_wheel_event_time :
379351 continue
380-
381352 speed = self ._compute_speed (values , speed )
382-
383-
384353 if not values .last_crank_event_time :
385354 continue
386-
387355 cadence = self ._compute_cadence (values , cadence )
388356
389357 if speed :
390358 speed = str (speed )[:8 ]
391359 if cadence :
392360 cadence = str (cadence )[:8 ]
361+
393362 return speed , cadence
394363
395364
@@ -403,8 +372,10 @@ def read_heart(self):
403372 else :
404373 heart = measurement .heart_rate
405374 self ._previous_heart = measurement .heart_rate
375+
406376 if heart :
407377 heart = str (heart )[:4 ]
378+
408379 return heart
409380
410381
@@ -417,7 +388,6 @@ def read_ams(self):
417388 if current - self .start > 3 :
418389 self .track_artist = not self .track_artist
419390 self .start = time .time ()
420-
421391 if self .track_artist :
422392 data = self .ams .artist
423393 if not self .track_artist :
@@ -427,6 +397,7 @@ def read_ams(self):
427397
428398 if data :
429399 data = data [:16 ] + (data [16 :] and '..' )
400+
430401 return data
431402
432403
@@ -458,15 +429,12 @@ def _get_y(self):
458429 if self .heart_enabled :
459430 self ._heart_y = 45 * (self .num_enabled - enabled ) + 75
460431 enabled -= 1
461-
462432 if self .speed_enabled :
463433 self ._speed_y = 45 * (self .num_enabled - enabled ) + 75
464434 enabled -= 1
465-
466435 if self .cadence_enabled :
467436 self ._cadence_y = 45 * (self .num_enabled - enabled ) + 75
468437 enabled -= 1
469-
470438 if self .ams_enabled :
471439 self ._ams_y = 45 * (self .num_enabled - enabled ) + 75
472440 enabled -= 1
@@ -508,7 +476,6 @@ def setup_display(self):
508476 self .loading_group .pop ()
509477
510478
511-
512479 def update_display (self ): #pylint: disable=too-many-branches
513480 """
514481 Updates the display to display the most recent values
0 commit comments