Skip to content

Commit 2ed55c6

Browse files
committed
Fixed bugs caused by refactor
1 parent 6e644fd commit 2ed55c6

File tree

2 files changed

+84
-62
lines changed

2 files changed

+84
-62
lines changed

CircuitPython_Pyloton/code.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
Heart Rate service.
44
"""
55

6+
import time
67
import adafruit_ble
78
import board
89
from adafruit_ble_heart_rate import HeartRateService
910
import pyloton
10-
import time
11-
1211

1312
# PyLint can't find BLERadio for some reason so special case it here.
1413
ble = adafruit_ble.BLERadio() # pylint: disable=no-member
1514

1615
display = board.DISPLAY
1716

18-
pyloton = pyloton.Pyloton(ble, display, debug=True)
17+
# 84.229 is wheel circumference (700x23 in my case)
18+
pyloton = pyloton.Pyloton(ble, display, 84.229, debug=True)
1919
pyloton.show_splash()
2020

2121
hr_connection = None
@@ -28,23 +28,22 @@
2828

2929
start = time.time()
3030
hr_connection = None
31-
s_and_c_connection = []
31+
speed_cad_connection = []
3232
while True:
3333
if not hr_connection:
3434
print("Running hr_connection")
3535
hr_connection = pyloton.heart_connect()
3636
ble.stop_scan()
37-
if not s_and_c_connection:
38-
print("Running s_and_c_connection")
39-
s_and_c_connection = pyloton.s_and_c_connect()
37+
if not speed_cad_connection:
38+
print("Running speed_cad_connection")
39+
speed_cad_connection = pyloton.speed_cad_connect()
4040

4141
if time.time()-start >= 45:
4242
pyloton.timeout()
4343
break
4444
# Stop scanning whether or not we are connected.
4545
ble.stop_scan()
46-
if hr_connection and hr_connection.connected and s_and_c_connection:
47-
46+
if hr_connection and hr_connection.connected and speed_cad_connection:
4847
print("Fetch connection")
4948
hr_service = hr_connection[HeartRateService]
5049
print("Location:", hr_service.location)

CircuitPython_Pyloton/pyloton.py

Lines changed: 76 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,42 @@ class Pyloton:
2222

2323
_previous_heart = 0
2424

25-
splash = displayio.Group()
26-
25+
splash = displayio.Group(max_size=25)
26+
2727
setup = False
2828

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):
3039
self.debug = debug
3140

3241
self.ble = ble
33-
self.hr_connection = None
42+
43+
self.display = display
44+
45+
self.circumference = circ
3446

3547
self.heart_enabled = heart
3648
self.speed_enabled = speed
37-
self.cadence_enabled = cadence
49+
self.cadence_enabled = cad
3850
self.ams_enabled = ams
3951

40-
self.display = display
52+
self.hr_connection = None
53+
54+
55+
self._load_fonts()
4156

4257
self.sprite_sheet, self.palette = adafruit_imageload.load("/sprite_sheet.bmp",
58+
bitmap=displayio.Bitmap,
4359
palette=displayio.Palette)
44-
self._load_fonts()
60+
4561

4662
def show_splash(self):
4763
"""
@@ -54,11 +70,18 @@ def show_splash(self):
5470

5571
tile_grid = displayio.TileGrid(bitmap, pixel_shader=displayio.ColorConverter())
5672

57-
self.splash_group = displayio.Group()
73+
5874
self.splash_group.append(tile_grid)
5975

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+
6083
self.display.show(self.splash_group)
61-
time.sleep(.05)
84+
time.sleep(.01)
6285

6386

6487
def _load_fonts(self):
@@ -77,27 +100,26 @@ def _status_update(self, message):
77100
if self.debug:
78101
print(message)
79102
return
80-
YELLOW = 0xFCFF00
81-
PURPLE = 0x64337E
103+
82104
text_group = displayio.Group()
83105
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+
86111
text_group.append(status)
87112
text_group.append(status1)
88113
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)
90115
text_group.append(status)
91116

92117

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:
98119
self.splash_group.append(text_group)
99120
else:
100121
self.splash_group[3] = text_group
122+
101123
self.display.show(self.splash_group)
102124
time.sleep(0.01)
103125

@@ -121,7 +143,7 @@ def heart_connect(self):
121143
break
122144
return self.hr_connection
123145

