You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

436 lines
17 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include <math.h>
  2. #include "app_event.h"
  3. #include "app_event_distribute.h"
  4. #include "basic/device_version_info_mgr.h"
  5. #include "ble_cmd_process_service.h"
  6. #include "board/board_adc_module_ctrl.h"
  7. #include "board/board_battery_state.h"
  8. #include "board/board_beep_ctrl.h"
  9. #include "board/board_ecg_sensor.h"
  10. #include "board/board_light_ctrl.h"
  11. #include "config.h"
  12. #include "display_manager.h"
  13. #include "heart_wave_sample_service.h"
  14. #include "one_conduction_board.h"
  15. #include "sample_data_manager.h"
  16. #include "zble_module.h"
  17. #include "zdatachannel_service.h"
  18. #include "znordic.h"
  19. /*******************************************************************************
  20. * GLOBAL *
  21. *******************************************************************************/
  22. APP_TIMER_DEF(m_state_machine_driver_tmr); // ״̬��������ʱ��
  23. // APP_TIMER_DEF(m_plod_state_event_detect_tmr); // ��������״̬���ⶨʱ��
  24. // APP_TIMER_DEF(m_charge_event_detect_tmr); // �����¼�����
  25. static int m_cur_fd;
  26. static sample_data_filename_t sampledata_file_name;
  27. #define SCHED_MAX_EVENT_DATA_SIZE MAX(sizeof(app_event_t), APP_TIMER_SCHED_EVENT_DATA_SIZE)
  28. /*******************************************************************************
  29. * *
  30. *******************************************************************************/
  31. // ҵ���¼���������
  32. /*******************************************************************************
  33. * ¼ *
  34. *******************************************************************************/
  35. // static void m_charge_event_detect_tmr_cb(void* p_context) { //
  36. // static app_event_t appevent;
  37. // memset(&appevent, 0, sizeof(appevent));
  38. // static bool ischarging = false;
  39. // }
  40. static void state_machine_driver_tmr_cb(void* p_context) { //
  41. static app_event_t appevent;
  42. appevent.eventType = kevent_tmr_scheduler_event;
  43. wd_feed();
  44. AppEvent_pushEvent(&appevent);
  45. }
  46. /*******************************************************************************
  47. * ¼ *
  48. *******************************************************************************/
  49. static bool m_poweronflag;
  50. static void power_on() {
  51. if (m_poweronflag) {
  52. return;
  53. }
  54. BoardBeepCtrl_load();
  55. BoardEcgSensor_load();
  56. BoardBattery_load();
  57. BoardLight_load();
  58. hwss_init();
  59. sample_data_mgr_init();
  60. dsp_mgr_init();
  61. ble_cmder_init();
  62. ble_cmder_start_adv();
  63. m_poweronflag = true;
  64. }
  65. static void power_off() {
  66. if (!m_poweronflag) return;
  67. dsp_mgr_uninit();
  68. sample_data_mgr_uninit();
  69. hwss_uninit();
  70. BoardEcgSensor_unload();
  71. BoardBattery_unload();
  72. BoardLight_unload();
  73. BoardBeepCtrl_unload();
  74. zble_module_disconnect();
  75. ble_cmder_stop_adv();
  76. ble_cmder_uninit();
  77. m_poweronflag = false;
  78. }
  79. void ENTER_DEEP_SLEEP() {
  80. // ��������˯��ǰ��ʹ�ܻ�������
  81. BoardEcgSensor_set_sence_state();
  82. BoardBattery_sence_gpio_init_before_sleep();
  83. // nrf_sdh_disable_request();
  84. app_timer_pause();
  85. nrf_sdh_suspend();
  86. sd_power_system_off();
  87. NVIC_SystemReset();
  88. }
  89. /*******************************************************************************
  90. * ״̬л *
  91. *******************************************************************************/
  92. /**
  93. * @brief л״̬
  94. */
  95. static void state_machine__change_to_charging_state() { //
  96. power_on();
  97. // �л���������ʾҳ��
  98. dsp_mgr_change_to_chargingPage();
  99. ds_change_to_state(kdevice_state_charging);
  100. }
  101. /**
  102. * @brief л״̬
  103. */
  104. static void state_machine__change_to_standby_state() {
  105. ZLOGI_BLOCK("change to standby");
  106. power_off();
  107. ds_change_to_state(kdevice_state_standby);
  108. }
  109. /**
  110. * @brief лл
  111. */
  112. static void state_machine__change_to_poweroning_state() {
  113. power_on();
  114. dsp_mgr_change_to_welcome();
  115. ds_change_to_state(kdevice_state_poweron);
  116. }
  117. /**
  118. * @brief лҳ
  119. */
  120. static void state_machine__change_to_home_state() {
  121. ds_change_to_state(kdevice_state_home);
  122. dsp_mgr_change_to_main();
  123. }
  124. static void app_event_listener(void* p_event_data, uint16_t event_size) {
  125. if (!p_event_data) return;
  126. app_event_t* p_event = (app_event_t*)p_event_data;
  127. if (ds_now_state() != kdevice_state_standby && //
  128. ds_now_state() != kdevice_state_charging && //
  129. ds_now_state() != kdevice_state_poweron //
  130. ) {
  131. if (zdatachannel_is_connected()) {
  132. BoardLight_setGreenLightEffect(kLightEffect_slowFlash);
  133. } else {
  134. BoardLight_setGreenLightEffect(kLightEffect_quickFlash);
  135. }
  136. }
  137. /*******************************************************************************
  138. * ״̬ *
  139. *******************************************************************************/
  140. static uint32_t lasttrypoweron_time = 0;
  141. if (ds_now_state() == kdevice_state_standby) {
  142. // �����¼����� --> �л�������ҳ��
  143. if (BoardBattery_get_charging_state()) {
  144. state_machine__change_to_charging_state();
  145. }
  146. // ���������¼����� --> �л���������ҳ��
  147. else if (BoardEcgSensor_plod_get_connected_state_after_filter() && znordic_haspassed_ms(lasttrypoweron_time) >= LOW_BATTERY_REMINDER_DELAY_MS) {
  148. lasttrypoweron_time = znordic_getpower_on_ms();
  149. if (BoardBattery_get_battery_level() > APP_WORK_BATTERY_LEVEL) {
  150. state_machine__change_to_poweroning_state();
  151. } else {
  152. BoardLight_load();
  153. BoardLight_blockFlash(3, 100);
  154. BoardLight_unload();
  155. }
  156. }
  157. // 10����������RTCδ�����ù��������볬�͹���
  158. else if (ds_cur_state_haspassed_ms() >= 10000) {
  159. if (!znordic_rtc_has_setted()) {
  160. ENTER_DEEP_SLEEP();
  161. }
  162. if (APP_BATTERY_PROTECT_LEVEL > BoardBattery_get_battery_level()) {
  163. ENTER_DEEP_SLEEP();
  164. }
  165. }
  166. }
  167. /*******************************************************************************
  168. * ״̬ *
  169. *******************************************************************************/
  170. else if (ds_now_state() == kdevice_state_charging) {
  171. if (!BoardBattery_get_charging_state()) {
  172. state_machine__change_to_standby_state();
  173. }
  174. }
  175. /*******************************************************************************
  176. * *
  177. *******************************************************************************/
  178. else if (ds_now_state() == kdevice_state_poweron) {
  179. if (ds_cur_state_haspassed_ms() >= 1500) {
  180. state_machine__change_to_home_state();
  181. }
  182. }
  183. /*******************************************************************************
  184. * ҳ *
  185. *******************************************************************************/
  186. else if (ds_now_state() == kdevice_state_home) {
  187. // �����û���ʱ�䲻�������Զ��л�������״̬
  188. if (!zdatachannel_is_connected() && //
  189. !BoardEcgSensor_plod_get_connected_state_after_filter() && //
  190. BoardEcgSensor_plod_state_has_disconnected_ms() >= 3000 && //
  191. ds_cur_state_haspassed_ms() >= AUTOMATIC_SLEEP_TIME) {
  192. state_machine__change_to_standby_state();
  193. }
  194. // �����û��������־�ֹ���л����ɼ�ҳ��
  195. else if (BoardEcgSensor_plod_get_connected_state_after_filter() && ds_cur_state_haspassed_ms() > 1500) {
  196. // dsp_mgr_change_to_preparePage
  197. if (!BoardBattery_get_charging_state()) {
  198. ds_change_to_state(kdevice_state_keep_still);
  199. dsp_mgr_change_to_preparePage();
  200. hwss_start_prepare_capture();
  201. }
  202. }
  203. // �͵������豸��������ģʽ
  204. else if (BoardBattery_get_battery_level() < APP_AUTO_STANDY_BATTERY_LEVEL && ds_cur_state_haspassed_ms() > 3000) {
  205. state_machine__change_to_standby_state();
  206. } //
  207. else if (zble_module_is_connected() && zdatachannel_last_rx_data_haspassed_s() >= BLE_UNCONNECTED_OVERTIME_S) {
  208. ZLOGI("auto sleep because ble unconnected");
  209. state_machine__change_to_standby_state();
  210. }
  211. else if (BoardBattery_get_charging_state()) {
  212. // BoardLight_setGreenLightEffect(kLightEffect_close);
  213. state_machine__change_to_standby_state();
  214. }
  215. // 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());
  216. }
  217. /*******************************************************************************
  218. * ־ֹҳ *
  219. *******************************************************************************/
  220. else if (ds_now_state() == kdevice_state_keep_still) {
  221. if (!BoardEcgSensor_plod_get_connected_state_after_filter()) {
  222. // �����û�δ���־�ֹ���л�����ҳ
  223. state_machine__change_to_home_state();
  224. hwss_stop_capture();
  225. } else {
  226. /*******************************************************************************
  227. * ҳ *
  228. *******************************************************************************/
  229. if (ds_cur_state_haspassed_ms() >= (KEEP_STILL_OVERTIME_MS_1P5 * 5)) {
  230. /**
  231. * @brief
  232. *
  233. * 1. ʱ
  234. * 2. л״̬
  235. * 3. лҳ
  236. */
  237. ds_change_to_state(kdevice_state_sampling);
  238. dsp_mgr_change_to_sampling(0, 0);
  239. hwss_start_capture();
  240. static ztm_t tm;
  241. memset(&sampledata_file_name, 0, sizeof(sampledata_file_name));
  242. znordic_rtc_gettime(&tm);
  243. sample_capture_state_reset();
  244. sampledata_file_name.year = tm.tm_year + 1900 - 2000;
  245. sampledata_file_name.month = tm.tm_mon + 1;
  246. sampledata_file_name.day = tm.tm_mday;
  247. sampledata_file_name.hour = tm.tm_hour;
  248. sampledata_file_name.min = tm.tm_min;
  249. sampledata_file_name.sec = tm.tm_sec;
  250. m_cur_fd = sample_data_mgr_open(&sampledata_file_name, kwrflag_write_only);
  251. BoardBeepCtrl_setEffect(kBoardBeepEffect_oneShortBeep);
  252. } else if (ds_cur_state_haspassed_ms() >= (KEEP_STILL_OVERTIME_MS_1P5 * 4)) {
  253. if (dsp_mgr_preparePage_get_progress() != 4) {
  254. dsp_mgr_preparePage_set_progress(4);
  255. }
  256. } else if (ds_cur_state_haspassed_ms() >= (KEEP_STILL_OVERTIME_MS_1P5 * 3)) {
  257. if (dsp_mgr_preparePage_get_progress() != 3) {
  258. dsp_mgr_preparePage_set_progress(3);
  259. }
  260. } else if (ds_cur_state_haspassed_ms() >= (KEEP_STILL_OVERTIME_MS_1P5 * 2)) {
  261. if (dsp_mgr_preparePage_get_progress() != 2) {
  262. dsp_mgr_preparePage_set_progress(2);
  263. }
  264. } else if (ds_cur_state_haspassed_ms() >= (KEEP_STILL_OVERTIME_MS_1P5 * 1)) {
  265. if (dsp_mgr_preparePage_get_progress() != 1) {
  266. dsp_mgr_preparePage_set_progress(1);
  267. }
  268. }
  269. }
  270. }
  271. /*******************************************************************************
  272. * ҳ߼ *
  273. *******************************************************************************/
  274. else if (ds_now_state() == kdevice_state_sampling) {
  275. /*******************************************************************************
  276. * ݴ *
  277. *******************************************************************************/
  278. if (p_event->eventType == kevent_capture_256data_event) {
  279. if (hwss_has_captured_time_ms() <= (MAX_STORAGE_TIMEOUT_S * 1000)) {
  280. sample_data_mgr_write(m_cur_fd, p_event->val.capture_data_cache, 256);
  281. }
  282. }
  283. /*******************************************************************************
  284. * ʵʱϱ *
  285. *******************************************************************************/
  286. if (p_event->eventType == kevent_capture_little_data_block_event) {
  287. // ��֡ʵʱ�ϱ�
  288. ZASSERT(LITTLE_DATA_BLOCK_FRAME_NUM == 5);
  289. ble_cmder_try_report_one_sample_data(p_event->val.little_data_block.frameIndex, //
  290. p_event->val.little_data_block.data[0].data, //
  291. p_event->val.little_data_block.data[1].data, //
  292. p_event->val.little_data_block.data[2].data, //
  293. p_event->val.little_data_block.data[3].data, //
  294. p_event->val.little_data_block.data[4].data);
  295. }
  296. /*******************************************************************************
  297. * 30¼ *
  298. *******************************************************************************/
  299. if (hwss_has_captured_time_ms() >= (SAMPLE_MIN_TIME_S * 1000)) {
  300. if (!sample_capture_state_get()->is_over30s) {
  301. BoardBeepCtrl_setEffect(kBoardBeepEffect_oneShortBeep);
  302. sample_capture_state_set_is_over30s(true);
  303. }
  304. }
  305. /*******************************************************************************
  306. * ж *
  307. *******************************************************************************/
  308. bool capture_end = false;
  309. if (hwss_has_captured_time_ms() >= (MAX_STORAGE_TIMEOUT_S * 1000)) {
  310. capture_end = true;
  311. } else if (p_event->eventType == kplod_disconnected_event) {
  312. ble_cmder_try_report_sensor_drop_event(0x01, 0);
  313. capture_end = true;
  314. }
  315. if (capture_end) {
  316. if (sample_capture_state_get()->is_over30s) {
  317. sample_data_mgr_close(m_cur_fd);
  318. dsp_mgr_change_to_sampleSuc();
  319. BoardBeepCtrl_setEffect(kBoardBeepEffect_oneShortBeep);
  320. ds_change_to_state(kdevice_state_sampling_complete);
  321. hwss_stop_capture();
  322. } else {
  323. // ����������ʮ��
  324. // ֹͣ����
  325. hwss_stop_capture();
  326. // �ر��ļ�
  327. sample_data_mgr_close(m_cur_fd);
  328. // ɾ���ļ�
  329. sample_data_mgr_delete_file(&sampledata_file_name);
  330. // �л����ɼ�����ҳ��
  331. dsp_mgr_change_to_samplingError();
  332. BoardBeepCtrl_setEffect(kBoardBeepEffect_threeShortBeep);
  333. ds_change_to_state(kdevice_state_sampling_error);
  334. }
  335. }
  336. }
  337. /*******************************************************************************
  338. * ҳ *
  339. *******************************************************************************/
  340. else if (ds_now_state() == kdevice_state_sampling_complete) {
  341. if (ds_cur_state_haspassed_ms() >= 3000) {
  342. ZLOGI("ds_cur_state_haspassed_ms() %d> 3000", ds_cur_state_haspassed_ms());
  343. state_machine__change_to_home_state();
  344. ble_cmder_report_sample_finish_event();
  345. }
  346. }
  347. /*******************************************************************************
  348. * ҳ *
  349. *******************************************************************************/
  350. else if (ds_now_state() == kdevice_state_sampling_error) {
  351. if ((ds_cur_state_haspassed_ms() >= 3000) || //
  352. (ds_cur_state_haspassed_ms() >= 1000 && BoardEcgSensor_plod_get_connected_state_after_filter())) {
  353. ZLOGI("ds_cur_state_haspassed_ms() %d> 3000", ds_cur_state_haspassed_ms());
  354. state_machine__change_to_home_state();
  355. ble_cmder_report_sample_finish_event();
  356. }
  357. }
  358. }
  359. /*******************************************************************************
  360. * MAIN_CODE *
  361. *******************************************************************************/
  362. void one_conduction_main() {
  363. ZLOGI("one_conduction_main");
  364. BoardBeepCtrl_init();
  365. AppEvent_regListener(app_event_listener);
  366. BoardBattery_init();
  367. BoardEcgSensor_init();
  368. BoardLight_Init();
  369. BoardBattery_load();
  370. #if 1
  371. if (BoardBattery_get_battery_level() < APP_WORK_BATTERY_LEVEL) {
  372. BoardLight_load();
  373. BoardLight_blockFlash(3, 100);
  374. BoardLight_unload();
  375. // ϵͳ��������˯��,��������˯��ǰ��ʹ�ܻ�������
  376. app_timer_pause();
  377. nrf_delay_ms(LOW_BATTERY_REMINDER_DELAY_MS);
  378. ENTER_DEEP_SLEEP();
  379. }
  380. #endif
  381. // wd_init();
  382. ZERROR_CHECK(app_timer_create(&m_state_machine_driver_tmr, APP_TIMER_MODE_REPEATED, state_machine_driver_tmr_cb));
  383. ZERROR_CHECK(app_timer_start(m_state_machine_driver_tmr, APP_TIMER_TICKS(300), NULL));
  384. state_machine__change_to_standby_state();
  385. znordic_loop();
  386. }