From 2f050b7057006d71cc7972250097e16514693f71 Mon Sep 17 00:00:00 2001 From: zhaohe Date: Fri, 9 Feb 2024 20:27:41 +0800 Subject: [PATCH] v1 --- .vscode/settings.json | 3 +- README.md | 8 +- app/app.uvoptx | 174 +++++++++++++++++++++------------------ app/app.uvprojx | 10 +++ app/config/sdk_config.h | 2 +- app/src/board/board.h | 16 ++-- app/src/board/board_light_ctrl.c | 148 +++++++++++++++++++++++++++++++++ app/src/board/board_light_ctrl.h | 24 ++++++ app/src/one_conduction_board.c | 30 ++----- app/src/one_conduction_board.h | 7 +- app/src/one_conduction_main.c | 18 ++-- 11 files changed, 311 insertions(+), 129 deletions(-) create mode 100644 app/src/board/board_light_ctrl.c create mode 100644 app/src/board/board_light_ctrl.h diff --git a/.vscode/settings.json b/.vscode/settings.json index 2da517e..cad4efe 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -117,7 +117,8 @@ "nrfx_rtc.h": "c", "device_state.h": "c", "board_adc_module_ctrl.h": "c", - "board_ecg_sensor.h": "c" + "board_ecg_sensor.h": "c", + "board_light_ctrl.h": "c" }, "files.encoding": "gbk" } \ No newline at end of file diff --git a/README.md b/README.md index db34dd2..733ca02 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,11 @@ TODO 0. 支持无线升级,修改SN码 OK 6. 开始采集蜂鸣器响响一声,结束采集蜂鸣器响一声 OK 2. 不足三十30,响三声 OK -3. 添加是否充电检测 -1. 添加逻辑,如果未配置时间,设备进入深度休眠。 -2. 修改电池电量相关逻辑,校准电池电量。 +3. 添加是否充电检测 OK +1. 添加逻辑,如果未配置时间,设备进入深度休眠。 OK +2. 修改电池电量相关逻辑,校准电池电量。OK +4. 添加连接状态指示灯 + 3. 修改心率采样ADC参考电压 4. 修改屏幕显示位置 5. 充电中,限制用户操作 diff --git a/app/app.uvoptx b/app/app.uvoptx index 6789123..1dc2f2d 100644 --- a/app/app.uvoptx +++ b/app/app.uvoptx @@ -643,6 +643,18 @@ 0 0 + + 1 + 24 + 1 + 0 + 0 + 0 + .\src\board\board_light_ctrl.c + board_light_ctrl.c + 0 + 0 + @@ -653,7 +665,7 @@ 0 2 - 24 + 25 1 0 0 @@ -673,7 +685,7 @@ 0 3 - 25 + 26 1 0 0 @@ -685,7 +697,7 @@ 3 - 26 + 27 1 0 0 @@ -705,7 +717,7 @@ 0 4 - 27 + 28 1 0 0 @@ -725,7 +737,7 @@ 0 5 - 28 + 29 1 0 0 @@ -737,7 +749,7 @@ 5 - 29 + 30 1 0 0 @@ -749,7 +761,7 @@ 5 - 30 + 31 1 0 0 @@ -761,7 +773,7 @@ 5 - 31 + 32 1 0 0 @@ -773,7 +785,7 @@ 5 - 32 + 33 1 0 0 @@ -785,7 +797,7 @@ 5 - 33 + 34 1 0 0 @@ -797,7 +809,7 @@ 5 - 34 + 35 1 0 0 @@ -809,7 +821,7 @@ 5 - 35 + 36 1 0 0 @@ -829,7 +841,7 @@ 0 6 - 36 + 37 1 0 0 @@ -849,7 +861,7 @@ 0 7 - 37 + 38 1 0 0 @@ -861,7 +873,7 @@ 7 - 38 + 39 1 0 0 @@ -873,7 +885,7 @@ 7 - 39 + 40 1 0 0 @@ -885,7 +897,7 @@ 7 - 40 + 41 1 0 0 @@ -897,7 +909,7 @@ 7 - 41 + 42 1 0 0 @@ -909,7 +921,7 @@ 7 - 42 + 43 1 0 0 @@ -921,7 +933,7 @@ 7 - 43 + 44 1 0 0 @@ -933,7 +945,7 @@ 7 - 44 + 45 1 0 0 @@ -945,7 +957,7 @@ 7 - 45 + 46 1 0 0 @@ -957,7 +969,7 @@ 7 - 46 + 47 1 0 0 @@ -969,7 +981,7 @@ 7 - 47 + 48 1 0 0 @@ -981,7 +993,7 @@ 7 - 48 + 49 1 0 0 @@ -993,7 +1005,7 @@ 7 - 49 + 50 1 0 0 @@ -1005,7 +1017,7 @@ 7 - 50 + 51 1 0 0 @@ -1017,7 +1029,7 @@ 7 - 51 + 52 1 0 0 @@ -1029,7 +1041,7 @@ 7 - 52 + 53 1 0 0 @@ -1041,7 +1053,7 @@ 7 - 53 + 54 1 0 0 @@ -1053,7 +1065,7 @@ 7 - 54 + 55 1 0 0 @@ -1065,7 +1077,7 @@ 7 - 55 + 56 1 0 0 @@ -1077,7 +1089,7 @@ 7 - 56 + 57 1 0 0 @@ -1097,7 +1109,7 @@ 0 8 - 57 + 58 1 0 0 @@ -1109,7 +1121,7 @@ 8 - 58 + 59 1 0 0 @@ -1121,7 +1133,7 @@ 8 - 59 + 60 1 0 0 @@ -1133,7 +1145,7 @@ 8 - 60 + 61 1 0 0 @@ -1145,7 +1157,7 @@ 8 - 61 + 62 1 0 0 @@ -1157,7 +1169,7 @@ 8 - 62 + 63 1 0 0 @@ -1169,7 +1181,7 @@ 8 - 63 + 64 1 0 0 @@ -1181,7 +1193,7 @@ 8 - 64 + 65 1 0 0 @@ -1193,7 +1205,7 @@ 8 - 65 + 66 1 0 0 @@ -1205,7 +1217,7 @@ 8 - 66 + 67 1 0 0 @@ -1217,7 +1229,7 @@ 8 - 67 + 68 1 0 0 @@ -1229,7 +1241,7 @@ 8 - 68 + 69 1 0 0 @@ -1241,7 +1253,7 @@ 8 - 69 + 70 1 0 0 @@ -1253,7 +1265,7 @@ 8 - 70 + 71 1 0 0 @@ -1265,7 +1277,7 @@ 8 - 71 + 72 1 0 0 @@ -1277,7 +1289,7 @@ 8 - 72 + 73 1 0 0 @@ -1289,7 +1301,7 @@ 8 - 73 + 74 1 0 0 @@ -1301,7 +1313,7 @@ 8 - 74 + 75 1 0 0 @@ -1313,7 +1325,7 @@ 8 - 75 + 76 1 0 0 @@ -1325,7 +1337,7 @@ 8 - 76 + 77 1 0 0 @@ -1337,7 +1349,7 @@ 8 - 77 + 78 1 0 0 @@ -1349,7 +1361,7 @@ 8 - 78 + 79 1 0 0 @@ -1361,7 +1373,7 @@ 8 - 79 + 80 1 0 0 @@ -1373,7 +1385,7 @@ 8 - 80 + 81 1 0 0 @@ -1385,7 +1397,7 @@ 8 - 81 + 82 1 0 0 @@ -1397,7 +1409,7 @@ 8 - 82 + 83 1 0 0 @@ -1409,7 +1421,7 @@ 8 - 83 + 84 1 0 0 @@ -1429,7 +1441,7 @@ 0 9 - 84 + 85 1 0 0 @@ -1441,7 +1453,7 @@ 9 - 85 + 86 1 0 0 @@ -1453,7 +1465,7 @@ 9 - 86 + 87 1 0 0 @@ -1465,7 +1477,7 @@ 9 - 87 + 88 1 0 0 @@ -1477,7 +1489,7 @@ 9 - 88 + 89 1 0 0 @@ -1489,7 +1501,7 @@ 9 - 89 + 90 1 0 0 @@ -1509,7 +1521,7 @@ 0 10 - 90 + 91 1 0 0 @@ -1521,7 +1533,7 @@ 10 - 91 + 92 1 0 0 @@ -1533,7 +1545,7 @@ 10 - 92 + 93 1 0 0 @@ -1553,7 +1565,7 @@ 0 11 - 93 + 94 1 0 0 @@ -1565,7 +1577,7 @@ 11 - 94 + 95 1 0 0 @@ -1577,7 +1589,7 @@ 11 - 95 + 96 1 0 0 @@ -1597,7 +1609,7 @@ 0 12 - 96 + 97 1 0 0 @@ -1609,7 +1621,7 @@ 12 - 97 + 98 1 0 0 @@ -1621,7 +1633,7 @@ 12 - 98 + 99 1 0 0 @@ -1641,7 +1653,7 @@ 0 13 - 99 + 100 1 0 0 @@ -1653,7 +1665,7 @@ 13 - 100 + 101 1 0 0 @@ -1673,7 +1685,7 @@ 0 14 - 101 + 102 1 0 0 @@ -1685,7 +1697,7 @@ 14 - 102 + 103 1 0 0 @@ -1697,7 +1709,7 @@ 14 - 103 + 104 1 0 0 @@ -1709,7 +1721,7 @@ 14 - 104 + 105 1 0 0 diff --git a/app/app.uvprojx b/app/app.uvprojx index bd2f398..fd4babf 100644 --- a/app/app.uvprojx +++ b/app/app.uvprojx @@ -498,6 +498,11 @@ 1 .\src\board\board_ecg_sensor.c + + board_light_ctrl.c + 1 + .\src\board\board_light_ctrl.c + @@ -4377,6 +4382,11 @@ 1 .\src\board\board_ecg_sensor.c + + board_light_ctrl.c + 1 + .\src\board\board_light_ctrl.c + diff --git a/app/config/sdk_config.h b/app/config/sdk_config.h index 54116c9..03e3122 100644 --- a/app/config/sdk_config.h +++ b/app/config/sdk_config.h @@ -7812,7 +7812,7 @@ // NRF_LOG_ENABLED - nrf_log - Logger //========================================================== #ifndef NRF_LOG_ENABLED -#define NRF_LOG_ENABLED 1 +#define NRF_LOG_ENABLED 0 #endif // Log message pool - Configuration of log message pool diff --git a/app/src/board/board.h b/app/src/board/board.h index d066c9c..ed0139d 100644 --- a/app/src/board/board.h +++ b/app/src/board/board.h @@ -10,10 +10,10 @@ * 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 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 // 不重复即可 @@ -28,11 +28,15 @@ #define MAX_FILE_NUM 10 // 最多存储条目数 #define SAMPLE_RATE 500 // 采样率 #define SAMPLE_PRECISION 12 // 采样精度 -#define AUTOMATIC_SLEEP_TIME 5000 // 开机后自动休眠时间 +#define AUTOMATIC_SLEEP_TIME 5000 // 开机后自动休眠时间 #define SAMPLE_MIN_TIME_S (30.0) // 采样最小时间 #define LITTLE_DATA_BLOCK_FRAME_NUM 5 // 每次多少帧上报一次 #define KEEP_STILL_OVERTIME_MS_1P5 2000 // 保持静止超时时间的1/6 +#define LED_GREEN_PIN 9 +#define LED_BLUE_PIN 10 -#define HEART_WAVE_SAMPLE_TMR_INSTANCE 3 \ No newline at end of file +#define HEART_WAVE_SAMPLE_TMR_INSTANCE 3 + +#define LIGHT_PWM_INSTANCE 1 diff --git a/app/src/board/board_light_ctrl.c b/app/src/board/board_light_ctrl.c new file mode 100644 index 0000000..ad9c957 --- /dev/null +++ b/app/src/board/board_light_ctrl.c @@ -0,0 +1,148 @@ +#include "board_light_ctrl.h" + +#include "board.h" +#include "znordic.h" + +#define BLINK_CNT 1 +#define BLINK_PERIOD_MS (100) +#define QUICK_BLINK_PERIOD_MS (100) +#define SLOW_BLINK_PERIOD_MS (2000) +// LED_GREEN_PIN + +typedef struct { + nrf_drv_pwm_t driver; + nrf_pwm_values_individual_t seq_values; + nrf_pwm_sequence_t seq; + nrf_drv_pwm_config_t config; +} pwm_light_ctrl_t; +static pwm_light_ctrl_t light_ctrl = { + .driver = NRF_DRV_PWM_INSTANCE(LIGHT_PWM_INSTANCE), + .seq_values = {0}, + .seq = + { + .values.p_individual = &light_ctrl.seq_values, + .length = NRF_PWM_VALUES_LENGTH(light_ctrl.seq_values), + .repeats = 0, + .end_delay = 0, + }, + .config = + { + + .output_pins = + { + LED_BLUE_PIN, // + NRF_DRV_PWM_PIN_NOT_USED, + NRF_DRV_PWM_PIN_NOT_USED, + NRF_DRV_PWM_PIN_NOT_USED, + }, + .irq_priority = APP_IRQ_PRIORITY_LOWEST, + .base_clock = NRF_PWM_CLK_250kHz, + .count_mode = NRF_PWM_MODE_UP, + .top_value = 100, // 125kHz / 46 = 2.717k + .load_mode = NRF_PWM_LOAD_INDIVIDUAL, + .step_mode = NRF_PWM_STEP_AUTO, + + }, +}; + +static LightEffect_t m_light_effect; +static bool m_led_green_light_state; +static int m_blink_cnt; +APP_TIMER_DEF(m_green_light_effect_tmr); + +static bool m_boardlight_greenlightstate = false; + +void BoardLight_toggleGreenLightState() { BoardLight_setGreenLightState(!m_boardlight_greenlightstate); } +void BoardLight_setGreenLightState(bool state) { + m_boardlight_greenlightstate = state; + if (state) { + light_ctrl.seq_values.channel_0 = 80; // 设置占空比,数值最大不超过 top_value + nrfx_pwm_simple_playback(&light_ctrl.driver, &light_ctrl.seq, 1, NRF_DRV_PWM_FLAG_LOOP); + } else { + nrfx_pwm_stop(&light_ctrl.driver, true); + } +} + +static void BoardLight_effect_tmr_handler(void* p_context) { // + if (m_light_effect == kLightEffect_close) { + if (m_led_green_light_state) { + BoardLight_setGreenLightState(false); + } + } else if (m_light_effect == kLightEffect_open) { + if (!m_led_green_light_state) { + BoardLight_setGreenLightState(true); + } + } else if (m_light_effect == kLightEffect_slowFlash) { + if (m_blink_cnt < 2) { + if (m_blink_cnt % 2 == 0) { + BoardLight_setGreenLightState(true); + } else if (m_blink_cnt % 2 == 1) { + BoardLight_setGreenLightState(false); + } + } else { + if (BLINK_PERIOD_MS * m_blink_cnt >= SLOW_BLINK_PERIOD_MS) { + m_blink_cnt = 0; + return; + } + } + } else if (m_light_effect == kLightEffect_quickFlash) { + if (BLINK_PERIOD_MS * m_blink_cnt >= QUICK_BLINK_PERIOD_MS) { + BoardLight_toggleGreenLightState(); + m_blink_cnt = 0; + return; + } + } + m_blink_cnt++; +} + +void BoardLight_Init() { + ZERROR_CHECK(app_timer_create(&m_green_light_effect_tmr, APP_TIMER_MODE_REPEATED, BoardLight_effect_tmr_handler)); + APP_ERROR_CHECK(nrfx_pwm_init(&light_ctrl.driver, &light_ctrl.config, NULL)); +} +static bool m_boardlight_loaded = false; + +void BoardLight_load() { + m_boardlight_loaded = true; + BoardLight_setGreenLightState(false); +} +void BoardLight_unload() { + m_boardlight_loaded = false; + app_timer_stop(m_green_light_effect_tmr); + BoardLight_setGreenLightEffect(kLightEffect_close); + // nrfx_pwm_uninit(&light_ctrl.driver); +} + +void BoardLight_setGreenLightEffect(LightEffect_t effect) { + if (m_light_effect == effect) return; + m_light_effect = effect; + + + switch (effect) { + case kLightEffect_close: + app_timer_stop(m_green_light_effect_tmr); + BoardLight_setGreenLightState(false); + break; + case kLightEffect_open: + app_timer_stop(m_green_light_effect_tmr); + BoardLight_setGreenLightState(true); + break; + case kLightEffect_slowFlash: + app_timer_start(m_green_light_effect_tmr, APP_TIMER_TICKS(BLINK_PERIOD_MS), NULL); + break; + case kLightEffect_quickFlash: + app_timer_start(m_green_light_effect_tmr, APP_TIMER_TICKS(BLINK_PERIOD_MS), NULL); + break; + default: + break; + } + return; +} + +void BoardLight_blockFlash(int times, int periodms) { + for (int i = 0; i < times; i++) { + BoardLight_setGreenLightState(true); + nrf_delay_ms(periodms); + BoardLight_setGreenLightState(false); + nrf_delay_ms(periodms); + } +} diff --git a/app/src/board/board_light_ctrl.h b/app/src/board/board_light_ctrl.h new file mode 100644 index 0000000..fe7231e --- /dev/null +++ b/app/src/board/board_light_ctrl.h @@ -0,0 +1,24 @@ +#pragma once + +#include +#include + +#include "board/board.h" + +typedef enum { + kLightEffect_unset, + kLightEffect_close, + kLightEffect_open, + kLightEffect_slowFlash, + kLightEffect_quickFlash, +} LightEffect_t; + +void BoardLight_Init(); // main中初始化一遍 +void BoardLight_load(); +void BoardLight_unload(); + +void BoardLight_setGreenLightState(bool state); // 尽量不要直接使用该函数 +void BoardLight_toggleGreenLightState(); // 尽量不要直接使用该函数 + +void BoardLight_setGreenLightEffect(LightEffect_t effect); +void BoardLight_blockFlash(int times, int periodms); diff --git a/app/src/one_conduction_board.c b/app/src/one_conduction_board.c index 7cd9e25..4b3d1a8 100644 --- a/app/src/one_conduction_board.c +++ b/app/src/one_conduction_board.c @@ -24,8 +24,6 @@ #define SCREEN_CLK_PIN 4 #define SCREEN_MOSI_PIN 11 -#define LED_GREEN_PIN 9 -#define LED_BLUE_PIN 10 #define EEPROM_I2C_SCL_M 15 // I2C SCL引脚 #define EEPROM_I2C_SDA_M 17 // I2C SDA引脚 @@ -35,8 +33,6 @@ * TOOLS * *******************************************************************************/ - - /******************************************************************************* * SCREEN * *******************************************************************************/ @@ -96,7 +92,7 @@ uint8_t SingleLeadECG_reset_gpio_write(uint8_t value) { } return 0; } - +#if 0 /******************************************************************************* * LED * *******************************************************************************/ @@ -112,30 +108,14 @@ void SingleLeadECG_led_init() { // nrf_gpio_pin_write(LED_GREEN_PIN, 1); // nrf_gpio_pin_write(LED_GREEN_PIN, 1); - SingleLeadECG_led_green_set_state(false); - SingleLeadECG_led_blue_set_state(false); io_init = true; } -void SingleLeadECG_led_deinit() { - SingleLeadECG_led_green_set_state(false); - SingleLeadECG_led_blue_set_state(false); -} -void SingleLeadECG_led_green_set_state(bool state) { - if (state) { - nrf_gpio_pin_set(LED_GREEN_PIN); - } else { - nrf_gpio_pin_clear(LED_GREEN_PIN); - } -} -void SingleLeadECG_led_blue_set_state(bool state) { - if (state) { - nrf_gpio_pin_set(LED_BLUE_PIN); - } else { - nrf_gpio_pin_clear(LED_BLUE_PIN); - } -} + + + +#endif /******************************************************************************* * eeprom * *******************************************************************************/ diff --git a/app/src/one_conduction_board.h b/app/src/one_conduction_board.h index 5f75268..f7171dd 100644 --- a/app/src/one_conduction_board.h +++ b/app/src/one_conduction_board.h @@ -16,7 +16,6 @@ void SingleLeadECG_adc_module_init(); void SingleLeadECG_adc_module_deinit(); - /******************************************************************************* * EEPROM * *******************************************************************************/ @@ -37,14 +36,12 @@ void SingleLeadECG_eeprom_read(uint32_t add, uint8_t* data, uint16_t len); void SingleLeadECG_screen_init(); void SingleLeadECG_screen_deinit(); - +#if 0 /******************************************************************************* * LED * *******************************************************************************/ void SingleLeadECG_led_init(); -void SingleLeadECG_led_deinit(); -void SingleLeadECG_led_green_set_state(bool state); -void SingleLeadECG_led_blue_set_state(bool state); +#endif #if 0 /******************************************************************************* diff --git a/app/src/one_conduction_main.c b/app/src/one_conduction_main.c index 4baa0da..b618bc9 100644 --- a/app/src/one_conduction_main.c +++ b/app/src/one_conduction_main.c @@ -8,6 +8,7 @@ #include "board/board_battery_state.h" #include "board/board_beep_ctrl.h" #include "board/board_ecg_sensor.h" +#include "board/board_light_ctrl.h" #include "config.h" #include "display_manager.h" #include "heart_wave_sample_service.h" @@ -62,7 +63,6 @@ static void power_on() { BoardBeepCtrl_load(); BoardEcgSensor_load(); BoardBattery_load(); - SingleLeadECG_led_init(); hwss_init(); sample_data_mgr_init(); @@ -79,8 +79,8 @@ static void power_off() { BoardEcgSensor_unload(); BoardBattery_unload(); + BoardLight_unload(); - SingleLeadECG_led_deinit(); BoardBeepCtrl_unload(); ble_cmder_stop_adv(); ble_cmder_uninit(); @@ -147,11 +147,13 @@ 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 (ds_now_state() != kdevice_state_standby) { + if (zdatachannel_is_connected()) { + BoardLight_setGreenLightEffect(kLightEffect_slowFlash); + } else { + BoardLight_setGreenLightEffect(kLightEffect_quickFlash); + } + } /******************************************************************************* * 待机状态 * @@ -378,6 +380,8 @@ void one_conduction_main() { BoardBattery_init(); BoardEcgSensor_init(); + BoardLight_Init(); + BoardLight_load(); 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));