From 6189ddf36bde5daa36fc2173f511a93cb4e179b4 Mon Sep 17 00:00:00 2001 From: zhaohe Date: Fri, 9 Feb 2024 19:28:16 +0800 Subject: [PATCH] update --- .vscode/settings.json | 4 +- README.md | 7 +- app/app.uvoptx | 217 ++++++++++++++++++---------------- app/app.uvprojx | 30 +++++ app/config/sdk_config.h | 14 +-- app/src/ble_cmd_process_service.c | 8 +- app/src/board/board.h | 16 ++- app/src/board/board_adc_module_ctrl.c | 55 +++++++++ app/src/board/board_adc_module_ctrl.h | 13 ++ app/src/board/board_battery_state.c | 68 +++++++++++ app/src/board/board_battery_state.h | 17 +++ app/src/board/board_beep_ctrl.c | 7 -- app/src/board/board_ecg_sensor.c | 60 ++++++++++ app/src/board/board_ecg_sensor.h | 20 ++++ app/src/device_state.h | 1 - app/src/display_manager.c | 10 +- app/src/heart_wave_sample_service.c | 45 ++++--- app/src/one_conduction_board.c | 105 ---------------- app/src/one_conduction_board.h | 15 +-- app/src/one_conduction_main.c | 174 ++++++++++----------------- libznordic | 2 +- 21 files changed, 510 insertions(+), 378 deletions(-) create mode 100644 app/src/board/board_adc_module_ctrl.c create mode 100644 app/src/board/board_adc_module_ctrl.h create mode 100644 app/src/board/board_battery_state.c create mode 100644 app/src/board/board_battery_state.h create mode 100644 app/src/board/board_ecg_sensor.c create mode 100644 app/src/board/board_ecg_sensor.h diff --git a/.vscode/settings.json b/.vscode/settings.json index f4cb0be..2da517e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -115,7 +115,9 @@ "filter.h": "c", "filters.h": "c", "nrfx_rtc.h": "c", - "device_state.h": "c" + "device_state.h": "c", + "board_adc_module_ctrl.h": "c", + "board_ecg_sensor.h": "c" }, "files.encoding": "gbk" } \ No newline at end of file diff --git a/README.md b/README.md index 1cc331a..db34dd2 100644 --- a/README.md +++ b/README.md @@ -2,14 +2,17 @@ ``` TODO -0. 支持无线升级,修改SN码 -6. 开始采集蜂鸣器响响三声,结束采集蜂鸣器响三声 +0. 支持无线升级,修改SN码 OK +6. 开始采集蜂鸣器响响一声,结束采集蜂鸣器响一声 OK +2. 不足三十30,响三声 OK +3. 添加是否充电检测 1. 添加逻辑,如果未配置时间,设备进入深度休眠。 2. 修改电池电量相关逻辑,校准电池电量。 3. 修改心率采样ADC参考电压 4. 修改屏幕显示位置 5. 充电中,限制用户操作 6. 调整ADC放大倍数 +7. 开机时进行低电量检测 测试: diff --git a/app/app.uvoptx b/app/app.uvoptx index 972c44d..6789123 100644 --- a/app/app.uvoptx +++ b/app/app.uvoptx @@ -148,24 +148,7 @@ UL2CM3(-S0 -C0 -P0 -FD20000000 -FC4000 -FN2 -FF0nrf52xxx -FS00 -FL0200000 -FF1nrf52xxx_uicr -FS110001000 -FL11000 -FP0($$Device:nRF52833_xxAA$Flash\nrf52xxx.flm) -FP1($$Device:nRF52833_xxAA$Flash\nrf52xxx_uicr.flm)) - - - 0 - 0 - 19 - 1 -
189332
- 0 - 0 - 0 - 0 - 0 - 1 - .\src\main.c - - \\app\src/main.c\19 -
-
+ 0 @@ -624,6 +607,42 @@ 0 0 + + 1 + 21 + 1 + 0 + 0 + 0 + .\src\board\board_adc_module_ctrl.c + board_adc_module_ctrl.c + 0 + 0 + + + 1 + 22 + 1 + 0 + 0 + 0 + .\src\board\board_battery_state.c + board_battery_state.c + 0 + 0 + + + 1 + 23 + 1 + 0 + 0 + 0 + .\src\board\board_ecg_sensor.c + board_ecg_sensor.c + 0 + 0 + @@ -634,7 +653,7 @@ 0 2 - 21 + 24 1 0 0 @@ -654,7 +673,7 @@ 0 3 - 22 + 25 1 0 0 @@ -666,7 +685,7 @@ 3 - 23 + 26 1 0 0 @@ -686,7 +705,7 @@ 0 4 - 24 + 27 1 0 0 @@ -706,7 +725,7 @@ 0 5 - 25 + 28 1 0 0 @@ -718,7 +737,7 @@ 5 - 26 + 29 1 0 0 @@ -730,7 +749,7 @@ 5 - 27 + 30 1 0 0 @@ -742,7 +761,7 @@ 5 - 28 + 31 1 0 0 @@ -754,7 +773,7 @@ 5 - 29 + 32 1 0 0 @@ -766,7 +785,7 @@ 5 - 30 + 33 1 0 0 @@ -778,7 +797,7 @@ 5 - 31 + 34 1 0 0 @@ -790,7 +809,7 @@ 5 - 32 + 35 1 0 0 @@ -810,7 +829,7 @@ 0 6 - 33 + 36 1 0 0 @@ -830,7 +849,7 @@ 0 7 - 34 + 37 1 0 0 @@ -842,7 +861,7 @@ 7 - 35 + 38 1 0 0 @@ -854,7 +873,7 @@ 7 - 36 + 39 1 0 0 @@ -866,7 +885,7 @@ 7 - 37 + 40 1 0 0 @@ -878,7 +897,7 @@ 7 - 38 + 41 1 0 0 @@ -890,7 +909,7 @@ 7 - 39 + 42 1 0 0 @@ -902,7 +921,7 @@ 7 - 40 + 43 1 0 0 @@ -914,7 +933,7 @@ 7 - 41 + 44 1 0 0 @@ -926,7 +945,7 @@ 7 - 42 + 45 1 0 0 @@ -938,7 +957,7 @@ 7 - 43 + 46 1 0 0 @@ -950,7 +969,7 @@ 7 - 44 + 47 1 0 0 @@ -962,7 +981,7 @@ 7 - 45 + 48 1 0 0 @@ -974,7 +993,7 @@ 7 - 46 + 49 1 0 0 @@ -986,7 +1005,7 @@ 7 - 47 + 50 1 0 0 @@ -998,7 +1017,7 @@ 7 - 48 + 51 1 0 0 @@ -1010,7 +1029,7 @@ 7 - 49 + 52 1 0 0 @@ -1022,7 +1041,7 @@ 7 - 50 + 53 1 0 0 @@ -1034,7 +1053,7 @@ 7 - 51 + 54 1 0 0 @@ -1046,7 +1065,7 @@ 7 - 52 + 55 1 0 0 @@ -1058,7 +1077,7 @@ 7 - 53 + 56 1 0 0 @@ -1078,7 +1097,7 @@ 0 8 - 54 + 57 1 0 0 @@ -1090,7 +1109,7 @@ 8 - 55 + 58 1 0 0 @@ -1102,7 +1121,7 @@ 8 - 56 + 59 1 0 0 @@ -1114,7 +1133,7 @@ 8 - 57 + 60 1 0 0 @@ -1126,7 +1145,7 @@ 8 - 58 + 61 1 0 0 @@ -1138,7 +1157,7 @@ 8 - 59 + 62 1 0 0 @@ -1150,7 +1169,7 @@ 8 - 60 + 63 1 0 0 @@ -1162,7 +1181,7 @@ 8 - 61 + 64 1 0 0 @@ -1174,7 +1193,7 @@ 8 - 62 + 65 1 0 0 @@ -1186,7 +1205,7 @@ 8 - 63 + 66 1 0 0 @@ -1198,7 +1217,7 @@ 8 - 64 + 67 1 0 0 @@ -1210,7 +1229,7 @@ 8 - 65 + 68 1 0 0 @@ -1222,7 +1241,7 @@ 8 - 66 + 69 1 0 0 @@ -1234,7 +1253,7 @@ 8 - 67 + 70 1 0 0 @@ -1246,7 +1265,7 @@ 8 - 68 + 71 1 0 0 @@ -1258,7 +1277,7 @@ 8 - 69 + 72 1 0 0 @@ -1270,7 +1289,7 @@ 8 - 70 + 73 1 0 0 @@ -1282,7 +1301,7 @@ 8 - 71 + 74 1 0 0 @@ -1294,7 +1313,7 @@ 8 - 72 + 75 1 0 0 @@ -1306,7 +1325,7 @@ 8 - 73 + 76 1 0 0 @@ -1318,7 +1337,7 @@ 8 - 74 + 77 1 0 0 @@ -1330,7 +1349,7 @@ 8 - 75 + 78 1 0 0 @@ -1342,7 +1361,7 @@ 8 - 76 + 79 1 0 0 @@ -1354,7 +1373,7 @@ 8 - 77 + 80 1 0 0 @@ -1366,7 +1385,7 @@ 8 - 78 + 81 1 0 0 @@ -1378,7 +1397,7 @@ 8 - 79 + 82 1 0 0 @@ -1390,7 +1409,7 @@ 8 - 80 + 83 1 0 0 @@ -1410,7 +1429,7 @@ 0 9 - 81 + 84 1 0 0 @@ -1422,7 +1441,7 @@ 9 - 82 + 85 1 0 0 @@ -1434,7 +1453,7 @@ 9 - 83 + 86 1 0 0 @@ -1446,7 +1465,7 @@ 9 - 84 + 87 1 0 0 @@ -1458,7 +1477,7 @@ 9 - 85 + 88 1 0 0 @@ -1470,7 +1489,7 @@ 9 - 86 + 89 1 0 0 @@ -1490,7 +1509,7 @@ 0 10 - 87 + 90 1 0 0 @@ -1502,7 +1521,7 @@ 10 - 88 + 91 1 0 0 @@ -1514,7 +1533,7 @@ 10 - 89 + 92 1 0 0 @@ -1534,7 +1553,7 @@ 0 11 - 90 + 93 1 0 0 @@ -1546,7 +1565,7 @@ 11 - 91 + 94 1 0 0 @@ -1558,7 +1577,7 @@ 11 - 92 + 95 1 0 0 @@ -1578,7 +1597,7 @@ 0 12 - 93 + 96 1 0 0 @@ -1590,7 +1609,7 @@ 12 - 94 + 97 1 0 0 @@ -1602,7 +1621,7 @@ 12 - 95 + 98 1 0 0 @@ -1622,7 +1641,7 @@ 0 13 - 96 + 99 1 0 0 @@ -1634,7 +1653,7 @@ 13 - 97 + 100 1 0 0 @@ -1654,7 +1673,7 @@ 0 14 - 98 + 101 1 0 0 @@ -1666,7 +1685,7 @@ 14 - 99 + 102 1 0 0 @@ -1678,7 +1697,7 @@ 14 - 100 + 103 1 0 0 @@ -1690,7 +1709,7 @@ 14 - 101 + 104 1 0 0 diff --git a/app/app.uvprojx b/app/app.uvprojx index 73e2ba1..bd2f398 100644 --- a/app/app.uvprojx +++ b/app/app.uvprojx @@ -483,6 +483,21 @@ 1 .\src\board\board_beep_ctrl.c + + board_adc_module_ctrl.c + 1 + .\src\board\board_adc_module_ctrl.c + + + board_battery_state.c + 1 + .\src\board\board_battery_state.c + + + board_ecg_sensor.c + 1 + .\src\board\board_ecg_sensor.c + @@ -4347,6 +4362,21 @@ 1 .\src\board\board_beep_ctrl.c + + board_adc_module_ctrl.c + 1 + .\src\board\board_adc_module_ctrl.c + + + board_battery_state.c + 1 + .\src\board\board_battery_state.c + + + board_ecg_sensor.c + 1 + .\src\board\board_ecg_sensor.c + diff --git a/app/config/sdk_config.h b/app/config/sdk_config.h index 908361c..54116c9 100644 --- a/app/config/sdk_config.h +++ b/app/config/sdk_config.h @@ -3992,21 +3992,21 @@ #ifndef NRFX_TIMER1_ENABLED -#define NRFX_TIMER1_ENABLED 0 +#define NRFX_TIMER1_ENABLED 1 #endif // NRFX_TIMER2_ENABLED - Enable TIMER2 instance #ifndef NRFX_TIMER2_ENABLED -#define NRFX_TIMER2_ENABLED 0 +#define NRFX_TIMER2_ENABLED 1 #endif // NRFX_TIMER3_ENABLED - Enable TIMER3 instance #ifndef NRFX_TIMER3_ENABLED -#define NRFX_TIMER3_ENABLED 0 +#define NRFX_TIMER3_ENABLED 1 #endif // NRFX_TIMER4_ENABLED - Enable TIMER4 instance @@ -5772,14 +5772,14 @@ #ifndef TIMER2_ENABLED -#define TIMER2_ENABLED 0 +#define TIMER2_ENABLED 1 #endif // TIMER3_ENABLED - Enable TIMER3 instance #ifndef TIMER3_ENABLED -#define TIMER3_ENABLED 0 +#define TIMER3_ENABLED 1 #endif // TIMER4_ENABLED - Enable TIMER4 instance @@ -7734,7 +7734,7 @@ // NRF_LOG_BACKEND_RTT_ENABLED - nrf_log_backend_rtt - Log RTT backend //========================================================== #ifndef NRF_LOG_BACKEND_RTT_ENABLED -#define NRF_LOG_BACKEND_RTT_ENABLED 0 +#define NRF_LOG_BACKEND_RTT_ENABLED 1 #endif // NRF_LOG_BACKEND_RTT_TEMP_BUFFER_SIZE - Size of buffer for partially processed strings. // Size of the buffer is a trade-off between RAM usage and processing. @@ -7812,7 +7812,7 @@ // NRF_LOG_ENABLED - nrf_log - Logger //========================================================== #ifndef NRF_LOG_ENABLED -#define NRF_LOG_ENABLED 0 +#define NRF_LOG_ENABLED 1 #endif // Log message pool - Configuration of log message pool diff --git a/app/src/ble_cmd_process_service.c b/app/src/ble_cmd_process_service.c index 8eb34c7..0d910df 100644 --- a/app/src/ble_cmd_process_service.c +++ b/app/src/ble_cmd_process_service.c @@ -8,6 +8,8 @@ #include "zble_module.h" #include "zdatachannel_service.h" #include "basic/device_version_info_mgr.h" +#include "board/board_ecg_sensor.h" +#include "board/board_battery_state.h" APP_TIMER_DEF(m_record_upload_tmr); // 数据上报定时器 APP_TIMER_DEF(m_record_upload_finish_packet_report_tmr); // 数据上报完成上报定时器 @@ -296,15 +298,15 @@ void ble_cmder_process_rx(uint8_t* rx, int len) { device_state_receipt_t* receipt = (device_state_receipt_t*)txheader->data; uint16_t sendlen = sizeof(ify_hrs_packet_t) + sizeof(device_state_receipt_t); - receipt->drop_state0 = (!SingleLeadECG_ecg_plod_get_connected_state()); + receipt->drop_state0 = (!BoardEcgSensor_plod_get_connected_state()); receipt->drop_state1 = 0x00; receipt->device_state0.sampling_state = (ds_now_state() == kdevice_state_sampling); receipt->device_state0.report_state = m_realtime_report_state; - receipt->device_state0.low_battery = (SingleLeadECG_battery_val() < 20); + receipt->device_state0.low_battery = (BoardBattery_get_battery_level() < 20); receipt->device_state0.full_storge = (sample_data_mgr_storage_is_full()); receipt->device_state1 = 0; - receipt->powerlevel = SingleLeadECG_battery_val(); + receipt->powerlevel = BoardBattery_get_battery_level(); receipt->storage_item_num = sample_data_mgr_get_file_num(); zdatachannel_data_send2(txbuf, sendlen); diff --git a/app/src/board/board.h b/app/src/board/board.h index c2f8de9..cfe8963 100644 --- a/app/src/board/board.h +++ b/app/src/board/board.h @@ -6,7 +6,18 @@ #define BEEP_PIN 1 #define BEEP_PWM_INSTANCE 0 +/******************************************************************************* + * ADC * + *******************************************************************************/ + +#define ECG_NLOD_PIN 3 +#define ECG_PLOD_PIN 28 +#define ECG_ADC_PIN NRF_SAADC_INPUT_AIN0 +#define ECG_ADC_CHANNEL 0 // 不重复即可 +#define BATTERY_ADC_PIN NRF_SAADC_INPUT_AIN3 +#define BATTERY_ADC_CHANNEL 1 // 不重复即可 +#define BATTERY_CHARGE_DETECT_PIN 18 /** * @brief @@ -21,4 +32,7 @@ #define SAMPLE_MIN_TIME_S (30.0) // 采样最小时间 #define LITTLE_DATA_BLOCK_FRAME_NUM 5 // 每次多少帧上报一次 -#define KEEP_STILL_OVERTIME_MS_1P5 2000 // 保持静止超时时间的1/6 +#define KEEP_STILL_OVERTIME_MS_1P5 2000 // 保持静止超时时间的1/6 + + +#define HEART_WAVE_SAMPLE_TMR_INSTANCE 3 \ No newline at end of file diff --git a/app/src/board/board_adc_module_ctrl.c b/app/src/board/board_adc_module_ctrl.c new file mode 100644 index 0000000..f70fd81 --- /dev/null +++ b/app/src/board/board_adc_module_ctrl.c @@ -0,0 +1,55 @@ + +#include "board_adc_module_ctrl.h" + +/******************************************************************************* + * ADC * + *******************************************************************************/ + +static uint8_t m_now_adc_channel = 0xff; +static int32_t m_load_times = 0; + +void BoardAdc_set_ch(uint8_t ch); +void BoardAdc_init() {} + +void BoardAdc_load() { + if (m_load_times == 0) { + nrf_drv_saadc_config_t adccfg = NRFX_SAADC_DEFAULT_CONFIG; + adccfg.resolution = NRF_SAADC_RESOLUTION_12BIT; // 4096 等于满采样率 + ZERROR_CHECK(nrf_drv_saadc_init(&adccfg, NULL)); + m_load_times++; + } else { + m_load_times += 1; + } +} +void BoardAdc_unload() { + if (m_load_times == 1) { + BoardAdc_set_ch(0xff); + nrf_drv_saadc_uninit(); + m_load_times = 0; + } else { + m_load_times -= 1; + } + if (m_load_times < 0) { + m_load_times = 0; + } +} +void BoardAdc_set_ch(uint8_t ch) { + if (m_now_adc_channel != ch) { + if (m_now_adc_channel != 0xff) nrfx_saadc_channel_uninit(m_now_adc_channel); + if (ch == ECG_ADC_CHANNEL) { + nrf_saadc_channel_config_t channel_config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(ECG_ADC_PIN); + channel_config.acq_time = NRF_SAADC_ACQTIME_40US; + ZERROR_CHECK(nrfx_saadc_channel_init(ECG_ADC_CHANNEL, &channel_config)); + } else if (ch == BATTERY_ADC_CHANNEL) { + nrf_saadc_channel_config_t channel_config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(BATTERY_ADC_PIN); + channel_config.acq_time = NRF_SAADC_ACQTIME_40US; + ZERROR_CHECK(nrfx_saadc_channel_init(BATTERY_ADC_CHANNEL, &channel_config)); + } + } + m_now_adc_channel = ch; +} + +int16_t BoardAdc_get_val(uint8_t ch) { + BoardAdc_set_ch(ch); + return znrf_adc_channel_read_val(ch); +} diff --git a/app/src/board/board_adc_module_ctrl.h b/app/src/board/board_adc_module_ctrl.h new file mode 100644 index 0000000..28b27a1 --- /dev/null +++ b/app/src/board/board_adc_module_ctrl.h @@ -0,0 +1,13 @@ +#pragma once + +#include +#include + +#include "board/board.h" +#include "znordic.h" + +void BoardAdc_init(); +void BoardAdc_load(); +void BoardAdc_unload(); + +int16_t BoardAdc_get_val(uint8_t ch); diff --git a/app/src/board/board_battery_state.c b/app/src/board/board_battery_state.c new file mode 100644 index 0000000..7d4166c --- /dev/null +++ b/app/src/board/board_battery_state.c @@ -0,0 +1,68 @@ + +#include "board_battery_state.h" + +#include "app_event.h" +#include "board_adc_module_ctrl.h" +/******************************************************************************* + * Battery * + *******************************************************************************/ +APP_TIMER_DEF(battery_state_detect_tmr); // +static bool m_battery_is_chargeing = false; // + +static bool battery_get_chargeing_state() { return !nrf_gpio_pin_read(BATTERY_CHARGE_DETECT_PIN); } +static void module_tmr_cb(void *context) { // + bool chargeing_state = battery_get_chargeing_state(); + if (chargeing_state != m_battery_is_chargeing) { + app_event_t event; + memset(&event, 0, sizeof(event)); + if (chargeing_state) { + ZLOGI("battery is charging"); + event.eventType = kbattery_start_charge_event; + } else { + ZLOGI("battery end charging"); + event.eventType = kbattery_end_charge_event; + } + AppEvent_pushEvent(&event); + } + m_battery_is_chargeing = chargeing_state; +} + +void BoardBattery_init() { + /** + * @brief 500ms启动定时器500ms检查一次当前是否正在充电 + */ + nrf_gpio_cfg_sense_input(BATTERY_CHARGE_DETECT_PIN, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_NOSENSE); + app_timer_create(&battery_state_detect_tmr, APP_TIMER_MODE_REPEATED, module_tmr_cb); + app_timer_start(battery_state_detect_tmr, APP_TIMER_TICKS(500), NULL); +} +void BoardBattery_load() { BoardAdc_load(); } +void BoardBattery_unload() { BoardAdc_unload(); } + +int16_t BoardBattery_sence_gpio_init_before_sleep() { nrf_gpio_cfg_sense_input(BATTERY_CHARGE_DETECT_PIN, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_LOW); } + +int16_t BoardBattery_get_adc_val() { + int16_t val = BoardAdc_get_val(BATTERY_ADC_CHANNEL); + return val; +} +int16_t BoardBattery_get_charging_state() { // + return !nrf_gpio_pin_read(BATTERY_CHARGE_DETECT_PIN); +} +int16_t BoardBattery_get_battery_level() { + static const float maxv = 4.0; + static const float minv = 3.3; + + int16_t rawval = BoardBattery_get_adc_val(); + + float voltage = rawval / 4096.0 * 3.6 / 2.0 * 3; + if (voltage > maxv) voltage = maxv; + if (voltage < minv) voltage = minv; + + float percent = (voltage - minv) / (maxv - minv) * 100 + (float)0.1 /*加0.1是为了避免999.999时显示电量为90*/; + int16_t percent_int = (int16_t)percent; + if (percent_int < 0) { + percent_int = 0; + } else if (percent_int > 100) { + percent_int = 100; + } + return percent_int; +} diff --git a/app/src/board/board_battery_state.h b/app/src/board/board_battery_state.h new file mode 100644 index 0000000..2fbe4ed --- /dev/null +++ b/app/src/board/board_battery_state.h @@ -0,0 +1,17 @@ +#pragma once + +#include +#include + +#include "board/board.h" +#include "znordic.h" + +void BoardBattery_init(); +void BoardBattery_load(); +void BoardBattery_unload(); + +int16_t BoardBattery_get_adc_val(); +int16_t BoardBattery_get_battery_level(); // 0->100 +int16_t BoardBattery_get_charging_state(); + +int16_t BoardBattery_sence_gpio_init_before_sleep(); diff --git a/app/src/board/board_beep_ctrl.c b/app/src/board/board_beep_ctrl.c index 1803e7d..4865c84 100644 --- a/app/src/board/board_beep_ctrl.c +++ b/app/src/board/board_beep_ctrl.c @@ -129,12 +129,5 @@ void BoardBeepCtrl_setEffect(BoardBeepEffect_t effect) { #else -void BoardBeepCtrl_init(void) {} -void BoardBeepCtrl_load() {} -void BoardBeepCtrl_unload() {} - -void BoardBeepCtrl_set(uint8_t state) {} - -void BoardBeepCtrl_setEffect(BoardBeepEffect_t effect) {} #endif \ No newline at end of file diff --git a/app/src/board/board_ecg_sensor.c b/app/src/board/board_ecg_sensor.c new file mode 100644 index 0000000..0b08f15 --- /dev/null +++ b/app/src/board/board_ecg_sensor.c @@ -0,0 +1,60 @@ +#include "board_ecg_sensor.h" + +#include "app_event_distribute.h" +#include "board_adc_module_ctrl.h" + +APP_TIMER_DEF(module_tmr); // + +// 导联连接状态事件生成器 +static bool m_plod_state_connected_state = false; +static uint32_t m_connecte_state_last_trigger_timepoint = 0; + +static uint32_t plod_state_has_disconnected_ms() { return znordic_haspassed_ms(m_connecte_state_last_trigger_timepoint); } +static bool plod_state_is_connected() { return m_plod_state_connected_state; } + +static void module_tmr_cb(void *context) { // + static uint32_t connecte_state_trigger_cnt = 0; + static app_event_t appevent; + memset(&appevent, 0, sizeof(appevent)); + + if (BoardEcgSensor_plod_get_connected_state()) { + connecte_state_trigger_cnt++; + m_connecte_state_last_trigger_timepoint = znordic_getpower_on_ms(); + } else { + connecte_state_trigger_cnt = 0; + } + + if (!m_plod_state_connected_state && connecte_state_trigger_cnt >= 10) { + // 连接事件 + appevent.eventType = kplod_connected_event; + m_plod_state_connected_state = true; + AppEvent_pushEvent(&appevent); + } else if (m_plod_state_connected_state && connecte_state_trigger_cnt == 0) { + // 断开事件 + appevent.eventType = kplod_disconnected_event; + m_plod_state_connected_state = false; + AppEvent_pushEvent(&appevent); + } +} + +void BoardEcgSensor_init() { + nrf_gpio_cfg_sense_input(ECG_NLOD_PIN, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_NOSENSE); + nrf_gpio_cfg_sense_input(ECG_PLOD_PIN, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_NOSENSE); // 低有效 + + app_timer_create(&module_tmr, APP_TIMER_MODE_REPEATED, module_tmr_cb); + app_timer_start(module_tmr, APP_TIMER_TICKS(100), NULL); +} +void BoardEcgSensor_load() { BoardAdc_load(); } +void BoardEcgSensor_unload() { BoardAdc_unload(); } + +int16_t BoardEcgSensor_nlod_get_connected_state() { return nrf_gpio_pin_read(ECG_NLOD_PIN); } +int16_t BoardEcgSensor_plod_get_connected_state() { return !nrf_gpio_pin_read(ECG_PLOD_PIN); } +int16_t BoardEcgSensor_plod_get_ecg_val() { return BoardAdc_get_val(ECG_ADC_CHANNEL); } + +int16_t BoardEcgSensor_plod_get_connected_state_after_filter() { return plod_state_is_connected(); } +int16_t BoardEcgSensor_plod_state_has_disconnected_ms() { return plod_state_has_disconnected_ms(); } + +int16_t BoardEcgSensor_set_sence_state() { + nrf_gpio_cfg_sense_input(ECG_PLOD_PIN, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_LOW); + return 0; +} diff --git a/app/src/board/board_ecg_sensor.h b/app/src/board/board_ecg_sensor.h new file mode 100644 index 0000000..452bd6a --- /dev/null +++ b/app/src/board/board_ecg_sensor.h @@ -0,0 +1,20 @@ +#pragma once + +#include +#include + +#include "board/board.h" +#include "znordic.h" + +void BoardEcgSensor_init(); +void BoardEcgSensor_load(); +void BoardEcgSensor_unload(); + +int16_t BoardEcgSensor_nlod_get_connected_state(); +int16_t BoardEcgSensor_plod_get_connected_state(); +int16_t BoardEcgSensor_plod_get_connected_state_after_filter(); +int16_t BoardEcgSensor_plod_state_has_disconnected_ms(); + +int16_t BoardEcgSensor_plod_get_ecg_val(); + +int16_t BoardEcgSensor_set_sence_state(); diff --git a/app/src/device_state.h b/app/src/device_state.h index d259566..ca75ed9 100644 --- a/app/src/device_state.h +++ b/app/src/device_state.h @@ -9,7 +9,6 @@ typedef enum { kplod_connecting_event, // 导联连接中事件 kbattery_start_charge_event, // 充电事件 - kbattery_charging_event, // 充电中 kbattery_end_charge_event, // 充电结束事件 kevent_tmr_scheduler_event, // 定时器调度事件 diff --git a/app/src/display_manager.c b/app/src/display_manager.c index a71ff9a..f9dd181 100644 --- a/app/src/display_manager.c +++ b/app/src/display_manager.c @@ -11,6 +11,7 @@ #include "font.h" #include "heart_wave_sample_service.h" #include "one_conduction_board.h" +#include "board/board_battery_state.h" PageState_t g_pageState; void dsp_mgr_change_to_page(page_t page) { // @@ -58,6 +59,7 @@ int compute_y_pos_by_center(int16_t y, int16_t height) { return y - height / 2; * 1. 电池电量变化 * */ + static uint32_t m_nowshowbatterylevel; // 当前显示的电池电量 void dsp_mgr_change_to_chargingPage() { @@ -65,8 +67,8 @@ void dsp_mgr_change_to_chargingPage() { uint8_t x = compute_x_pos_by_center(SCREEN_CENTER_X, CHARGE_BATTERY_WIDTH); uint8_t y = compute_y_pos_by_center(SCREEN_CENTER_Y, CHARGE_BATTERY_HEIGHT); uint8_t xchange, ychange; - ssd1306_basic_draw_battery_level(x, y, &xchange, &ychange, SingleLeadECG_battery_val(), CHARGE_BATTERY_WIDTH, CHARGE_BATTERY_HEIGHT); - m_nowshowbatterylevel = SingleLeadECG_battery_val(); + ssd1306_basic_draw_battery_level(x, y, &xchange, &ychange, BoardBattery_get_battery_level(), CHARGE_BATTERY_WIDTH, CHARGE_BATTERY_HEIGHT); + m_nowshowbatterylevel = BoardBattery_get_battery_level(); ssd1306_basic_gram_update(); dsp_mgr_change_to_page(kPage_chargingPage); } @@ -88,7 +90,7 @@ void chargingPage_schedule() { // last_update_time = znordic_getpower_on_ms(); m_nowshowbatterylevel += 5; if (m_nowshowbatterylevel > 100) { - m_nowshowbatterylevel = SingleLeadECG_battery_val(); + m_nowshowbatterylevel = BoardBattery_get_battery_level(); } chargingPage_set_batteryLevel(m_nowshowbatterylevel); } @@ -125,7 +127,7 @@ void mainPage_show(bool colon_state) { x = compute_x_pos_by_center(SCREEN_CENTER_X, fontclocklib.widthPixel * 5 + MAIN_PAGE_BATTERY_WIDTH); y = compute_y_pos_by_center(SCREEN_CENTER_Y, fontclocklib.heightPixel + 1); znordic_rtc_gettime(&now); - batterylevel = SingleLeadECG_battery_val(); + batterylevel = BoardBattery_get_battery_level(); ssd1306_basic_clear_gram(); diff --git a/app/src/heart_wave_sample_service.c b/app/src/heart_wave_sample_service.c index 96b3872..80fd58e 100644 --- a/app/src/heart_wave_sample_service.c +++ b/app/src/heart_wave_sample_service.c @@ -2,10 +2,9 @@ #include "app_event.h" #include "app_event_distribute.h" +#include "board/board_ecg_sensor.h" #include "nrfx_timer.h" #include "one_conduction_board.h" -static const nrfx_timer_t m_timer = NRFX_TIMER_INSTANCE(1); /**< Timer used for channel sweeps and tx with duty cycle. */ -static bool m_timer_started = false; /**< True if timer is running. */ static uint16_t m_capture_buffer_a[128]; static uint16_t m_capture_buffer_b[128]; @@ -93,7 +92,7 @@ static inline void prvf_light_block_trigger_event() { } void nrfx_timer_event_handler(nrf_timer_event_t event_type, void* p_context) { // - uint16_t val = SingleLeadECG_ecg_plod_get_ecg_val(); // 12bit + uint16_t val = BoardEcgSensor_plod_get_ecg_val(); // 12bit m_frame_index++; /******************************************************************************* @@ -135,30 +134,28 @@ void nrfx_timer_event_handler(nrf_timer_event_t event_type, void* p_context) { prvf_light_block_cache_clear(); } } +static const nrfx_timer_t m_timer = NRFX_TIMER_INSTANCE(HEART_WAVE_SAMPLE_TMR_INSTANCE); void hwss_init(void) { - if (m_timer_started) { - return; - } - /** - * @brief 初始化定时器 - */ - nrfx_err_t err; - nrfx_timer_config_t timer_cfg = { - .frequency = NRF_TIMER_FREQ_125kHz, - .mode = NRF_TIMER_MODE_TIMER, - .bit_width = NRF_TIMER_BIT_WIDTH_24, - .p_context = NULL, - .interrupt_priority = NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY, - }; - - err = nrfx_timer_init(&m_timer, &timer_cfg, nrfx_timer_event_handler); - if (err != NRFX_SUCCESS) { - NRF_LOG_ERROR("nrfx_timer_init failed with: %d\n", err); + static bool m_timer_inited = false; + if (!m_timer_inited) { + /** + * @brief 初始化定时器 + */ + static nrfx_timer_config_t timer_cfg = { + .frequency = NRF_TIMER_FREQ_500kHz, + .mode = NRF_TIMER_MODE_TIMER, + .bit_width = NRF_TIMER_BIT_WIDTH_24, + .p_context = NULL, + .interrupt_priority = NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY, + }; + + // nrfx_timer_init(&m_timer, &timer_cfg, nrfx_timer_event_handler); + ZERROR_CHECK(nrfx_timer_init(&m_timer, &timer_cfg, nrfx_timer_event_handler)); + uint32_t timer_ticks = nrfx_timer_ms_to_ticks(&m_timer, 5); // 200HZ + nrfx_timer_extended_compare(&m_timer, NRF_TIMER_CC_CHANNEL0, timer_ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true); + m_timer_inited = true; } - uint32_t timer_ticks = nrfx_timer_ms_to_ticks(&m_timer, 5); // 200HZ - nrfx_timer_extended_compare(&m_timer, NRF_TIMER_CC_CHANNEL0, timer_ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true); - m_timer_started = true; } void hwss_uninit(void) { nrfx_timer_disable(&m_timer); } diff --git a/app/src/one_conduction_board.c b/app/src/one_conduction_board.c index 0c206b0..7cd9e25 100644 --- a/app/src/one_conduction_board.c +++ b/app/src/one_conduction_board.c @@ -26,68 +26,16 @@ #define LED_GREEN_PIN 9 #define LED_BLUE_PIN 10 -// #define LED_GREEN_PIN 27 -// #define LED_BLUE_PIN 28 - -#define ECG_NLOD_PIN 3 -#define ECG_PLOD_PIN 28 -#define ECG_ADC_PIN NRF_SAADC_INPUT_AIN0 -#define ECG_ADC_CHANNEL 0 // 不重复即可 - -#define BATTERY_ADC_PIN NRF_SAADC_INPUT_AIN3 -#define BATTERY_ADC_CHANNEL 1 // 不重复即可 #define EEPROM_I2C_SCL_M 15 // I2C SCL引脚 #define EEPROM_I2C_SDA_M 17 // I2C SDA引脚 #define EEPROM_I2C_INSTANCE 1 // I2C使用的硬件控制器ID - - -#define BATTERY_CHARGE_DETECT_PIN 18 - /******************************************************************************* * TOOLS * *******************************************************************************/ -/******************************************************************************* - * ADC * - *******************************************************************************/ -uint8_t m_now_adc_channel = 0xff; - -void SingleLeadECG_adc_set_ch(uint8_t ch); -void SingleLeadECG_adc_module_init() { - nrf_drv_saadc_config_t adccfg = NRFX_SAADC_DEFAULT_CONFIG; - adccfg.resolution = NRF_SAADC_RESOLUTION_12BIT; // 4096 等于满采样率 - ZERROR_CHECK(nrf_drv_saadc_init(&adccfg, NULL)); - // SingleLeadECG_adc_set_ch(BATTERY_ADC_CHANNEL); -} -void SingleLeadECG_adc_set_ch(uint8_t ch) { - if (m_now_adc_channel != ch) { - if (m_now_adc_channel != 0xff) nrfx_saadc_channel_uninit(m_now_adc_channel); - - if (ch == ECG_ADC_CHANNEL) { - nrf_saadc_channel_config_t channel_config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(ECG_ADC_PIN); - channel_config.acq_time = NRF_SAADC_ACQTIME_40US; - ZERROR_CHECK(nrfx_saadc_channel_init(ECG_ADC_CHANNEL, &channel_config)); - } else if (ch == BATTERY_ADC_CHANNEL) { - nrf_saadc_channel_config_t channel_config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(BATTERY_ADC_PIN); - channel_config.acq_time = NRF_SAADC_ACQTIME_40US; - ZERROR_CHECK(nrfx_saadc_channel_init(BATTERY_ADC_CHANNEL, &channel_config)); - } - } - m_now_adc_channel = ch; -} - -int16_t SingleLeadECG_adc_get_val(uint8_t ch) { - SingleLeadECG_adc_set_ch(ch); - return znrf_adc_channel_read_val(ch); -} - -void SingleLeadECG_adc_module_deinit() { - SingleLeadECG_adc_set_ch(0xff); - nrf_drv_saadc_uninit(); -} /******************************************************************************* * SCREEN * @@ -189,59 +137,6 @@ void SingleLeadECG_led_blue_set_state(bool state) { } /******************************************************************************* - * ecg * - *******************************************************************************/ - -/** - * @brief - * PLOD: 表明当前点击连接状态,如果为0,表示连接,如果为1,表示断开 - */ - -void SingleLeadECG_ecg_init() {} - -void SingleLeadECG_ecg_deinit() {} - -void SingleLeadECG_ecg_io_init() { - nrf_gpio_cfg_sense_input(ECG_NLOD_PIN, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_NOSENSE); - nrf_gpio_cfg_sense_input(ECG_PLOD_PIN, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_NOSENSE); -} - -uint32_t SingleLeadECG_ecg_nlod_get_connected_state() { return nrf_gpio_pin_read(ECG_NLOD_PIN); } -uint32_t SingleLeadECG_ecg_plod_get_connected_state() { return !nrf_gpio_pin_read(ECG_PLOD_PIN); } -int16_t SingleLeadECG_ecg_plod_get_ecg_val() { return SingleLeadECG_adc_get_val(ECG_ADC_CHANNEL); } - -/******************************************************************************* - * BATTERY * - *******************************************************************************/ -void SingleLeadECG_battery_init() {} -void SingleLeadECG_battery_deinit() {} - -int16_t SingleLeadECG_battery_get_adc_val() { return SingleLeadECG_adc_get_val(BATTERY_ADC_CHANNEL); } -int16_t SingleLeadECG_battery_val() { // - - static const float maxv = 4.0; - static const float minv = 3.5; - - float voltage = SingleLeadECG_battery_get_adc_val() / 4096.0 * 3.3 / 2.0 * 3; - if (voltage > maxv) voltage = maxv; - if (voltage < minv) voltage = minv; - - float percent = (voltage - minv) / (maxv - minv) * 100 + (float)0.1 /*加0.1是为了避免999.999时显示电量为90*/; - int16_t percent_int = (int16_t)percent; - if (percent_int < 10 && percent_int != 0) { - percent_int = 3; - } else { - percent_int = percent_int / 10 * 10; - } - // ZLOGI("battery %d ", SingleLeadECG_battery_get_adc_val()); - return percent_int; -} -void SingleLeadECG_battery_charge_detect_io_init() { // - nrf_gpio_cfg_sense_input(BATTERY_CHARGE_DETECT_PIN, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_NOSENSE); -} -bool SingleLeadECG_battery_charge_get_state() { return nrf_gpio_pin_read(BATTERY_CHARGE_DETECT_PIN) == 0; } - -/******************************************************************************* * eeprom * *******************************************************************************/ static const nrf_drv_twi_t m_eeprom_twi_master = NRF_DRV_TWI_INSTANCE(EEPROM_I2C_INSTANCE); diff --git a/app/src/one_conduction_board.h b/app/src/one_conduction_board.h index 87b8401..5f75268 100644 --- a/app/src/one_conduction_board.h +++ b/app/src/one_conduction_board.h @@ -17,7 +17,6 @@ void SingleLeadECG_adc_module_init(); void SingleLeadECG_adc_module_deinit(); - /******************************************************************************* * EEPROM * *******************************************************************************/ @@ -47,25 +46,15 @@ void SingleLeadECG_led_deinit(); void SingleLeadECG_led_green_set_state(bool state); void SingleLeadECG_led_blue_set_state(bool state); +#if 0 /******************************************************************************* * ECG * *******************************************************************************/ -void SingleLeadECG_ecg_init(); -void SingleLeadECG_ecg_deinit(); -void SingleLeadECG_ecg_io_init(); -uint32_t SingleLeadECG_ecg_nlod_get_connected_state(); -uint32_t SingleLeadECG_ecg_plod_get_connected_state(); -int16_t SingleLeadECG_ecg_plod_get_ecg_val(); /******************************************************************************* * BATTERY * *******************************************************************************/ -void SingleLeadECG_battery_init(); -void SingleLeadECG_battery_deinit(); -int16_t SingleLeadECG_battery_get_adc_val(); -int16_t SingleLeadECG_battery_val(); -void SingleLeadECG_battery_charge_detect_io_init(); -bool SingleLeadECG_battery_charge_get_state(); +#endif diff --git a/app/src/one_conduction_main.c b/app/src/one_conduction_main.c index 5d056f5..f5d0ef8 100644 --- a/app/src/one_conduction_main.c +++ b/app/src/one_conduction_main.c @@ -4,7 +4,10 @@ #include "app_event_distribute.h" #include "basic/device_version_info_mgr.h" #include "ble_cmd_process_service.h" +#include "board/board_adc_module_ctrl.h" +#include "board/board_battery_state.h" #include "board/board_beep_ctrl.h" +#include "board/board_ecg_sensor.h" #include "config.h" #include "display_manager.h" #include "heart_wave_sample_service.h" @@ -18,9 +21,9 @@ * GLOBAL * *******************************************************************************/ -APP_TIMER_DEF(m_state_machine_driver_tmr); // 状态机驱动定时器 -APP_TIMER_DEF(m_plod_state_event_detect_tmr); // 导联连接状态检测定时器 -APP_TIMER_DEF(m_charge_event_detect_tmr); // 充电事件检测 +APP_TIMER_DEF(m_state_machine_driver_tmr); // 状态机驱动定时器 +// APP_TIMER_DEF(m_plod_state_event_detect_tmr); // 导联连接状态检测定时器 +// APP_TIMER_DEF(m_charge_event_detect_tmr); // 充电事件检测 static int m_cur_fd; static sample_data_filename_t sampledata_file_name; @@ -36,74 +39,17 @@ static sample_data_filename_t sampledata_file_name; /******************************************************************************* * 事件生成器 * *******************************************************************************/ - -// 导联连接状态事件生成器 -static bool m_plod_state_connected_state = false; -static uint32_t m_connecte_state_last_trigger_timepoint = 0; - -static uint32_t plod_state_has_disconnected_ms() { return znordic_haspassed_ms(m_connecte_state_last_trigger_timepoint); } -static bool plod_state_is_connected() { return m_plod_state_connected_state; } - -static void m_plod_state_event_detect_tmr_cb(void* p_context) { // - static uint32_t connecte_state_trigger_cnt = 0; - static app_event_t appevent; - memset(&appevent, 0, sizeof(appevent)); - - if (SingleLeadECG_ecg_plod_get_connected_state()) { - connecte_state_trigger_cnt++; - m_connecte_state_last_trigger_timepoint = znordic_getpower_on_ms(); - } else { - connecte_state_trigger_cnt = 0; - } - - if (!m_plod_state_connected_state && connecte_state_trigger_cnt >= 10) { - // 连接事件 - appevent.eventType = kplod_connected_event; - m_plod_state_connected_state = true; - AppEvent_pushEvent(&appevent); - } else if (m_plod_state_connected_state && connecte_state_trigger_cnt == 0) { - // 断开事件 - appevent.eventType = kplod_disconnected_event; - m_plod_state_connected_state = false; - AppEvent_pushEvent(&appevent); - } - - // else if (connecte_state_trigger_cnt >= 10 && connecte_state_trigger_cnt % 10 == 0) { - // // 连接持续事件 - // appevent.eventType = kplod_connecting_event; - // appevent.val.plod_connected_accumulation_time = connecte_state_trigger_cnt / 10; - // AppEvent_pushEvent(&appevent); - // } -} - -static void m_charge_event_detect_tmr_cb(void* p_context) { // - static app_event_t appevent; - memset(&appevent, 0, sizeof(appevent)); - static bool ischarging = false; - // if (!SingleLeadECG_battery_charge_get_state()) { - // if (!ischarging) { - // appevent.eventType = kbattery_start_charge_event; - // ischarging = true; - // AppEvent_pushEvent(&appevent); - // } else { - // appevent.eventType = kbattery_charging_event; - // AppEvent_pushEvent(&appevent); - // } - // } else { - // if (ischarging) { - // ischarging = false; - // appevent.eventType = kbattery_end_charge_event; - // AppEvent_pushEvent(&appevent); - // } - // } -} +// static void m_charge_event_detect_tmr_cb(void* p_context) { // +// static app_event_t appevent; +// memset(&appevent, 0, sizeof(appevent)); +// static bool ischarging = false; +// } static void state_machine_driver_tmr_cb(void* p_context) { // static app_event_t appevent; appevent.eventType = kevent_tmr_scheduler_event; AppEvent_pushEvent(&appevent); } - /******************************************************************************* * 事件处理 * *******************************************************************************/ @@ -112,35 +58,31 @@ static void power_on() { if (m_poweronflag) { return; } - SingleLeadECG_adc_module_init(); + + hwss_init(); BoardBeepCtrl_load(); + BoardEcgSensor_load(); + BoardBattery_load(); SingleLeadECG_led_init(); - SingleLeadECG_ecg_init(); - SingleLeadECG_battery_init(); - hwss_init(); + sample_data_mgr_init(); dsp_mgr_init(); ble_cmder_init(); ble_cmder_start_adv(); - - // SingleLeadECG_beep_trigger_once(); - m_poweronflag = true; - ZERROR_CHECK(app_timer_start(m_state_machine_driver_tmr, APP_TIMER_TICKS(100), NULL)); // 200HZ采样 } static void power_off() { dsp_mgr_uninit(); sample_data_mgr_uninit(); hwss_uninit(); - SingleLeadECG_battery_deinit(); - SingleLeadECG_ecg_deinit(); + BoardEcgSensor_unload(); + BoardBattery_unload(); + SingleLeadECG_led_deinit(); BoardBeepCtrl_unload(); - SingleLeadECG_adc_module_deinit(); ble_cmder_stop_adv(); ble_cmder_uninit(); - ZERROR_CHECK(app_timer_stop(m_state_machine_driver_tmr)); m_poweronflag = false; } /******************************************************************************* @@ -188,42 +130,56 @@ static void state_machine__change_to_home_state() { ds_change_to_state(kdevice_state_home); dsp_mgr_change_to_main(); } + +void ENTER_DEEP_SLEEP() { + // 进入深度睡眠前,使能唤醒引脚 + BoardEcgSensor_set_sence_state(); + BoardBattery_sence_gpio_init_before_sleep(); + // nrf_sdh_disable_request(); + app_timer_stop_all(); + nrf_sdh_disable_request(); + sd_power_system_off(); + NVIC_SystemReset(); +} + static void app_event_listener(void* p_event_data, uint16_t event_size) { if (!p_event_data) return; app_event_t* p_event = (app_event_t*)p_event_data; - if (zdatachannel_is_connected()) { - SingleLeadECG_led_blue_set_state(true); - } else { - SingleLeadECG_led_blue_set_state(false); - } + // if (zdatachannel_is_connected()) { + // SingleLeadECG_led_blue_set_state(true); + // } else { + // SingleLeadECG_led_blue_set_state(false); + // } /******************************************************************************* * 待机状态 * *******************************************************************************/ if (ds_now_state() == kdevice_state_standby) { - /** - * @brief - * 充电事件触发 --> 切换到充电页面 - * 导联连接事件触发 --> 切换到开机中页面 - * - */ - - // if (!SingleLeadECG_battery_charge_get_state()) { - // state_machine__change_to_charging_state(); - // } - - if (p_event->eventType == kplod_connected_event) { + ZLOGI("standby......"); + // 充电事件触发 --> 切换到充电页面 + if (BoardBattery_get_charging_state()) { + state_machine__change_to_charging_state(); + } + + // 导联连接事件触发 --> 切换到开机中页面 + else if (BoardEcgSensor_plod_get_connected_state()) { state_machine__change_to_poweroning_state(); } + // 10秒后,如果RTC未被设置过,则进入超低功耗 + else if (ds_cur_state_haspassed_ms() >= 10000) { + if (!znordic_rtc_has_setted()) { + // ENTER_DEEP_SLEEP(); + } + } } /******************************************************************************* * 充电状态 * *******************************************************************************/ else if (ds_now_state() == kdevice_state_charging) { - if (!SingleLeadECG_battery_charge_get_state()) { + if (!BoardBattery_get_charging_state()) { state_machine__change_to_standby_state(); } } @@ -244,26 +200,26 @@ static void app_event_listener(void* p_event_data, uint16_t event_size) { else if (ds_now_state() == kdevice_state_home) { // 如果用户长时间不操作,自动切换到待机状态 - if (!zdatachannel_is_connected() && // - !plod_state_is_connected() && // - plod_state_has_disconnected_ms() >= 3000 && // + if (!zdatachannel_is_connected() && // + !BoardEcgSensor_plod_get_connected_state_after_filter() && // + BoardEcgSensor_plod_state_has_disconnected_ms() >= 3000 && // ds_cur_state_haspassed_ms() >= AUTOMATIC_SLEEP_TIME) { state_machine__change_to_standby_state(); } // 如果用户继续保持静止,切换到采集页面 - else if (plod_state_is_connected() && ds_cur_state_haspassed_ms() > 1500) { + else if (BoardEcgSensor_plod_get_connected_state_after_filter() && ds_cur_state_haspassed_ms() > 1500) { // dsp_mgr_change_to_preparePage ds_change_to_state(kdevice_state_keep_still); dsp_mgr_change_to_preparePage(); } - // ZLOGI("bt:%d plod:%d has_disc:%d state:%d", zdatachannel_is_connected(), plod_state_is_connected(), plod_state_has_disconnected_ms(), ds_cur_state_haspassed_ms()); + // ZLOGI("bt:%d plod:%d has_disc:%d state:%d", zdatachannel_is_connected(), BoardEcgSensor_plod_get_connected_state_after_filter(), plod_state_has_disconnected_ms(), ds_cur_state_haspassed_ms()); } /******************************************************************************* * 保持静止页面 * *******************************************************************************/ else if (ds_now_state() == kdevice_state_keep_still) { - if (!plod_state_is_connected()) { + if (!BoardEcgSensor_plod_get_connected_state_after_filter()) { // 如果用户未保持静止,切换到首页 state_machine__change_to_home_state(); } else { @@ -403,7 +359,8 @@ static void app_event_listener(void* p_event_data, uint16_t event_size) { * 采样错误页面 * *******************************************************************************/ else if (ds_now_state() == kdevice_state_sampling_error) { - if ((ds_cur_state_haspassed_ms() >= 3000) || (ds_cur_state_haspassed_ms() >= 1000 && m_plod_state_connected_state)) { + if ((ds_cur_state_haspassed_ms() >= 3000) || // + (ds_cur_state_haspassed_ms() >= 1000 && BoardEcgSensor_plod_get_connected_state_after_filter())) { ZLOGI("ds_cur_state_haspassed_ms() %d> 3000", ds_cur_state_haspassed_ms()); state_machine__change_to_home_state(); ble_cmder_report_sample_finish_event(); @@ -416,17 +373,14 @@ static void app_event_listener(void* p_event_data, uint16_t event_size) { void one_conduction_main() { BoardBeepCtrl_init(); - SingleLeadECG_ecg_io_init(); - SingleLeadECG_battery_charge_detect_io_init(); AppEvent_regListener(app_event_listener); - ZERROR_CHECK(app_timer_create(&m_state_machine_driver_tmr, APP_TIMER_MODE_REPEATED, state_machine_driver_tmr_cb)); - ZERROR_CHECK(app_timer_create(&m_plod_state_event_detect_tmr, APP_TIMER_MODE_REPEATED, m_plod_state_event_detect_tmr_cb)); - ZERROR_CHECK(app_timer_create(&m_charge_event_detect_tmr, APP_TIMER_MODE_REPEATED, m_charge_event_detect_tmr_cb)); + BoardBattery_init(); + BoardEcgSensor_init(); - ZERROR_CHECK(app_timer_start(m_plod_state_event_detect_tmr, APP_TIMER_TICKS(30), NULL)); - ZERROR_CHECK(app_timer_start(m_charge_event_detect_tmr, APP_TIMER_TICKS(100), NULL)); + ZERROR_CHECK(app_timer_create(&m_state_machine_driver_tmr, APP_TIMER_MODE_REPEATED, state_machine_driver_tmr_cb)); + ZERROR_CHECK(app_timer_start(m_state_machine_driver_tmr, APP_TIMER_TICKS(300), NULL)); - state_machine__change_to_poweroning_state(); + state_machine__change_to_standby_state(); znordic_loop(); } diff --git a/libznordic b/libznordic index 2e93f83..52d1811 160000 --- a/libznordic +++ b/libznordic @@ -1 +1 @@ -Subproject commit 2e93f83bd44b54f4b6bec2d338d674e7a884bd42 +Subproject commit 52d18110e10b67278d7eafad5a88cb01a7395137