diff --git a/main.py b/main.py deleted file mode 100644 index 17f2e40f09894d2ea37297d58adc4733fcc098e8..0000000000000000000000000000000000000000 --- a/main.py +++ /dev/null @@ -1,96 +0,0 @@ -from machine import Pin, I2C -from filefifo import Filefifo -import ssd1306 -import utime - -SAMPLE_RATE = 250 -THRESHOLD = 20000 -MIN_PPI = 30 -MAX_PPI = 500 -MEASURE_INTERVAL_MS = 5000 -REFERENCE_BPM = 75 # Set this to the SpO2 device BPM manually - -# ---------- HARDWARE SETUP ---------- -i2c = I2C(1, scl=Pin(15), sda=Pin(14)) # GP1=SCL, GP0=SDA -oled = ssd1306.SSD1306_I2C(128, 64, i2c) - -button = Pin(8, Pin.IN, Pin.PULL_UP) # SW1 - -# ---------- GLOBAL VARIABLES ---------- -fifo = Filefifo(10, name="capture_250Hz_02.txt") -prev = fifo.get() -curr = fifo.get() -next_val = fifo.get() -sample_index = 3 -last_peak_index = -MIN_PPI -bpm_list = [] - -measuring = False -last_button = 1 -last_toggle_time = 0 -start_time = utime.ticks_ms() - -# ---------- UI FUNCTION ---------- -def show_screen(bpm=None, measuring=False, ref_bpm=None): - oled.fill(0) - if not measuring: - oled.text("PCN BeatSense", 8, 0) - oled.text("Press SW1 to", 16, 24) - oled.text("start measuring", 8, 36) - else: - oled.text("Measuring...", 24, 0) - if bpm is not None: - oled.text("Your BPM: {}".format(bpm), 0, 16) - if ref_bpm is not None: - diff = abs(bpm - ref_bpm) - percent = int((diff / ref_bpm) * 100) - oled.text("Ref BPM: {}".format(ref_bpm), 0, 26) - oled.text("Diff: {}%".format(percent), 0, 36) - oled.text("OK" if percent <= 10 else "Too far", 0, 48) - else: - oled.text("BPM: ---", 32, 32) - oled.text("SW1 to stop", 0, 56) - oled.show() - -# ---------- INITIAL SCREEN ---------- -show_screen() - -# ---------- MAIN LOOP ---------- -while True: - # --- Handle button press --- - current_button = button.value() - now = utime.ticks_ms() - - if current_button == 0 and last_button == 1 and utime.ticks_diff(now, last_toggle_time) > 300: - measuring = not measuring - last_toggle_time = now - sample_index = 3 - bpm_list = [] - start_time = utime.ticks_ms() - show_screen(None, measuring, ref_bpm=REFERENCE_BPM) - - last_button = current_button - - # --- If measuring, process data --- - if measuring: - utime.sleep_ms(4) - - prev = curr - curr = next_val - next_val = fifo.get() - sample_index += 1 - - if curr > THRESHOLD and curr > prev and curr > next_val: - ppi = sample_index - last_peak_index - if MIN_PPI < ppi < MAX_PPI: - bpm = int(60000 / (ppi * 1000 / SAMPLE_RATE)) - bpm_list.append(bpm) - last_peak_index = sample_index - - # --- Every 5s: display BPM --- - if utime.ticks_diff(utime.ticks_ms(), start_time) >= MEASURE_INTERVAL_MS: - avg_bpm = sum(bpm_list) // len(bpm_list) if bpm_list else None - show_screen(avg_bpm, measuring, ref_bpm=REFERENCE_BPM) - bpm_list = [] - start_time = utime.ticks_ms() -