Skip to content

Commit cdd81e7

Browse files
committed
first commit
1 parent d746d7a commit cdd81e7

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
"""
2+
Heart Rate Trainer
3+
Read heart rate data from a heart rate peripheral using the standard BLE
4+
Heart Rate service.
5+
Displays BPM value and percentage of max heart rate on CLUE
6+
"""
7+
8+
import time
9+
from adafruit_clue import clue
10+
import adafruit_ble
11+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
12+
from adafruit_ble.services.standard.device_info import DeviceInfoService
13+
from adafruit_ble_heart_rate import HeartRateService
14+
15+
clue_data = clue.simple_text_display(title="Heart Rate", title_color = clue.PINK, title_scale=1, text_scale=3)
16+
17+
alarm_enable = True
18+
19+
# target heart rate for interval training
20+
# Change this number depending on your max heart rate, usually figured
21+
# as (220 - your age).
22+
max_rate = 180
23+
24+
# PyLint can't find BLERadio for some reason so special case it here.
25+
ble = adafruit_ble.BLERadio() # pylint: disable=no-member
26+
27+
hr_connection = None
28+
29+
# Start with a fresh connection.
30+
if ble.connected:
31+
print("SCAN")
32+
print("BLE")
33+
time.sleep(1)
34+
35+
for connection in ble.connections:
36+
if HeartRateService in connection:
37+
connection.disconnect()
38+
break
39+
40+
while True:
41+
print("Scanning...")
42+
print("SCAN")
43+
print("BLE")
44+
time.sleep(1)
45+
clue_data[0].text = "BPM: ---"
46+
clue_data[0].color = ((30, 0, 0))
47+
clue_data[1].text = "Scanning..."
48+
clue_data[3].text = ""
49+
clue_data[1].color = ((130, 130, 0))
50+
clue_data.show()
51+
52+
for adv in ble.start_scan(ProvideServicesAdvertisement, timeout=5):
53+
if HeartRateService in adv.services:
54+
print("found a HeartRateService advertisement")
55+
hr_connection = ble.connect(adv)
56+
#display_dots()
57+
print("....")
58+
time.sleep(2)
59+
print("Connected")
60+
break
61+
62+
# Stop scanning whether or not we are connected.
63+
ble.stop_scan()
64+
print("Stopped scan")
65+
time.sleep(0.1)
66+
67+
if hr_connection and hr_connection.connected:
68+
print("Fetch connection")
69+
if DeviceInfoService in hr_connection:
70+
dis = hr_connection[DeviceInfoService]
71+
try:
72+
manufacturer = dis.manufacturer
73+
except AttributeError:
74+
manufacturer = "(Manufacturer Not specified)"
75+
try:
76+
model_number = dis.model_number
77+
except AttributeError:
78+
model_number = "(Model number not specified)"
79+
print("Device:", manufacturer, model_number)
80+
else:
81+
print("No device information")
82+
hr_service = hr_connection[HeartRateService]
83+
print("Location:", hr_service.location)
84+
85+
while hr_connection.connected:
86+
values = hr_service.measurement_values
87+
#print(values) # returns the full heart_rate data set
88+
if values:
89+
bpm = (values.heart_rate)
90+
if bpm is not 0:
91+
pct_target = (round(100*(bpm/max_rate)))
92+
if values.heart_rate is 0:
93+
print("----")
94+
clue_data[0].text = "BPM: ---"
95+
clue_data[0].color = ((80, 0, 0))
96+
clue_data[1].text = "Target: --"
97+
clue_data[1].color = ((0, 0, 80))
98+
else:
99+
#print("pct target", pct_target)
100+
#time.sleep(0.1)
101+
#print("bpm", bpm)
102+
103+
clue_data[0].text = "BPM: {0:d}".format(bpm)
104+
clue_data[0].color = clue.RED
105+
106+
clue_data[1].text = "Target: {0:d}%".format(pct_target)
107+
if pct_target < 90:
108+
alarm = False
109+
clue_data[1].color = clue.CYAN
110+
else:
111+
alarm = True
112+
clue_data[1].color = clue.RED
113+
114+
clue_data[3].text = "Max HR: : {0:d}".format(max_rate)
115+
clue_data[3].color = clue.BLUE
116+
clue_data.show()
117+
118+
119+
if alarm and alarm_enable:
120+
clue.start_tone(2000)
121+
else:
122+
clue.stop_tone()
123+
124+
# Inputs
125+
if clue.button_a:
126+
max_rate = max_rate - 1
127+
if clue.button_b:
128+
max_rate = max_rate + 1
129+
130+
131+
if clue.touch_0:
132+
alarm_enable = False
133+
if clue.touch_1:
134+
alarm_enable = True
135+
136+
time.sleep(0.2)

0 commit comments

Comments
 (0)