Skip to content

Commit 0bddd9e

Browse files
committed
initialize
1 parent 7a68538 commit 0bddd9e

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

CPB_heart_rate_display/code.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
"""
2+
Circuit Playground Bluefruit BLE Heart Rate Display
3+
Read heart rate data from a heart rate peripheral using
4+
the standard BLE heart rate service.
5+
The heart rate monitor connects to the CPB via BLE.
6+
LEDs on CPB blink to the heart rate of the user.
7+
"""
8+
9+
import time
10+
import board
11+
import neopixel
12+
import adafruit_ble
13+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
14+
from adafruit_ble.services.standard.device_info import DeviceInfoService
15+
from adafruit_ble_heart_rate import HeartRateService
16+
from digitalio import DigitalInOut, Direction
17+
18+
#on-board status LED setup
19+
red_led = DigitalInOut(board.D13)
20+
red_led.direction = Direction.OUTPUT
21+
red_led.value = True
22+
23+
#NeoPixel code
24+
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.2, auto_write=False)
25+
RED = (255, 0, 0)
26+
LIGHTRED = (20, 0, 0)
27+
28+
def color_chase(color, wait):
29+
for i in range(10):
30+
pixels[i] = color
31+
time.sleep(wait)
32+
pixels.show()
33+
time.sleep(0.5)
34+
35+
# animation to show initialization of program
36+
color_chase(RED, 0.1) # Increase the number to slow down the color chase
37+
38+
#starting bpm value
39+
bpm = 60
40+
41+
# PyLint can't find BLERadio for some reason so special case it here.
42+
ble = adafruit_ble.BLERadio() # pylint: disable=no-member
43+
44+
hr_connection = None
45+
46+
# Start with a fresh connection.
47+
if ble.connected:
48+
time.sleep(1)
49+
50+
for connection in ble.connections:
51+
if HeartRateService in connection:
52+
connection.disconnect()
53+
break
54+
55+
while True:
56+
print("Scanning...")
57+
red_led.value = True
58+
time.sleep(1)
59+
60+
for adv in ble.start_scan(ProvideServicesAdvertisement, timeout=5):
61+
if HeartRateService in adv.services:
62+
print("found a HeartRateService advertisement")
63+
hr_connection = ble.connect(adv)
64+
time.sleep(2)
65+
print("Connected")
66+
red_led.value = False
67+
break
68+
69+
# Stop scanning whether or not we are connected.
70+
ble.stop_scan()
71+
print("Stopped scan")
72+
red_led.value = False
73+
time.sleep(0.5)
74+
75+
if hr_connection and hr_connection.connected:
76+
print("Fetch connection")
77+
if DeviceInfoService in hr_connection:
78+
dis = hr_connection[DeviceInfoService]
79+
try:
80+
manufacturer = dis.manufacturer
81+
except AttributeError:
82+
manufacturer = "(Manufacturer Not specified)"
83+
try:
84+
model_number = dis.model_number
85+
except AttributeError:
86+
model_number = "(Model number not specified)"
87+
print("Device:", manufacturer, model_number)
88+
else:
89+
print("No device information")
90+
hr_service = hr_connection[HeartRateService]
91+
print("Location:", hr_service.location)
92+
93+
while hr_connection.connected:
94+
values = hr_service.measurement_values
95+
print(values) # returns the full heart_rate data set
96+
if values:
97+
bpm = (values.heart_rate)
98+
if values.heart_rate == 0:
99+
print("-")
100+
else:
101+
time.sleep(0.1)
102+
print(bpm)
103+
if bpm != 0: # prevent from divide by zero
104+
#find interval time between beats
105+
bps = bpm / 60
106+
period = 1 / bps
107+
time_on = 0.375 * period
108+
time_off = period - time_on
109+
110+
# Blink leds at the given BPM
111+
pixels.fill(RED)
112+
pixels.show()
113+
# Increase or decrease to change the speed of the solid color change.
114+
time.sleep(time_on)
115+
pixels.fill(LIGHTRED)
116+
pixels.show()
117+
time.sleep(time_off)

0 commit comments

Comments
 (0)