|
18 | 18 | import busio
|
19 | 19 | import digitalio
|
20 | 20 | import storage
|
| 21 | +import adafruit_pcf8523 |
21 | 22 | import _bleio
|
22 | 23 | import adafruit_ble
|
23 | 24 | from adafruit_ble.advertising.standard import Advertisement
|
|
32 | 33 | vfs = storage.VfsFat(sd_card)
|
33 | 34 | storage.mount(vfs, "/sd_card")
|
34 | 35 |
|
| 36 | +log_interval = 2 # you can adjust this to log at a different rate |
| 37 | + |
| 38 | +# RTC setup |
| 39 | +I2C = busio.I2C(board.SCL, board.SDA) |
| 40 | +rtc = adafruit_pcf8523.PCF8523(I2C) |
| 41 | + |
| 42 | +days = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday") |
| 43 | + |
| 44 | +set_time = False |
| 45 | +if set_time: # change to True if you want to write the time! |
| 46 | + # year, mon, date, hour, min, sec, wday, yday, isdst |
| 47 | + t = time.struct_time((2020, 4, 21, 16, 36, 0, 2, -1, -1)) |
| 48 | + # you must set year, mon, date, hour, min, sec and weekday |
| 49 | + # yearday not supported, isdst can be set but we don't use it at this time |
| 50 | + print("Setting time to:", t) # uncomment for debugging |
| 51 | + rtc.datetime = t |
| 52 | + print() |
| 53 | + |
35 | 54 | # PyLint can't find BLERadio for some reason so special case it here.
|
36 | 55 | ble = adafruit_ble.BLERadio() # pylint: disable=no-member
|
37 | 56 |
|
38 | 57 | pulse_ox_connection = None
|
39 | 58 | initial_time = time.monotonic()
|
40 | 59 |
|
41 | 60 | while True:
|
| 61 | + t = rtc.datetime |
42 | 62 | print("Scanning for Pulse Oximeter...")
|
43 | 63 | for adv in ble.start_scan(Advertisement, timeout=5):
|
44 | 64 | name = adv.complete_name
|
|
80 | 100 | valid, spo2, pulse_rate, pleth, finger = values
|
81 | 101 | if not valid:
|
82 | 102 | continue
|
83 |
| - |
| 103 | + if ( |
| 104 | + pulse_rate == 255 |
| 105 | + ): # device sends 255 as pulse until it has a valid read |
| 106 | + continue |
84 | 107 | print(
|
85 | 108 | "SpO2: {}% | ".format(spo2),
|
86 | 109 | "Pulse Rate: {} BPM | ".format(pulse_rate),
|
|
96 | 119 | "Seconds since current data log started:",
|
97 | 120 | int(time_stamp),
|
98 | 121 | )
|
| 122 | + sdc.write( |
| 123 | + "{} {}/{}/{} {}:{}:{} ".format( |
| 124 | + days[t.tm_wday], |
| 125 | + t.tm_mday, |
| 126 | + t.tm_mon, |
| 127 | + t.tm_year, |
| 128 | + t.tm_hour, |
| 129 | + t.tm_min, |
| 130 | + t.tm_sec, |
| 131 | + ) |
| 132 | + ) |
99 | 133 | sdc.write(
|
100 | 134 | "{}, {}, {}, {:.2f}\n".format(
|
101 | 135 | int(time_stamp), spo2, pulse_rate, pleth
|
102 | 136 | )
|
103 | 137 | )
|
104 | 138 |
|
105 |
| - time.sleep(2) |
| 139 | + time.sleep(log_interval) |
106 | 140 | except OSError:
|
107 | 141 | pass
|
108 | 142 | except RuntimeError:
|
|
0 commit comments