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.

391 lines
15 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year 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. AppEvent_pushEvent(&appevent);
  44. }
  45. /*******************************************************************************
  46. * ¼ *
  47. *******************************************************************************/
  48. static bool m_poweronflag;
  49. static void power_on() {
  50. if (m_poweronflag) {
  51. return;
  52. }
  53. BoardBeepCtrl_load();
  54. BoardEcgSensor_load();
  55. BoardBattery_load();
  56. hwss_init();
  57. sample_data_mgr_init();
  58. dsp_mgr_init();
  59. ble_cmder_init();
  60. ble_cmder_start_adv();
  61. m_poweronflag = true;
  62. }
  63. static void power_off() {
  64. if (!m_poweronflag) return;
  65. dsp_mgr_uninit();
  66. sample_data_mgr_uninit();
  67. hwss_uninit();
  68. BoardEcgSensor_unload();
  69. BoardBattery_unload();
  70. BoardLight_unload();
  71. BoardBeepCtrl_unload();
  72. ble_cmder_stop_adv();
  73. ble_cmder_uninit();
  74. m_poweronflag = false;
  75. }
  76. /*******************************************************************************
  77. * ״̬л *
  78. *******************************************************************************/
  79. /**
  80. * @brief л״̬
  81. */
  82. static void state_machine__change_to_charging_state() { //
  83. if (ds_now_state() == kdevice_state_home) {
  84. // �ѿ��� DO Nothing
  85. } else if (ds_now_state() == kdevice_state_standby) {
  86. power_on();
  87. } else {
  88. ZASSERT(false);
  89. }
  90. // �л���������ʾҳ��
  91. dsp_mgr_change_to_chargingPage();
  92. ds_change_to_state(kdevice_state_charging);
  93. }
  94. /**
  95. * @brief л״̬
  96. */
  97. static void state_machine__change_to_standby_state() {
  98. ZLOGI_BLOCK("change to standby");
  99. power_off();
  100. ds_change_to_state(kdevice_state_standby);
  101. }
  102. /**
  103. * @brief лл
  104. */
  105. static void state_machine__change_to_poweroning_state() {
  106. power_on();
  107. dsp_mgr_change_to_welcome();
  108. ds_change_to_state(kdevice_state_poweron);
  109. }
  110. /**
  111. * @brief лҳ
  112. */
  113. static void state_machine__change_to_home_state() {
  114. ds_change_to_state(kdevice_state_home);
  115. dsp_mgr_change_to_main();
  116. }
  117. void ENTER_DEEP_SLEEP() {
  118. // ��������˯��ǰ��ʹ�ܻ�������
  119. BoardEcgSensor_set_sence_state();
  120. BoardBattery_sence_gpio_init_before_sleep();
  121. // nrf_sdh_disable_request();
  122. app_timer_stop_all();
  123. // nrf_sdh_disable_request();
  124. sd_power_system_off();
  125. NVIC_SystemReset();
  126. }
  127. static void app_event_listener(void* p_event_data, uint16_t event_size) {
  128. if (!p_event_data) return;
  129. app_event_t* p_event = (app_event_t*)p_event_data;
  130. if (ds_now_state() != kdevice_state_standby) {
  131. if (zdatachannel_is_connected()) {
  132. BoardLight_setGreenLightEffect(kLightEffect_slowFlash);
  133. } else {
  134. BoardLight_setGreenLightEffect(kLightEffect_quickFlash);
  135. }
  136. }
  137. /*******************************************************************************
  138. * ״̬ *
  139. *******************************************************************************/
  140. if (ds_now_state() == kdevice_state_standby) {
  141. ZLOGI("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()) {
  148. state_machine__change_to_poweroning_state();
  149. }
  150. // 10����������RTCδ�����ù��������볬�͹���
  151. else if (ds_cur_state_haspassed_ms() >= 2000) {
  152. if (!znordic_rtc_has_setted()) {
  153. ENTER_DEEP_SLEEP();
  154. }
  155. }
  156. }
  157. /*******************************************************************************
  158. * ״̬ *
  159. *******************************************************************************/
  160. else if (ds_now_state() == kdevice_state_charging) {
  161. if (!BoardBattery_get_charging_state()) {
  162. state_machine__change_to_standby_state();
  163. }
  164. }
  165. /*******************************************************************************
  166. * *
  167. *******************************************************************************/
  168. else if (ds_now_state() == kdevice_state_poweron) {
  169. if (ds_cur_state_haspassed_ms() >= 1500) {
  170. state_machine__change_to_home_state();
  171. }
  172. }
  173. /*******************************************************************************
  174. * ҳ *
  175. *******************************************************************************/
  176. else if (ds_now_state() == kdevice_state_home) {
  177. // �����û���ʱ�䲻�������Զ��л�������״̬
  178. if (!zdatachannel_is_connected() && //
  179. !BoardEcgSensor_plod_get_connected_state_after_filter() && //
  180. BoardEcgSensor_plod_state_has_disconnected_ms() >= 3000 && //
  181. ds_cur_state_haspassed_ms() >= AUTOMATIC_SLEEP_TIME) {
  182. state_machine__change_to_standby_state();
  183. }
  184. // �����û��������־�ֹ���л����ɼ�ҳ��
  185. else if (BoardEcgSensor_plod_get_connected_state_after_filter() && ds_cur_state_haspassed_ms() > 1500) {
  186. // dsp_mgr_change_to_preparePage
  187. ds_change_to_state(kdevice_state_keep_still);
  188. dsp_mgr_change_to_preparePage();
  189. }
  190. // 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());
  191. }
  192. /*******************************************************************************
  193. * ־ֹҳ *
  194. *******************************************************************************/
  195. else if (ds_now_state() == kdevice_state_keep_still) {
  196. if (!BoardEcgSensor_plod_get_connected_state_after_filter()) {
  197. // �����û�δ���־�ֹ���л�����ҳ
  198. state_machine__change_to_home_state();
  199. } else {
  200. /*******************************************************************************
  201. * ҳ *
  202. *******************************************************************************/
  203. if (ds_cur_state_haspassed_ms() >= (KEEP_STILL_OVERTIME_MS_1P5 * 5)) {
  204. /**
  205. * @brief
  206. *
  207. * 1. ʱ
  208. * 2. л״̬
  209. * 3. лҳ
  210. */
  211. ds_change_to_state(kdevice_state_sampling);
  212. dsp_mgr_change_to_sampling(0, 0);
  213. hwss_start_capture();
  214. static ztm_t tm;
  215. memset(&sampledata_file_name, 0, sizeof(sampledata_file_name));
  216. znordic_rtc_gettime(&tm);
  217. sample_capture_state_reset();
  218. sampledata_file_name.year = tm.tm_year + 1900 - 2000;
  219. sampledata_file_name.month = tm.tm_mon + 1;
  220. sampledata_file_name.day = tm.tm_mday;
  221. sampledata_file_name.hour = tm.tm_hour;
  222. sampledata_file_name.min = tm.tm_min;
  223. sampledata_file_name.sec = tm.tm_sec;
  224. m_cur_fd = sample_data_mgr_open(&sampledata_file_name, kwrflag_write_only);
  225. BoardBeepCtrl_setEffect(kBoardBeepEffect_oneShortBeep);
  226. } else if (ds_cur_state_haspassed_ms() >= (KEEP_STILL_OVERTIME_MS_1P5 * 4)) {
  227. if (dsp_mgr_preparePage_get_progress() != 4) {
  228. dsp_mgr_preparePage_set_progress(4);
  229. }
  230. } else if (ds_cur_state_haspassed_ms() >= (KEEP_STILL_OVERTIME_MS_1P5 * 3)) {
  231. if (dsp_mgr_preparePage_get_progress() != 3) {
  232. dsp_mgr_preparePage_set_progress(3);
  233. }
  234. } else if (ds_cur_state_haspassed_ms() >= (KEEP_STILL_OVERTIME_MS_1P5 * 2)) {
  235. if (dsp_mgr_preparePage_get_progress() != 2) {
  236. dsp_mgr_preparePage_set_progress(2);
  237. }
  238. } else if (ds_cur_state_haspassed_ms() >= (KEEP_STILL_OVERTIME_MS_1P5 * 1)) {
  239. if (dsp_mgr_preparePage_get_progress() != 1) {
  240. dsp_mgr_preparePage_set_progress(1);
  241. }
  242. }
  243. }
  244. }
  245. /*******************************************************************************
  246. * ҳ߼ *
  247. *******************************************************************************/
  248. else if (ds_now_state() == kdevice_state_sampling) {
  249. /*******************************************************************************
  250. * ݴ *
  251. *******************************************************************************/
  252. if (p_event->eventType == kevent_capture_256data_event) {
  253. if (hwss_has_captured_time_ms() <= (MAX_STORAGE_TIMEOUT_S * 1000)) {
  254. sample_data_mgr_write(m_cur_fd, p_event->val.capture_data_cache, 256);
  255. }
  256. }
  257. /*******************************************************************************
  258. * ʵʱϱ *
  259. *******************************************************************************/
  260. if (p_event->eventType == kevent_capture_little_data_block_event) {
  261. // ��֡ʵʱ�ϱ�
  262. ZASSERT(LITTLE_DATA_BLOCK_FRAME_NUM == 5);
  263. ble_cmder_try_report_one_sample_data(p_event->val.little_data_block.frameIndex, //
  264. p_event->val.little_data_block.data[0].data, //
  265. p_event->val.little_data_block.data[1].data, //
  266. p_event->val.little_data_block.data[2].data, //
  267. p_event->val.little_data_block.data[3].data, //
  268. p_event->val.little_data_block.data[4].data);
  269. }
  270. /*******************************************************************************
  271. * 30¼ *
  272. *******************************************************************************/
  273. if (hwss_has_captured_time_ms() >= (SAMPLE_MIN_TIME_S * 1000)) {
  274. if (!sample_capture_state_get()->is_over30s) {
  275. BoardBeepCtrl_setEffect(kBoardBeepEffect_oneShortBeep);
  276. sample_capture_state_set_is_over30s(true);
  277. }
  278. }
  279. /*******************************************************************************
  280. * ж *
  281. *******************************************************************************/
  282. bool capture_end = false;
  283. if (hwss_has_captured_time_ms() >= (MAX_STORAGE_TIMEOUT_S * 1000)) {
  284. capture_end = true;
  285. } else if (p_event->eventType == kplod_disconnected_event) {
  286. ble_cmder_try_report_sensor_drop_event(0x01, 0);
  287. capture_end = true;
  288. }
  289. if (capture_end) {
  290. if (sample_capture_state_get()->is_over30s) {
  291. sample_data_mgr_close(m_cur_fd);
  292. dsp_mgr_change_to_sampleSuc();
  293. BoardBeepCtrl_setEffect(kBoardBeepEffect_oneShortBeep);
  294. ds_change_to_state(kdevice_state_sampling_complete);
  295. hwss_stop_capture();
  296. } else {
  297. // ����������ʮ��
  298. // ֹͣ����
  299. hwss_stop_capture();
  300. // �ر��ļ�
  301. sample_data_mgr_close(m_cur_fd);
  302. // ɾ���ļ�
  303. sample_data_mgr_delete_file(&sampledata_file_name);
  304. // �л����ɼ�����ҳ��
  305. dsp_mgr_change_to_samplingError();
  306. BoardBeepCtrl_setEffect(kBoardBeepEffect_threeShortBeep);
  307. ds_change_to_state(kdevice_state_sampling_error);
  308. }
  309. }
  310. }
  311. /*******************************************************************************
  312. * ҳ *
  313. *******************************************************************************/
  314. else if (ds_now_state() == kdevice_state_sampling_complete) {
  315. if (ds_cur_state_haspassed_ms() >= 3000) {
  316. ZLOGI("ds_cur_state_haspassed_ms() %d> 3000", ds_cur_state_haspassed_ms());
  317. state_machine__change_to_home_state();
  318. ble_cmder_report_sample_finish_event();
  319. }
  320. }
  321. /*******************************************************************************
  322. * ҳ *
  323. *******************************************************************************/
  324. else if (ds_now_state() == kdevice_state_sampling_error) {
  325. if ((ds_cur_state_haspassed_ms() >= 3000) || //
  326. (ds_cur_state_haspassed_ms() >= 1000 && BoardEcgSensor_plod_get_connected_state_after_filter())) {
  327. ZLOGI("ds_cur_state_haspassed_ms() %d> 3000", ds_cur_state_haspassed_ms());
  328. state_machine__change_to_home_state();
  329. ble_cmder_report_sample_finish_event();
  330. }
  331. }
  332. }
  333. /*******************************************************************************
  334. * MAIN_CODE *
  335. *******************************************************************************/
  336. void one_conduction_main() {
  337. BoardBeepCtrl_init();
  338. AppEvent_regListener(app_event_listener);
  339. BoardBattery_init();
  340. BoardEcgSensor_init();
  341. BoardLight_Init();
  342. BoardLight_load();
  343. ZERROR_CHECK(app_timer_create(&m_state_machine_driver_tmr, APP_TIMER_MODE_REPEATED, state_machine_driver_tmr_cb));
  344. ZERROR_CHECK(app_timer_start(m_state_machine_driver_tmr, APP_TIMER_TICKS(300), NULL));
  345. state_machine__change_to_poweroning_state();
  346. znordic_loop();
  347. }