|
|
@ -25,87 +25,16 @@ |
|
|
|
* 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); // 充电事件检测 |
|
|
|
|
|
|
|
static int m_cur_fd; |
|
|
|
static sample_data_filename_t sampledata_file_name; |
|
|
|
|
|
|
|
#define SCHED_MAX_EVENT_DATA_SIZE MAX(sizeof(app_event_t), APP_TIMER_SCHED_EVENT_DATA_SIZE) |
|
|
|
|
|
|
|
/******************************************************************************* |
|
|
|
* 函数声明 * |
|
|
|
*******************************************************************************/ |
|
|
|
|
|
|
|
// 业务事件处理函数 |
|
|
|
|
|
|
|
/******************************************************************************* |
|
|
|
* 事件生成器 * |
|
|
|
*******************************************************************************/ |
|
|
|
// 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; |
|
|
|
wd_feed(); |
|
|
|
AppEvent_pushEvent(&appevent); |
|
|
|
} |
|
|
|
/******************************************************************************* |
|
|
|
* 事件处理 * |
|
|
|
*******************************************************************************/ |
|
|
|
static bool m_poweronflag; |
|
|
|
static void power_on() { |
|
|
|
if (m_poweronflag) { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
BoardBeepCtrl_load(); |
|
|
|
BoardEcgSensor_load(); |
|
|
|
BoardBattery_load(); |
|
|
|
BoardLight_load(); |
|
|
|
hwss_init(); |
|
|
|
|
|
|
|
sample_data_mgr_init(); |
|
|
|
dsp_mgr_init(); |
|
|
|
ble_cmder_init(); |
|
|
|
ble_cmder_start_adv(); |
|
|
|
m_poweronflag = true; |
|
|
|
} |
|
|
|
static void power_off() { |
|
|
|
if (!m_poweronflag) return; |
|
|
|
dsp_mgr_uninit(); |
|
|
|
sample_data_mgr_uninit(); |
|
|
|
hwss_uninit(); |
|
|
|
|
|
|
|
BoardEcgSensor_unload(); |
|
|
|
BoardBattery_unload(); |
|
|
|
BoardLight_unload(); |
|
|
|
|
|
|
|
BoardBeepCtrl_unload(); |
|
|
|
ble_stop_upload_record(); |
|
|
|
zble_module_disconnect(); |
|
|
|
|
|
|
|
for (size_t i = 0; i < 100; i++) { |
|
|
|
nrf_delay_ms(10); |
|
|
|
if (!zble_module_is_connected()) { |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
ble_cmder_stop_adv(); |
|
|
|
ble_cmder_uninit(); |
|
|
|
|
|
|
|
board_power_mgr_main_power_supply_set(false); |
|
|
|
m_poweronflag = false; |
|
|
|
} |
|
|
|
void ENTER_DEEP_SLEEP() { |
|
|
|
// 进入深度睡眠前,使能唤醒引脚 |
|
|
|
BoardEcgSensor_set_sence_state(); |
|
|
|
BoardBattery_sence_gpio_init_before_sleep(); |
|
|
|
// nrf_sdh_disable_request(); |
|
|
|
app_timer_pause(); |
|
|
@ -117,52 +46,66 @@ void ENTER_DEEP_SLEEP() { |
|
|
|
/******************************************************************************* |
|
|
|
* 状态切换方法 * |
|
|
|
*******************************************************************************/ |
|
|
|
/** |
|
|
|
* @brief 切换到充电中状态 |
|
|
|
*/ |
|
|
|
static void state_machine__change_to_charging_state() { // |
|
|
|
power_on(); |
|
|
|
// 切换到充电显示页面 |
|
|
|
dsp_mgr_change_to_chargingPage(); |
|
|
|
ds_change_to_state(kdevice_state_charging); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @brief 切换到待机状态 |
|
|
|
*/ |
|
|
|
static void on_state_change(device_state_t from, device_state_t to) { |
|
|
|
/** |
|
|
|
* @brief 开关机控制 |
|
|
|
*/ |
|
|
|
if (from == kdevice_state_standby && to != kdevice_state_standby) { |
|
|
|
/** |
|
|
|
* @brief 切换到工作状态 |
|
|
|
*/ |
|
|
|
BoardBeepCtrl_load(); |
|
|
|
BoardLight_load(); |
|
|
|
|
|
|
|
static void state_machine__change_to_standby_state() { |
|
|
|
ZLOGI_BLOCK("change to standby"); |
|
|
|
power_off(); |
|
|
|
ds_change_to_state(kdevice_state_standby); |
|
|
|
} |
|
|
|
sample_data_mgr_init(); |
|
|
|
dsp_mgr_init(); |
|
|
|
ble_cmder_init(); |
|
|
|
ble_cmder_start_adv(); |
|
|
|
|
|
|
|
/** |
|
|
|
* @brief 切换到开机中画面 |
|
|
|
*/ |
|
|
|
static void state_machine__change_to_poweroning_state() { |
|
|
|
power_on(); |
|
|
|
dsp_mgr_change_to_welcome(); |
|
|
|
ds_change_to_state(kdevice_state_poweron); |
|
|
|
} |
|
|
|
app_board_change_state(kapp_power_state_working); |
|
|
|
|
|
|
|
} else if (to == kdevice_state_standby) { |
|
|
|
/** |
|
|
|
* @brief 切换到待机模式 |
|
|
|
* |
|
|
|
*/ |
|
|
|
dsp_mgr_uninit(); |
|
|
|
sample_data_mgr_uninit(); |
|
|
|
|
|
|
|
BoardLight_unload(); |
|
|
|
|
|
|
|
/** |
|
|
|
* @brief 切换到首页 |
|
|
|
*/ |
|
|
|
static void state_machine__change_to_home_state() { |
|
|
|
ds_change_to_state(kdevice_state_home); |
|
|
|
dsp_mgr_change_to_main(); |
|
|
|
BoardBeepCtrl_unload(); |
|
|
|
ble_stop_upload_record(); |
|
|
|
zble_module_disconnect(); |
|
|
|
for (size_t i = 0; i < 100; i++) { |
|
|
|
nrf_delay_ms(10); |
|
|
|
if (!zble_module_is_connected()) { |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
ble_cmder_stop_adv(); |
|
|
|
ble_cmder_uninit(); |
|
|
|
board_power_mgr_main_power_supply_set(false); |
|
|
|
app_board_change_state(kapp_power_state_standby); |
|
|
|
} |
|
|
|
|
|
|
|
/*********************************************************************************************************************** |
|
|
|
* 切换状态 附带操作 * |
|
|
|
***********************************************************************************************************************/ |
|
|
|
if (to == kdevice_state_home) { |
|
|
|
dsp_mgr_change_to_main(); |
|
|
|
} else if (to == kdevice_state_poweron) { |
|
|
|
dsp_mgr_change_to_welcome(); |
|
|
|
} else if (to == kdevice_state_charging) { |
|
|
|
dsp_mgr_change_to_chargingPage(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
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; |
|
|
|
|
|
|
|
// BoardLight_blockFlash(3,100); |
|
|
|
// ZLOGI("......"); |
|
|
|
|
|
|
|
// return; |
|
|
|
|
|
|
|
if (ds_now_state() != kdevice_state_standby && // |
|
|
|
ds_now_state() != kdevice_state_charging && // |
|
|
|
ds_now_state() != kdevice_state_poweron // |
|
|
@ -182,16 +125,17 @@ static void app_event_listener(void* p_event_data, uint16_t event_size) { |
|
|
|
if (ds_now_state() == kdevice_state_standby) { |
|
|
|
// 充电事件触发 --> 切换到充电页面 |
|
|
|
if (BoardBattery_get_charging_state()) { |
|
|
|
state_machine__change_to_charging_state(); |
|
|
|
ds_change_to_state(kdevice_state_charging); |
|
|
|
} |
|
|
|
|
|
|
|
// 导联连接事件触发 --> 切换到开机中页面 |
|
|
|
|
|
|
|
else if (BoardEcgSensor_plod_get_connected_state_after_filter() && znordic_haspassed_ms(lasttrypoweron_time) >= LOW_BATTERY_REMINDER_DELAY_MS) { |
|
|
|
else if (hwss_lead_get_state_connected_state() && znordic_haspassed_ms(lasttrypoweron_time) >= LOW_BATTERY_REMINDER_DELAY_MS) { |
|
|
|
lasttrypoweron_time = znordic_getpower_on_ms(); |
|
|
|
// if (BoardBattery_get_battery_level() > APP_WORK_BATTERY_LEVEL) { |
|
|
|
// TODO: 检查电池地量 |
|
|
|
if (true) { |
|
|
|
state_machine__change_to_poweroning_state(); |
|
|
|
ds_change_to_state(kdevice_state_poweron); |
|
|
|
} else { |
|
|
|
BoardLight_load(); |
|
|
|
BoardLight_blockFlash(3, 100); |
|
|
@ -199,14 +143,16 @@ static void app_event_listener(void* p_event_data, uint16_t event_size) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 10秒后,如果RTC未被设置过,则进入超低功耗 |
|
|
|
/** |
|
|
|
* @brief |
|
|
|
* TODO: |
|
|
|
* 1. 永远不进入超低功耗状态,直到电池电量低于一定程度 |
|
|
|
* 2. 如果RTC未被设置,则重置设备状态 |
|
|
|
* 3. 在每次从待机状态切换到开机状态,如果RTC未被设置,则清空已经存储的波形数据 |
|
|
|
*/ |
|
|
|
else if (ds_cur_state_haspassed_ms() >= 10000) { |
|
|
|
if (!znordic_rtc_has_setted()) { |
|
|
|
ENTER_DEEP_SLEEP(); |
|
|
|
} |
|
|
|
// if (APP_BATTERY_PROTECT_LEVEL > BoardBattery_get_battery_level()) { |
|
|
|
// ENTER_DEEP_SLEEP(); |
|
|
|
// } |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -215,7 +161,7 @@ static void app_event_listener(void* p_event_data, uint16_t event_size) { |
|
|
|
*******************************************************************************/ |
|
|
|
else if (ds_now_state() == kdevice_state_charging) { |
|
|
|
if (!BoardBattery_get_charging_state()) { |
|
|
|
state_machine__change_to_standby_state(); |
|
|
|
ds_change_to_state(kdevice_state_standby); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -225,7 +171,7 @@ static void app_event_listener(void* p_event_data, uint16_t event_size) { |
|
|
|
|
|
|
|
else if (ds_now_state() == kdevice_state_poweron) { |
|
|
|
if (ds_cur_state_haspassed_ms() >= 1500) { |
|
|
|
state_machine__change_to_home_state(); |
|
|
|
ds_change_to_state(kdevice_state_home); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -235,14 +181,13 @@ 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() && // |
|
|
|
!BoardEcgSensor_plod_get_connected_state_after_filter() && // |
|
|
|
BoardEcgSensor_plod_state_has_disconnected_ms() >= 3000 && // |
|
|
|
if (!zdatachannel_is_connected() && // |
|
|
|
!hwss_lead_get_state_connected_state() && // |
|
|
|
ds_cur_state_haspassed_ms() >= AUTOMATIC_SLEEP_TIME) { |
|
|
|
state_machine__change_to_standby_state(); |
|
|
|
ds_change_to_state(kdevice_state_standby); |
|
|
|
} |
|
|
|
// 如果用户继续保持静止,切换到采集页面 |
|
|
|
else if (BoardEcgSensor_plod_get_connected_state_after_filter() && ds_cur_state_haspassed_ms() > 1500) { |
|
|
|
else if (hwss_lead_get_state_connected_state() && ds_cur_state_haspassed_ms() > 1500) { |
|
|
|
// dsp_mgr_change_to_preparePage |
|
|
|
if (!BoardBattery_get_charging_state()) { |
|
|
|
ds_change_to_state(kdevice_state_keep_still); |
|
|
@ -254,32 +199,31 @@ static void app_event_listener(void* p_event_data, uint16_t event_size) { |
|
|
|
} |
|
|
|
} |
|
|
|
#if 0 |
|
|
|
//TODO:使能 |
|
|
|
// 低电量,设备进入待机模式 |
|
|
|
else if (BoardBattery_get_battery_level() < APP_AUTO_STANDY_BATTERY_LEVEL && ds_cur_state_haspassed_ms() > 3000) { |
|
|
|
state_machine__change_to_standby_state(); |
|
|
|
ds_change_to_state(kdevice_state_standby); |
|
|
|
} // |
|
|
|
#endif |
|
|
|
|
|
|
|
else if (!ble_is_upload_record() && zble_module_is_connected() && zdatachannel_last_rx_data_haspassed_s() >= BLE_UNCONNECTED_OVERTIME_S) { |
|
|
|
ZLOGI("auto sleep because ble unconnected"); |
|
|
|
state_machine__change_to_standby_state(); |
|
|
|
ds_change_to_state(kdevice_state_standby); |
|
|
|
} |
|
|
|
|
|
|
|
else if (BoardBattery_get_charging_state()) { |
|
|
|
// BoardLight_setGreenLightEffect(kLightEffect_close); |
|
|
|
state_machine__change_to_standby_state(); |
|
|
|
ds_change_to_state(kdevice_state_standby); |
|
|
|
} |
|
|
|
// 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 (!BoardEcgSensor_plod_get_connected_state_after_filter()) { |
|
|
|
if (!hwss_lead_get_state_connected_state()) { |
|
|
|
// 如果用户未保持静止,切换到首页 |
|
|
|
state_machine__change_to_home_state(); |
|
|
|
ds_change_to_state(kdevice_state_home); |
|
|
|
hwss_stop_capture(); |
|
|
|
} else { |
|
|
|
/******************************************************************************* |
|
|
@ -410,7 +354,7 @@ static void app_event_listener(void* p_event_data, uint16_t event_size) { |
|
|
|
else if (ds_now_state() == kdevice_state_sampling_complete) { |
|
|
|
if (ds_cur_state_haspassed_ms() >= 3000) { |
|
|
|
ZLOGI("ds_cur_state_haspassed_ms() %d> 3000", ds_cur_state_haspassed_ms()); |
|
|
|
state_machine__change_to_home_state(); |
|
|
|
ds_change_to_state(kdevice_state_home); |
|
|
|
ble_cmder_report_sample_finish_event(); |
|
|
|
} |
|
|
|
} |
|
|
@ -419,9 +363,9 @@ 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 && BoardEcgSensor_plod_get_connected_state_after_filter())) { |
|
|
|
(ds_cur_state_haspassed_ms() >= 1000 && hwss_lead_get_state_connected_state())) { |
|
|
|
ZLOGI("ds_cur_state_haspassed_ms() %d> 3000", ds_cur_state_haspassed_ms()); |
|
|
|
state_machine__change_to_home_state(); |
|
|
|
ds_change_to_state(kdevice_state_home); |
|
|
|
ble_cmder_report_sample_finish_event(); |
|
|
|
} |
|
|
|
} |
|
|
@ -432,45 +376,17 @@ static void app_event_listener(void* p_event_data, uint16_t event_size) { |
|
|
|
|
|
|
|
void one_conduction_main() { |
|
|
|
ZLOGI("one_conduction_main"); |
|
|
|
AppEvent_regListener(app_event_listener); |
|
|
|
|
|
|
|
app_board_init(); |
|
|
|
ads129x_init(); |
|
|
|
|
|
|
|
BoardBeepCtrl_init(); |
|
|
|
AppEvent_regListener(app_event_listener); |
|
|
|
|
|
|
|
board_power_mgr_init(); |
|
|
|
board_power_mgr_main_power_supply_set(true); |
|
|
|
|
|
|
|
BoardEcgSensor_init(); |
|
|
|
|
|
|
|
// BoardBattery_selfTest(); |
|
|
|
// BoardLight_selftest(); |
|
|
|
|
|
|
|
BoardBattery_init(); |
|
|
|
BoardEcgSensor_init(); |
|
|
|
BoardLight_Init(); |
|
|
|
BoardBattery_load(); |
|
|
|
|
|
|
|
// power_on(); |
|
|
|
#if 0 |
|
|
|
if (BoardBattery_get_battery_level() < APP_WORK_BATTERY_LEVEL) { |
|
|
|
BoardLight_load(); |
|
|
|
BoardLight_blockFlash(3, 100); |
|
|
|
BoardLight_unload(); |
|
|
|
// 系统进入深度睡眠,进入深度睡眠前,使能唤醒引脚 |
|
|
|
app_timer_pause(); |
|
|
|
nrf_delay_ms(LOW_BATTERY_REMINDER_DELAY_MS); |
|
|
|
ENTER_DEEP_SLEEP(); |
|
|
|
} |
|
|
|
#endif |
|
|
|
|
|
|
|
// wd_init(); |
|
|
|
|
|
|
|
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)); |
|
|
|
/** |
|
|
|
* @brief 服务初始化 |
|
|
|
*/ |
|
|
|
hwss_init(); |
|
|
|
ds_init(on_state_change); |
|
|
|
|
|
|
|
state_machine__change_to_standby_state(); |
|
|
|
// state_machine__change_to_poweroning_state(); |
|
|
|
ds_change_to_state(kdevice_state_poweron); |
|
|
|
znordic_loop(); |
|
|
|
} |