124-
def s_and_c_connect(self):
146+
def speed_cad_connect(self):
125147
"""
126148
Connects to speed and cadence sensor
127149
"""
@@ -154,9 +176,10 @@ def s_and_c_connect(self):
154176
print("Done")
155177
return self.cyc_connections
156178

179+
157180
def read_s_and_c(self):
158181
"""
159-
Reads the speed and cadence sensor
182+
Reads data from the speed and cadence sensor
160183
"""
161184
speed = self._previous_speed
162185
cadence = self._previous_cadence
@@ -169,32 +192,33 @@ def read_s_and_c(self):
169192
rev_diff = values.cumulative_wheel_revolutions - self._previous_revolutions
170193

171194
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
174197
rpm = 60*(rev_diff/(wheel_diff/1024))
175198
# 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)
177200
self._previous_speed = speed
178201
self._previous_revolutions = values.cumulative_wheel_revolutions
179202
self._previous_wheel = values.last_wheel_event_time
180203

181204
if values.last_crank_event_time:
182205
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
184207

185208
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
188211
cadence = round(60*(crank_rev_diff/(crank_diff/1024)), 1)
189212
self._previous_cadence = cadence
190213
self._previous_crank_rev = values.cumulative_crank_revolutions
191214

192215
self._previous_crank = values.last_crank_event_time
193216
return speed, cadence
217+
return 0, 0
194218

195219
def read_heart(self, hr_service):
196220
"""
197-
Reads the heart rate sensor
221+
Reads date from the heart rate sensor
198222
"""
199223
measurement = hr_service.measurement_values
200224
if measurement is None:
@@ -204,46 +228,49 @@ def read_heart(self, hr_service):
204228
self._previous_heart = measurement.heart_rate
205229
return heart
206230

207-
def _icon_maker(self, n, icon_x, icon_y):
231+
232+
def icon_maker(self, n, icon_x, icon_y):
208233
"""
209-
Generates sprites
234+
Generates icons as sprites
210235
"""
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)
218238
return sprite
219239

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+
220246
def _setup_display(self):
221247
sprites = displayio.Group()
222248

223-
rect = Rect(0, 0, 240, 50, fill=0x64338e)
249+
rect = Rect(0, 0, 240, 50, fill=self.PURPLE)
224250
self.splash.append(rect)
225251

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)
227253
self.splash.append(heading)
228254

229255
if self.heart_enabled:
230-
heart_sprite = self._icon_maker(0, 2, 55)
256+
heart_sprite = self.icon_maker(0, 2, 55)
231257
sprites.append(heart_sprite)
232258

233259
if self.speed_enabled:
234-
speed_sprite = self._icon_maker(1, 2, 100)
260+
speed_sprite = self.icon_maker(1, 2, 100)
235261
sprites.append(speed_sprite)
236262

237263
if self.cadence_enabled:
238-
cadence_sprite = self._icon_maker(2, 2, 145)
264+
cadence_sprite = self.icon_maker(2, 2, 145)
239265
sprites.append(cadence_sprite)
240266

241267
if self.ams_enabled:
242-
ams_sprite = self._icon_maker(3, 2, 190)
268+
ams_sprite = self.icon_maker(3, 2, 190)
243269
sprites.append(ams_sprite)
244270

245271
self.splash.append(sprites)
246272

273+
247274
def update_display(self, hr_service):
248275
heart = self.read_heart(hr_service)
249276
speed, cadence = self.read_s_and_c()
@@ -252,36 +279,32 @@ def update_display(self, hr_service):
252279
self._setup_display()
253280

254281
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)
257283
if not self.setup:
258284
self.splash.append(hr_label)
259285
else:
260286
self.splash[3] = hr_label
261287

262288
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)
265290
if not self.setup:
266291
self.splash.append(sp_label)
267292
else:
268293
self.splash[4] = sp_label
269294

270295
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)
273297
if not self.setup:
274298
self.splash.append(cad_label)
275299
else:
276300
self.splash[5] = cad_label
277301

278302
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)
281304
if not self.setup:
282-
self.splash.append(now_playing)
305+
self.splash.append(ams_label)
283306
else:
284-
self.splash[6] = now_playing
307+
self.splash[6] = ams_label
285308

286309
self.setup=True
287310
time.sleep(0.2)

0 commit comments

Comments
 (0)