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