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.

392 lines
15 KiB

2 years ago
1 year 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
1 year ago
2 years ago
1 year ago
2 years ago
1 year 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
1 year ago
2 years ago
1 year ago
1 year ago
1 year ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
1 year 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
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year 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
1 year ago
2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
  1. #include <math.h>
  2. #include "basic_service/app_event.h"
  3. #include "basic_service/app_event_distribute.h"
  4. #include "basic_service/device_version_info_mgr.h"
  5. //
  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 "board/board_power_mgr.h"
  12. //
  13. #include "board/ads129x/ads129x.h"
  14. #include "board/app_board.h"
  15. #include "service/ble_cmd_processer/ble_cmd_process_service.h"
  16. #include "service/display_mgr/display_manager.h"
  17. #include "service/heart_wave_sample_service/heart_wave_sample_service.h"
  18. #include "service/storage/storage_service.h"
  19. #include "zble_module.h"
  20. #include "zdatachannel_service.h"
  21. #include "znordic.h"
  22. /*******************************************************************************
  23. * GLOBAL *
  24. *******************************************************************************/
  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. void ENTER_DEEP_SLEEP() {
  32. // ��������˯��ǰ��ʹ�ܻ�������
  33. BoardBattery_sence_gpio_init_before_sleep();
  34. // nrf_sdh_disable_request();
  35. app_timer_pause();
  36. nrf_sdh_suspend();
  37. sd_power_system_off();
  38. NVIC_SystemReset();
  39. }
  40. /*******************************************************************************
  41. * ״̬л *
  42. *******************************************************************************/
  43. static void on_state_change(device_state_t from, device_state_t to) {
  44. /**
  45. * @brief ػ
  46. */
  47. if (from == kdevice_state_standby && to != kdevice_state_standby) {
  48. /**
  49. * @brief л״̬
  50. */
  51. BoardBeepCtrl_load();
  52. BoardLight_load();
  53. sample_data_mgr_init();
  54. dsp_mgr_init();
  55. ble_cmder_init();
  56. ble_cmder_start_adv();
  57. app_board_change_state(kapp_power_state_working);
  58. } else if (to == kdevice_state_standby) {
  59. /**
  60. * @brief лģʽ
  61. *
  62. */
  63. dsp_mgr_uninit();
  64. sample_data_mgr_uninit();
  65. BoardLight_unload();
  66. BoardBeepCtrl_unload();
  67. ble_stop_upload_record();
  68. zble_module_disconnect();
  69. for (size_t i = 0; i < 100; i++) {
  70. nrf_delay_ms(10);
  71. if (!zble_module_is_connected()) {
  72. break;
  73. }
  74. }
  75. ble_cmder_stop_adv();
  76. ble_cmder_uninit();
  77. board_power_mgr_main_power_supply_set(false);
  78. app_board_change_state(kapp_power_state_standby);
  79. }
  80. /***********************************************************************************************************************
  81. * л״̬ *
  82. ***********************************************************************************************************************/
  83. if (to == kdevice_state_home) {
  84. dsp_mgr_change_to_main();
  85. } else if (to == kdevice_state_poweron) {
  86. dsp_mgr_change_to_welcome();
  87. } else if (to == kdevice_state_charging) {
  88. dsp_mgr_change_to_chargingPage();
  89. }
  90. }
  91. static void app_event_listener(void* p_event_data, uint16_t event_size) {
  92. if (!p_event_data) return;
  93. app_event_t* p_event = (app_event_t*)p_event_data;
  94. if (ds_now_state() != kdevice_state_standby && //
  95. ds_now_state() != kdevice_state_charging && //
  96. ds_now_state() != kdevice_state_poweron //
  97. ) {
  98. if (zdatachannel_is_connected()) {
  99. BoardLight_setGreenLightEffect(kLightEffect_slowFlash);
  100. } else {
  101. BoardLight_setGreenLightEffect(kLightEffect_quickFlash);
  102. }
  103. }
  104. /*******************************************************************************
  105. * ״̬ *
  106. *******************************************************************************/
  107. static uint32_t lasttrypoweron_time = 0;
  108. if (ds_now_state() == kdevice_state_standby) {
  109. // �����¼����� --> �л�������ҳ��
  110. if (BoardBattery_get_charging_state()) {
  111. ds_change_to_state(kdevice_state_charging);
  112. }
  113. // ���������¼����� --> �л���������ҳ��
  114. else if (hwss_lead_get_state_connected_state() && znordic_haspassed_ms(lasttrypoweron_time) >= LOW_BATTERY_REMINDER_DELAY_MS) {
  115. lasttrypoweron_time = znordic_getpower_on_ms();
  116. // if (BoardBattery_get_battery_level() > APP_WORK_BATTERY_LEVEL) {
  117. // TODO: �������ص���
  118. if (true) {
  119. ds_change_to_state(kdevice_state_poweron);
  120. } else {
  121. BoardLight_load();
  122. BoardLight_blockFlash(3, 100);
  123. BoardLight_unload();
  124. }
  125. }
  126. /**
  127. * @brief
  128. * TODO:
  129. * 1. Զ͹״ֱ̬صһ̶
  130. * 2. RTCδã״̬
  131. * 3. ÿδӴ״̬л״̬RTCδãѾIJ
  132. */
  133. else if (ds_cur_state_haspassed_ms() >= 10000) {
  134. if (!znordic_rtc_has_setted()) {
  135. }
  136. }
  137. }
  138. /*******************************************************************************
  139. * ״̬ *
  140. *******************************************************************************/
  141. else if (ds_now_state() == kdevice_state_charging) {
  142. if (!BoardBattery_get_charging_state()) {
  143. ds_change_to_state(kdevice_state_standby);
  144. }
  145. }
  146. /*******************************************************************************
  147. * *
  148. *******************************************************************************/
  149. else if (ds_now_state() == kdevice_state_poweron) {
  150. if (ds_cur_state_haspassed_ms() >= 1500) {
  151. ds_change_to_state(kdevice_state_home);
  152. }
  153. }
  154. /*******************************************************************************
  155. * ҳ *
  156. *******************************************************************************/
  157. else if (ds_now_state() == kdevice_state_home) {
  158. // �����û���ʱ�䲻�������Զ��л�������״̬
  159. if (!zdatachannel_is_connected() && //
  160. !hwss_lead_get_state_connected_state() && //
  161. ds_cur_state_haspassed_ms() >= AUTOMATIC_SLEEP_TIME) {
  162. ds_change_to_state(kdevice_state_standby);
  163. }
  164. // �����û��������־�ֹ���л����ɼ�ҳ��
  165. else if (hwss_lead_get_state_connected_state() && ds_cur_state_haspassed_ms() > 1500) {
  166. // dsp_mgr_change_to_preparePage
  167. if (!BoardBattery_get_charging_state()) {
  168. ds_change_to_state(kdevice_state_keep_still);
  169. dsp_mgr_change_to_preparePage();
  170. hwss_start_prepare_capture();
  171. if (ble_is_upload_record()) {
  172. ble_stop_upload_record();
  173. }
  174. }
  175. }
  176. #if 0
  177. //TODO:ʹ��
  178. // �͵������豸��������ģʽ
  179. else if (BoardBattery_get_battery_level() < APP_AUTO_STANDY_BATTERY_LEVEL && ds_cur_state_haspassed_ms() > 3000) {
  180. ds_change_to_state(kdevice_state_standby);
  181. } //
  182. #endif
  183. else if (!ble_is_upload_record() && zble_module_is_connected() && zdatachannel_last_rx_data_haspassed_s() >= BLE_UNCONNECTED_OVERTIME_S) {
  184. ZLOGI("auto sleep because ble unconnected");
  185. ds_change_to_state(kdevice_state_standby);
  186. }
  187. else if (BoardBattery_get_charging_state()) {
  188. // BoardLight_setGreenLightEffect(kLightEffect_close);
  189. ds_change_to_state(kdevice_state_standby);
  190. }
  191. }
  192. /*******************************************************************************
  193. * ־ֹҳ *
  194. *******************************************************************************/
  195. else if (ds_now_state() == kdevice_state_keep_still) {
  196. if (!hwss_lead_get_state_connected_state()) {
  197. // �����û�δ���־�ֹ���л�����ҳ
  198. ds_change_to_state(kdevice_state_home);
  199. hwss_stop_capture();
  200. } else {
  201. /*******************************************************************************
  202. * ҳ *
  203. *******************************************************************************/
  204. if (ds_cur_state_haspassed_ms() >= (KEEP_STILL_OVERTIME_MS_1P5 * 5)) {
  205. /**
  206. * @brief
  207. *
  208. * 1. ʱ
  209. * 2. л״̬
  210. * 3. лҳ
  211. */
  212. ds_change_to_state(kdevice_state_sampling);
  213. dsp_mgr_change_to_sampling(0, 0);
  214. hwss_start_capture();
  215. static ztm_t tm;
  216. memset(&sampledata_file_name, 0, sizeof(sampledata_file_name));
  217. znordic_rtc_gettime(&tm);
  218. sample_capture_state_reset();
  219. sampledata_file_name.year = tm.tm_year + 1900 - 2000;
  220. sampledata_file_name.month = tm.tm_mon + 1;
  221. sampledata_file_name.day = tm.tm_mday;
  222. sampledata_file_name.hour = tm.tm_hour;
  223. sampledata_file_name.min = tm.tm_min;
  224. sampledata_file_name.sec = tm.tm_sec;
  225. m_cur_fd = sample_data_mgr_open(&sampledata_file_name, kwrflag_write_only);
  226. BoardBeepCtrl_setEffect(kBoardBeepEffect_oneShortBeep);
  227. } else if (ds_cur_state_haspassed_ms() >= (KEEP_STILL_OVERTIME_MS_1P5 * 4)) {
  228. if (dsp_mgr_preparePage_get_progress() != 4) {
  229. dsp_mgr_preparePage_set_progress(4);
  230. }
  231. } else if (ds_cur_state_haspassed_ms() >= (KEEP_STILL_OVERTIME_MS_1P5 * 3)) {
  232. if (dsp_mgr_preparePage_get_progress() != 3) {
  233. dsp_mgr_preparePage_set_progress(3);
  234. }
  235. } else if (ds_cur_state_haspassed_ms() >= (KEEP_STILL_OVERTIME_MS_1P5 * 2)) {
  236. if (dsp_mgr_preparePage_get_progress() != 2) {
  237. dsp_mgr_preparePage_set_progress(2);
  238. }
  239. } else if (ds_cur_state_haspassed_ms() >= (KEEP_STILL_OVERTIME_MS_1P5 * 1)) {
  240. if (dsp_mgr_preparePage_get_progress() != 1) {
  241. dsp_mgr_preparePage_set_progress(1);
  242. }
  243. }
  244. }
  245. }
  246. /*******************************************************************************
  247. * ҳ߼ *
  248. *******************************************************************************/
  249. else if (ds_now_state() == kdevice_state_sampling) {
  250. /*******************************************************************************
  251. * ݴ *
  252. *******************************************************************************/
  253. if (p_event->eventType == kevent_capture_256data_event) {
  254. if (hwss_has_captured_time_ms() <= (MAX_STORAGE_TIMEOUT_S * 1000)) {
  255. sample_data_mgr_write(m_cur_fd, p_event->val.capture_data_cache, 256);
  256. }
  257. }
  258. /*******************************************************************************
  259. * ʵʱϱ *
  260. *******************************************************************************/
  261. if (p_event->eventType == kevent_capture_little_data_block_event) {
  262. // ��֡ʵʱ�ϱ�
  263. ZASSERT(LITTLE_DATA_BLOCK_FRAME_NUM == 5);
  264. ble_cmder_try_report_one_sample_data(p_event->val.little_data_block.frameIndex, //
  265. p_event->val.little_data_block.data[0], //
  266. p_event->val.little_data_block.data[1], //
  267. p_event->val.little_data_block.data[2], //
  268. p_event->val.little_data_block.data[3], //
  269. p_event->val.little_data_block.data[4]);
  270. }
  271. /*******************************************************************************
  272. * 30¼ *
  273. *******************************************************************************/
  274. if (hwss_has_captured_time_ms() >= (SAMPLE_MIN_TIME_S * 1000)) {
  275. if (!sample_capture_state_get()->is_over30s) {
  276. BoardBeepCtrl_setEffect(kBoardBeepEffect_oneShortBeep);
  277. sample_capture_state_set_is_over30s(true);
  278. }
  279. }
  280. /*******************************************************************************
  281. * ж *
  282. *******************************************************************************/
  283. bool capture_end = false;
  284. if (hwss_has_captured_time_ms() >= (MAX_STORAGE_TIMEOUT_S * 1000)) {
  285. capture_end = true;
  286. } else if (p_event->eventType == kplod_disconnected_event) {
  287. ble_cmder_try_report_sensor_drop_event(0x01, 0);
  288. capture_end = true;
  289. }
  290. if (capture_end) {
  291. if (sample_capture_state_get()->is_over30s) {
  292. sample_data_mgr_close(m_cur_fd);
  293. dsp_mgr_change_to_sampleSuc();
  294. BoardBeepCtrl_setEffect(kBoardBeepEffect_oneShortBeep);
  295. ds_change_to_state(kdevice_state_sampling_complete);
  296. hwss_stop_capture();
  297. } else {
  298. // ����������ʮ��
  299. // ֹͣ����
  300. hwss_stop_capture();
  301. // �ر��ļ�
  302. sample_data_mgr_close(m_cur_fd);
  303. // ɾ���ļ�
  304. sample_data_mgr_delete_file(&sampledata_file_name);
  305. // �л����ɼ�����ҳ��
  306. dsp_mgr_change_to_samplingError();
  307. BoardBeepCtrl_setEffect(kBoardBeepEffect_threeShortBeep);
  308. ds_change_to_state(kdevice_state_sampling_error);
  309. }
  310. }
  311. }
  312. /*******************************************************************************
  313. * ҳ *
  314. *******************************************************************************/
  315. else if (ds_now_state() == kdevice_state_sampling_complete) {
  316. if (ds_cur_state_haspassed_ms() >= 3000) {
  317. ZLOGI("ds_cur_state_haspassed_ms() %d> 3000", ds_cur_state_haspassed_ms());
  318. ds_change_to_state(kdevice_state_home);
  319. ble_cmder_report_sample_finish_event();
  320. }
  321. }
  322. /*******************************************************************************
  323. * ҳ *
  324. *******************************************************************************/
  325. else if (ds_now_state() == kdevice_state_sampling_error) {
  326. if ((ds_cur_state_haspassed_ms() >= 3000) || //
  327. (ds_cur_state_haspassed_ms() >= 1000 && hwss_lead_get_state_connected_state())) {
  328. ZLOGI("ds_cur_state_haspassed_ms() %d> 3000", ds_cur_state_haspassed_ms());
  329. ds_change_to_state(kdevice_state_home);
  330. ble_cmder_report_sample_finish_event();
  331. }
  332. }
  333. }
  334. /*******************************************************************************
  335. * MAIN_CODE *
  336. *******************************************************************************/
  337. void one_conduction_main() {
  338. ZLOGI("one_conduction_main");
  339. AppEvent_regListener(app_event_listener);
  340. app_board_init();
  341. BoardBeepCtrl_init();
  342. BoardLight_Init();
  343. /**
  344. * @brief ʼ
  345. */
  346. hwss_init();
  347. ds_init(on_state_change);
  348. ds_change_to_state(kdevice_state_poweron);
  349. znordic_loop();
  350. }