# OpenHRStrap-IDF **Repository Path**: genvex/open-hrstrap-idf ## Basic Information - **Project Name**: OpenHRStrap-IDF - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-10-27 - **Last Updated**: 2026-10-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # OpenHRStrap-IDF OpenHRStrap 心率带 → **ESP-IDF** 重构版。把原来 Arduino 框架(`analogRead` + DSP 塞在 `esp_timer` 回调里)的写法,重写成现代 ESP-IDF(C)工程。 ## 为什么要重构 | 维度 | 原版 Arduino | 本工程 | |---|---|---| | 采样 | `analogRead`,**无校准**(raw 计数) | `adc_oneshot` + **`adc_cali`**(raw → 真·mV) | | DSP 位置 | **整段 Pan-Tompkins 塞进 `esp_timer` 回调** | **独立 `ecg_task`**,与采样彻底解耦 | | 解耦 | `volatile` 全局 + `noInterrupts()` | FreeRTOS **SPSC 队列** | | BLE | `BLEDevice.h`(Bluedroid)`loop()` 里轮询 `notify()` | **NimBLE** 原生 GATT server,异步 notify | | 可测试 | 算法焊死固件 | `ecg_proc.c` **纯 C 无硬件**,可离线验证 | 收益:数据可信(mV)、采样率可上提、多任务隔离、BLE 时序更稳。 ## 架构 ``` esp_timer (200Hz) ── 只采样 adc_oneshot → 队列(16) │ ▼ ecg_task ── 读队列 → adc_cali(mV) → Pan-Tompkins → R峰/bpm │ ▼ NimBLE host ── 0x180D/0x2A37 NOTIFY [0x00, bpm] → 手机 ``` ## 文件 - `main/app_main.c` 初始化 + 创建 `ecg_task` - `main/adc_sample.c` oneshot + esp_timer 周期读 + 校准 - `main/ecg_proc.c` Pan-Tompkins 逐样本增量 DSP(纯算法) - `main/ble_hr.c` NimBLE 标准心率 GATT server ## 构建(ESP32-S3) 前置:装好 ESP-IDF(本机 `~/.espressif/v6.1/esp-idf`)。 ```bash source ~/.espressif/tools/activate_idf_v6.1.sh # 或 export.sh cd OpenHRStrap-IDF idf.py set-target esp32s3 idf.py build ``` 产物:`build/OpenHRStrap.bin`。 ## 烧录 + 观察 ```bash idf.py -p /dev/ttyUSB0 flash monitor ``` 串口会每秒打一行:`raw=... mv=... | bpm=... peaks=... r=... cali=1`(`cali=1` 表示校准时效)。 ## 接 ECG 引脚(必须按你的板子改) 默认采样 `ADC_CHANNEL_0`。ESP32-S3 的 ADC 通道↔GPIO 随 revision 变,**接 ECG 的那个模拟脚 对应的 `ADC_CHANNEL_x`,二选一改**: - 改 `main/adc_sample.h` 里的默认值;或 - 在 `main/CMakeLists.txt` 末尾加:`target_compile_definitions(app PRIVATE -DECG_ADC_CHANNEL=ADC_CHANNEL_3)` 放大电路的输出要落在 0~3.3V 内(12-bit, DB_12),建议 DC 工作点约 1.65V。 ## 说明 / 取舍 - 无 efuse 校准时自动降级为 raw 代理值并打 WARN,程序不崩。 - NimBLE 需 NVS,`app_main` 已 `nvs_flash_init()`。 - 检测模式固定 mode 3(reactive + 输出限速 1 bpm/s);想换在 `ecg_proc.h` 的 `ECG_MODE_3`。