@@ -22,26 +22,42 @@ class Pyloton:
22
22
23
23
_previous_heart = 0
24
24
25
- splash = displayio .Group ()
26
-
25
+ splash = displayio .Group (max_size = 25 )
26
+
27
27
setup = False
28
28
29
- def __init__ (self , ble , display , heart = True , speed = True , cadence = True , ams = True , debug = False ):
29
+ YELLOW = 0xFCFF00
30
+ PURPLE = 0x64337E
31
+ WHITE = 0xFFFFFF
32
+
33
+ splash_group = displayio .Group ()
34
+
35
+ cyc_connections = []
36
+ cyc_services = []
37
+
38
+ def __init__ (self , ble , display , circ , heart = True , speed = True , cad = True , ams = True , debug = False ):
30
39
self .debug = debug
31
40
32
41
self .ble = ble
33
- self .hr_connection = None
42
+
43
+ self .display = display
44
+
45
+ self .circumference = circ
34
46
35
47
self .heart_enabled = heart
36
48
self .speed_enabled = speed
37
- self .cadence_enabled = cadence
49
+ self .cadence_enabled = cad
38
50
self .ams_enabled = ams
39
51
40
- self .display = display
52
+ self .hr_connection = None
53
+
54
+
55
+ self ._load_fonts ()
41
56
42
57
self .sprite_sheet , self .palette = adafruit_imageload .load ("/sprite_sheet.bmp" ,
58
+ bitmap = displayio .Bitmap ,
43
59
palette = displayio .Palette )
44
- self . _load_fonts ()
60
+
45
61
46
62
def show_splash (self ):
47
63
"""
@@ -54,11 +70,18 @@ def show_splash(self):
54
70
55
71
tile_grid = displayio .TileGrid (bitmap , pixel_shader = displayio .ColorConverter ())
56
72
57
- self . splash_group = displayio . Group ()
73
+
58
74
self .splash_group .append (tile_grid )
59
75
76
+ status_heading = label .Label (font = self .arial16 , x = 80 , y = 175 ,
77
+ text = "Status" , color = self .YELLOW )
78
+ rect = Rect (0 , 165 , 240 , 75 , fill = self .PURPLE )
79
+
80
+ self .splash_group .append (rect )
81
+ self .splash_group .append (status_heading )
82
+
60
83
self .display .show (self .splash_group )
61
- time .sleep (.05 )
84
+ time .sleep (.01 )
62
85
63
86
64
87
def _load_fonts (self ):
@@ -77,27 +100,26 @@ def _status_update(self, message):
77
100
if self .debug :
78
101
print (message )
79
102
return
80
- YELLOW = 0xFCFF00
81
- PURPLE = 0x64337E
103
+
82
104
text_group = displayio .Group ()
83
105
if len (message ) > 25 :
84
- status = label .Label (font = self .arial12 , x = 10 , y = 200 , text = message [:25 ], color = YELLOW )
85
- status1 = label .Label (font = self .arial12 , x = 10 , y = 220 , text = message [25 :], color = YELLOW )
106
+ status = label .Label (font = self .arial12 , x = 10 , y = 200 ,
107
+ text = message [:25 ], color = self .YELLOW )
108
+ status1 = label .Label (font = self .arial12 , x = 10 , y = 220 ,
109
+ text = message [25 :], color = self .YELLOW )
110
+
86
111
text_group .append (status )
87
112
text_group .append (status1 )
88
113
else :
89
- status = label .Label (font = self .arial12 , x = 10 , y = 200 , text = message , color = YELLOW )
114
+ status = label .Label (font = self .arial12 , x = 10 , y = 200 , text = message , color = self . YELLOW )
90
115
text_group .append (status )
91
116
92
117
93
- if len (self .splash_group ) == 1 :
94
- status_heading = label .Label (font = self .arial16 , x = 80 , y = 175 , text = "Status" , color = YELLOW )
95
- rect = Rect (0 , 165 , 240 , 75 , fill = PURPLE )
96
- self .splash_group .append (rect )
97
- self .splash_group .append (status_heading )
118
+ if len (self .splash_group ) == 3 :
98
119
self .splash_group .append (text_group )
99
120
else :
100
121
self .splash_group [3 ] = text_group
122
+
101
123
self .display .show (self .splash_group )
102
124
time .sleep (0.01 )
103
125
@@ -121,7 +143,7 @@ def heart_connect(self):
121
143
break
122
144
return self .hr_connection
123
145
124
- def s_and_c_connect (self ):
146
+ def speed_cad_connect (self ):
125
147
"""
126
148
Connects to speed and cadence sensor
127
149
"""
@@ -154,9 +176,10 @@ def s_and_c_connect(self):
154
176
print ("Done" )
155
177
return self .cyc_connections
156
178
179
+
157
180
def read_s_and_c (self ):
158
181
"""
159
- Reads the speed and cadence sensor
182
+ Reads data from the speed and cadence sensor
160
183
"""
161
184
speed = self ._previous_speed
162
185
cadence = self ._previous_cadence
@@ -169,32 +192,33 @@ def read_s_and_c(self):
169
192
rev_diff = values .cumulative_wheel_revolutions - self ._previous_revolutions
170
193
171
194
if wheel_diff :
172
- #Rotations per minute is 60 times the amount of revolutions since
173
- #the last update over the time since the last update
195
+ # Rotations per minute is 60 times the amount of revolutions since
196
+ # the last update over the time since the last update
174
197
rpm = 60 * (rev_diff / (wheel_diff / 1024 ))
175
198
# We then mutiply it by the wheel's circumference and convert it to mph
176
- speed = round ((rpm * 84.229 ) * (60 / 63360 ), 1 )
199
+ speed = round ((rpm * self . circumference ) * (60 / 63360 ), 1 )
177
200
self ._previous_speed = speed
178
201
self ._previous_revolutions = values .cumulative_wheel_revolutions
179
202
self ._previous_wheel = values .last_wheel_event_time
180
203
181
204
if values .last_crank_event_time :
182
205
crank_diff = values .last_crank_event_time - self ._previous_crank
183
- crank_rev_diff = values .cumulative_crank_revolutions - self ._previous_crank_rev
206
+ crank_rev_diff = values .cumulative_crank_revolutions - self ._previous_crank_rev
184
207
185
208
if crank_rev_diff :
186
- #Rotations per minute is 60 times the amount of revolutions since the
187
- #last update over the time since the last update
209
+ # Rotations per minute is 60 times the amount of revolutions since the
210
+ # last update over the time since the last update
188
211
cadence = round (60 * (crank_rev_diff / (crank_diff / 1024 )), 1 )
189
212
self ._previous_cadence = cadence
190
213
self ._previous_crank_rev = values .cumulative_crank_revolutions
191
214
192
215
self ._previous_crank = values .last_crank_event_time
193
216
return speed , cadence
217
+ return 0 , 0
194
218
195
219
def read_heart (self , hr_service ):
196
220
"""
197
- Reads the heart rate sensor
221
+ Reads date from the heart rate sensor
198
222
"""
199
223
measurement = hr_service .measurement_values
200
224
if measurement is None :
@@ -204,46 +228,49 @@ def read_heart(self, hr_service):
204
228
self ._previous_heart = measurement .heart_rate
205
229
return heart
206
230
207
- def _icon_maker (self , n , icon_x , icon_y ):
231
+
232
+ def icon_maker (self , n , icon_x , icon_y ):
208
233
"""
209
- Generates sprites
234
+ Generates icons as sprites
210
235
"""
211
- sprite = displayio .TileGrid (self .sprite_sheet , pixel_shader = self .palette ,
212
- width = 1 ,
213
- height = 1 ,
214
- tile_width = 40 ,
215
- tile_height = 40 ,
216
- default_tile = n ,
217
- x = icon_x , y = icon_y )
236
+
237
+ sprite = displayio .TileGrid (self .sprite_sheet , pixel_shader = self .palette , width = 1 , height = 1 , tile_width = 40 , tile_height = 40 , default_tile = n , x = icon_x , y = icon_y )
218
238
return sprite
219
239
240
+ def _label_maker (self , text , x , y ):
241
+ """
242
+ Generates labels
243
+ """
244
+ return label .Label (font = self .arial24 , x = x , y = y , text = text , color = self .WHITE )
245
+
220
246
def _setup_display (self ):
221
247
sprites = displayio .Group ()
222
248
223
- rect = Rect (0 , 0 , 240 , 50 , fill = 0x64338e )
249
+ rect = Rect (0 , 0 , 240 , 50 , fill = self . PURPLE )
224
250
self .splash .append (rect )
225
251
226
- heading = label .Label (font = self .arial24 , x = 55 , y = 25 , text = "PyLoton" , color = 0xFCFF00 )
252
+ heading = label .Label (font = self .arial24 , x = 55 , y = 25 , text = "PyLoton" , color = self . YELLOW )
227
253
self .splash .append (heading )
228
254
229
255
if self .heart_enabled :
230
- heart_sprite = self ._icon_maker (0 , 2 , 55 )
256
+ heart_sprite = self .icon_maker (0 , 2 , 55 )
231
257
sprites .append (heart_sprite )
232
258
233
259
if self .speed_enabled :
234
- speed_sprite = self ._icon_maker (1 , 2 , 100 )
260
+ speed_sprite = self .icon_maker (1 , 2 , 100 )
235
261
sprites .append (speed_sprite )
236
262
237
263
if self .cadence_enabled :
238
- cadence_sprite = self ._icon_maker (2 , 2 , 145 )
264
+ cadence_sprite = self .icon_maker (2 , 2 , 145 )
239
265
sprites .append (cadence_sprite )
240
266
241
267
if self .ams_enabled :
242
- ams_sprite = self ._icon_maker (3 , 2 , 190 )
268
+ ams_sprite = self .icon_maker (3 , 2 , 190 )
243
269
sprites .append (ams_sprite )
244
270
245
271
self .splash .append (sprites )
246
272
273
+
247
274
def update_display (self , hr_service ):
248
275
heart = self .read_heart (hr_service )
249
276
speed , cadence = self .read_s_and_c ()
@@ -252,36 +279,32 @@ def update_display(self, hr_service):
252
279
self ._setup_display ()
253
280
254
281
if self .heart_enabled :
255
- hr_str = '{} bpm' .format (heart )
256
- hr_label = label .Label (font = self .arial24 , x = 50 , y = 75 , text = hr_str , color = 0xFFFFFF )
282
+ hr_label = self ._label_maker ('{} bpm' .format (heart ), 50 , 75 )
257
283
if not self .setup :
258
284
self .splash .append (hr_label )
259
285
else :
260
286
self .splash [3 ] = hr_label
261
287
262
288
if self .speed_enabled :
263
- sp_str = '{} mph' .format (speed )
264
- sp_label = label .Label (font = self .arial24 , x = 50 , y = 120 , text = sp_str , color = 0xFFFFFF )
289
+ sp_label = self ._label_maker ('{} rpm' .format (speed ), 50 , 120 )
265
290
if not self .setup :
266
291
self .splash .append (sp_label )
267
292
else :
268
293
self .splash [4 ] = sp_label
269
294
270
295
if self .cadence_enabled :
271
- cad_str = '{} rpm' .format (cadence )
272
- cad_label = label .Label (font = self .arial24 , x = 50 , y = 165 , text = cad_str , color = 0xFFFFFF )
296
+ cad_label = self ._label_maker ('{} rpm' .format (cadence ), 50 , 165 )
273
297
if not self .setup :
274
298
self .splash .append (cad_label )
275
299
else :
276
300
self .splash [5 ] = cad_label
277
301
278
302
if self .ams_enabled :
279
- np_str = 'None'
280
- now_playing = label .Label (font = self .arial24 , x = 50 , y = 210 , text = np_str , color = 0xFFFFFF )
303
+ ams_label = self ._label_maker ('None' , 50 , 210 )
281
304
if not self .setup :
282
- self .splash .append (now_playing )
305
+ self .splash .append (ams_label )
283
306
else :
284
- self .splash [6 ] = now_playing
307
+ self .splash [6 ] = ams_label
285
308
286
309
self .setup = True
287
310
time .sleep (0.2 )
0 commit comments