From 4d9996ff955e2c4b47920aead0a48b5d13ac12e7 Mon Sep 17 00:00:00 2001 From: 1624952103 Date: Mon, 1 Jun 2026 14:36:32 +0800 Subject: [PATCH] =?utf8?q?=E5=A2=9E=E5=8A=A0=E8=A1=80=E5=8E=8B=E6=A0=A1?= =?utf8?q?=E5=87=86=E7=B3=BB=E6=95=B0=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- code/sdk/zhihui/drivers/ppg/hal/ppg_hal.c | 14 ++ code/sdk/zhihui/drivers/ppg/hal/ppg_hal.h | 4 + .../ppg/pah831x/pah_sensor/pah831x_main.c | 154 ++++++++++++++++-- .../ppg/pah831x/pah_sensor/pah831x_main.h | 10 +- 4 files changed, 171 insertions(+), 11 deletions(-) diff --git a/code/sdk/zhihui/drivers/ppg/hal/ppg_hal.c b/code/sdk/zhihui/drivers/ppg/hal/ppg_hal.c index eccb764eb..4ec88b88c 100644 --- a/code/sdk/zhihui/drivers/ppg/hal/ppg_hal.c +++ b/code/sdk/zhihui/drivers/ppg/hal/ppg_hal.c @@ -143,6 +143,20 @@ void ppg_get_bp(int *sbp,int *dbp) #endif } +void ppg_set_bp_cali_data(int *sbp,int *dbp) +{ +#if defined PPG_PAH831X + pah831x_set_bp_cali_data(sbp,dbp); +#endif +} + +bool is_ppg_bp_need_cali(void) +{ +#if defined PPG_PAH831X + return is_pah831x_need_cali(); +#endif + return 1; +} uint8_t ppg_get_wear_status(void) { diff --git a/code/sdk/zhihui/drivers/ppg/hal/ppg_hal.h b/code/sdk/zhihui/drivers/ppg/hal/ppg_hal.h index 25fd7391c..9c5712177 100644 --- a/code/sdk/zhihui/drivers/ppg/hal/ppg_hal.h +++ b/code/sdk/zhihui/drivers/ppg/hal/ppg_hal.h @@ -69,6 +69,10 @@ uint8_t ppg_get_factory_log_switch(void); uint8_t ppg_get_hrv(void); void ppg_get_bp(int *sbp,int *dbp); +void ppg_set_bp_cali_data(int *sbp,int *dbp); + +//是否需要血压校准 1是 0否 +bool is_ppg_bp_need_cali(void); #ifdef __cplusplus #if __cplusplus diff --git a/code/sdk/zhihui/drivers/ppg/pah831x/pah_sensor/pah831x_main.c b/code/sdk/zhihui/drivers/ppg/pah831x/pah_sensor/pah831x_main.c index eebd3ff03..985d06dac 100644 --- a/code/sdk/zhihui/drivers/ppg/pah831x/pah_sensor/pah831x_main.c +++ b/code/sdk/zhihui/drivers/ppg/pah831x/pah_sensor/pah831x_main.c @@ -48,6 +48,7 @@ #define ENABLE_PRINT_ECG_EVENT_DATA (1) #define ENABLE_PRINT_TOUCH_EVENT_DATA (1) +#define FILEPATH "user/userdata/bp_cali_data.bin" //============================================================================ @@ -154,6 +155,8 @@ static uint8_t ret_spo2 = 0; static uint8_t ret_hrv = 0; static uint8_t ret_wear = 0; static ecg_data_out _ecg_data_out_alg; + +static uint8_t bp_cali_cnt = 0; //void demo_pah831x(void); static bool sensor_basic_verification(main_state *this, pah_plat *plat); @@ -1105,8 +1108,8 @@ static int CalValue1_in = 0; static uint8_t calibration_mode = ECG_PPG_BP_ESTIMATE; // 2. 固定基准参数:静态常量,全程不变 -static const uint8_t user_age = 24; -static const float user_height = 173; +static uint8_t user_age = 24; +static uint16_t user_height = 173; static uint16_t Benchmark_SBP = 135; static uint16_t Benchmark_DBP = 85; @@ -1137,16 +1140,19 @@ static void pah831x_ecg_bp_timer_handler(void) }else if(calibration_mode==ECG_PPG_BP_ESTIMATE) { + + pah831x_read_calidata(); + //bp_cal_ready = get_bp_cal_data(cali_value); bp_cal_ready = true ;//read from flash - cali_value[0] = 26 ;//read from flash - cali_value[1] = 9 ;//read from flash - cali_value[2] = 29 ;//read from flash - cali_value[3] = 86 ;//read from flash - cal_sbp_in = cali_value[0]; - cal_dbp_in = cali_value[1]; - CalValue_in = cali_value[2]; - CalValue1_in = cali_value[3]; + // cali_value[0] = 26 ;//read from flash + // cali_value[1] = 9 ;//read from flash + // cali_value[2] = 29 ;//read from flash + // cali_value[3] = 86 ;//read from flash + // cal_sbp_in = cali_value[0]; + // cal_dbp_in = cali_value[1]; + // CalValue_in = cali_value[2]; + // CalValue1_in = cali_value[3]; got_BP = 0 ; Benchmark_SBP = 0 ; Benchmark_DBP = 0 ; @@ -1562,6 +1568,11 @@ void pah831x_ecgbp_stop(void) { pah_sensor_stop(); ecg_bp_algorithm_close(); + + if(calibration_mode == ECG_PPG_BP_CALI) + { + pah831x_save_calidata(); + } } void pah831x_ecg_afib_stop(void) @@ -1862,4 +1873,127 @@ uint8_t pah831x_get_hrv(void) uint8_t pah831x_get_wear_status(void) { return ret_wear; +} + +void pah831x_set_bp_cali_data(int *sbp,int *dbp) +{ + if(sbp == NULL || dbp == NULL) + return; + + Benchmark_SBP = *sbp; + Benchmark_DBP = *dbp; + + int user_info[5] = {0}; + get_user_info_from_config_model(user_info); + user_age = user_info[0]; + user_height = user_info[3]; + + // int user_info[5] = {0}; + // get_user_info_from_config_model(user_info); + // memset(&scl, 0, sizeof(scl)); + // scl.iData[0] = 1; + // scl.iData[1] = user_info[0]; + // scl.iData[2] = !user_info[1]; //APP 1女 0男 + // scl.iData[3] = user_info[2]; + // scl.iData[4] = user_info[3]; + // scl.iData[5] = user_info[4]; + // CWM_OS_dbgPrintf("set user info : age[%d] , sex[%d] , weight[%d] , height[%d] , method[%d] \r\n", + // user_info[0],user_info[1],user_info[2],user_info[3],user_info[4]); +} + +void pah831x_read_calidata(void) +{ + // C89 规范:所有变量定义在函数开头 + int cali_data[5] = {0}; + FILE *fp = NULL; + size_t ret = 0; + + fp = fopen(FILEPATH, "rb"); // 二进制读取,嵌入式/跨平台更安全 + if (fp == NULL) + { + printf("%s fopen failed\n", FILEPATH); + + cal_sbp_in = 26 ;//read from flash + cal_dbp_in = 9 ;//read from flash + CalValue_in = 29 ;//read from flash + CalValue1_in = 86 ;//read from flash + + return; + } + + // 读取数据,并检查是否读取成功(必须判断返回值) + ret = fread(cali_data, sizeof(cali_data), 1, fp); + fclose(fp); // 先关闭文件,再判断结果 + + // 读取失败:直接返回,不覆盖有效数据 + if (ret != 1) + { + printf("read cali data failed!\n"); + + cal_sbp_in = 26 ;//read from flash + cal_dbp_in = 9 ;//read from flash + CalValue_in = 29 ;//read from flash + CalValue1_in = 86 ;//read from flash + + return; + } + + // 数据赋值 + cal_sbp_in = cali_data[0]; + cal_dbp_in = cali_data[1]; + CalValue_in = cali_data[2]; + CalValue1_in = cali_data[3]; + bp_cali_cnt = cali_data[4]; + + printf("read cali data success\n"); +} + +/** + * @brief 保存血压校准数据 + */ +void pah831x_save_calidata(void) +{ + + if(!cal_sbp_in && !cal_dbp_in && !CalValue_in && !CalValue1_in) + { + printf("error cali param!\n"); + return; + } + + // C89 规范:所有变量定义在函数开头 + int cali_data[5] = {0}; + FILE *fp = NULL; + size_t ret = 0; + + // 填充校准数据 + cali_data[0] = cal_sbp_in; + cali_data[1] = cal_dbp_in; + cali_data[2] = CalValue_in; + cali_data[3] = CalValue1_in; + cali_data[4] = ++bp_cali_cnt; // 先保存当前计数值 + + fp = fopen(FILEPATH, "wb"); // 二进制写入,避免文本模式转义 + if (fp == NULL) + { + printf("%s fopen failed\n", FILEPATH); + return; + } + + // 写入数据,并检查是否成功 + ret = fwrite(cali_data, sizeof(cali_data), 1, fp); + fclose(fp); + + if (ret != 1) + { + printf("save cali data failed!\n"); + return; + } + +} + +bool is_pah831x_need_cali(void) +{ + pah831x_read_calidata(); + + return bp_cali_cnt >= 10 ? 0 : 1; } \ No newline at end of file diff --git a/code/sdk/zhihui/drivers/ppg/pah831x/pah_sensor/pah831x_main.h b/code/sdk/zhihui/drivers/ppg/pah831x/pah_sensor/pah831x_main.h index 1be7eeaf2..3a62944be 100644 --- a/code/sdk/zhihui/drivers/ppg/pah831x/pah_sensor/pah831x_main.h +++ b/code/sdk/zhihui/drivers/ppg/pah831x/pah_sensor/pah831x_main.h @@ -370,4 +370,12 @@ uint8_t pah831x_get_spo2(void); uint8_t pah831x_get_hrv(void); -void pah8311_get_bp(int *sbp,int *dbp); \ No newline at end of file +void pah8311_get_bp(int *sbp,int *dbp); + +void pah831x_read_calidata(void); + +void pah831x_save_calidata(void); + +void pah831x_set_bp_cali_data(int *sbp,int *dbp); + +bool is_pah831x_need_cali(void); \ No newline at end of file -- 2.17.1