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.

335 lines
12 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
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include "device_ctrl_service.h"
  2. #include "app_event_distribute.h"
  3. #include "board/board_battery_state.h"
  4. #include "board/board_beep_ctrl.h"
  5. #include "board/board_button.h"
  6. #include "board/board_light_ctrl.h"
  7. #include "board/board_sdcard_driver.h"
  8. #include "heart_wave_sample_service.h"
  9. #include "sample_data_manager_service.h"
  10. #include "zble_module.h"
  11. #include "zdatachannel_service.h"
  12. //
  13. APP_TIMER_DEF(m_state_machine_driver_tmr); // ״̬��������ʱ��
  14. static device_state_t m_device_state = kdevice_state_standby; // �豸״̬
  15. static uint32_t m_change_to_cur_state_tp = 0; // �л�����ǰ״̬��ʱ����
  16. static int m_sample_data_fd = -1; // �ɼ������ļ�������
  17. static int m_sample_duration_ms;
  18. /*******************************************************************************
  19. * б *
  20. *******************************************************************************/
  21. void DeviceCtrl_change_to_state(device_state_t state);
  22. uint32_t DeviceCtrl_cur_state_haspassed_ms();
  23. device_state_t DeviceCtrl_now_state();
  24. /*******************************************************************************
  25. * ¼· *
  26. *******************************************************************************/
  27. static void state_machine_driver_tmr_cb(void* p_context) { //
  28. static app_event_t appevent;
  29. appevent.eventType = kevent_tmr_scheduler;
  30. wd_feed();
  31. AppEvent_pushEvent(&appevent);
  32. }
  33. /**
  34. * @brief ¼
  35. */
  36. static void board_button_cb(ButtonIndex_t pin_no, ButtonAction_t button_action) { //
  37. static app_event_t event;
  38. event.eventType = button_action == kButtonAction_push ? kevent_button_push_event : kevent_button_release_event;
  39. AppEvent_pushEvent(&event);
  40. }
  41. static void zble_event_listener(zble_event_t* ble_event) { //
  42. static app_event_t event;
  43. if (ble_event->eventType == kzble_event_connected) {
  44. event.eventType = kevent_ble_connect_event;
  45. AppEvent_pushEvent(&event);
  46. } else if (ble_event->eventType == kzble_event_disconnected) {
  47. event.eventType = kevent_ble_disconnect_event;
  48. AppEvent_pushEvent(&event);
  49. }
  50. }
  51. /*******************************************************************************
  52. * ¼ *
  53. *******************************************************************************/
  54. static bool m_ispoweron = false;
  55. // static bool m_drop_state_triggered = false;
  56. static void poweroff() {
  57. if (!m_ispoweron) return;
  58. BoardBeepCtrl_unload();
  59. BoardLight_unload();
  60. BoardBattery_unload();
  61. BoardButton_unload();
  62. SampleDataMgr_unloadDriver();
  63. // hwss_unload();
  64. DeviceCtrl_change_to_state(kdevice_state_standby);
  65. zble_module_stop_adv();
  66. zble_module_disconnect();
  67. // sd_ble_gap_disconnect()
  68. BoardLight_setGreenLightEffect(kLightEffect_close);
  69. app_timer_stop_all();
  70. // ��������˯��ǰ��ʹ�ܻ�������
  71. BoardButton_enable_sense();
  72. sd_power_system_off();
  73. NVIC_SystemReset();
  74. m_ispoweron = false;
  75. }
  76. static void poweron() {
  77. if (m_ispoweron) {
  78. return;
  79. }
  80. BoardBeepCtrl_load();
  81. BoardLight_load();
  82. BoardBattery_load();
  83. BoardButton_load();
  84. SampleDataMgr_loadDriver();
  85. DeviceCtrl_change_to_state(kdevice_state_ready);
  86. zble_module_start_adv();
  87. BoardLight_setGreenLightEffect(kLightEffect_quickFlash);
  88. m_ispoweron = true;
  89. }
  90. static sample_data_filename_t* cratefilename() {
  91. static ztm_t tm;
  92. static sample_data_filename_t sampledata_file_name;
  93. memset(&sampledata_file_name, 0, sizeof(sampledata_file_name));
  94. znordic_rtc_gettime(&tm);
  95. sampledata_file_name.year = tm.tm_year + 1900 - 2000;
  96. sampledata_file_name.month = tm.tm_mon + 1;
  97. sampledata_file_name.day = tm.tm_mday;
  98. sampledata_file_name.hour = tm.tm_hour;
  99. sampledata_file_name.min = tm.tm_min;
  100. sampledata_file_name.sec = tm.tm_sec;
  101. return &sampledata_file_name;
  102. }
  103. static void prvf_change_to_standby_state() {
  104. poweroff();
  105. DeviceCtrl_change_to_state(kdevice_state_standby);
  106. }
  107. static void prvf_change_to_ready_state() {
  108. poweron();
  109. DeviceCtrl_change_to_state(kdevice_state_ready);
  110. BoardBeepCtrl_setEffect(kBoardBeepEffect_oneShortBeep);
  111. }
  112. #if 0
  113. static const char* dropstate(uint8_t drop0, uint8_t drop1) {
  114. static char state[128];
  115. sprintf(state, "drop0:%d%d%d%d-%d%d%d%d drop1:%d%d%d%d-%d%d%d%d", //
  116. drop0 & 0x80 ? 1 : 0, drop0 & 0x40 ? 1 : 0, drop0 & 0x20 ? 1 : 0, drop0 & 0x10 ? 1 : 0, //
  117. drop0 & 0x08 ? 1 : 0, drop0 & 0x04 ? 1 : 0, drop0 & 0x02 ? 1 : 0, drop0 & 0x01 ? 1 : 0, //
  118. drop1 & 0x80 ? 1 : 0, drop1 & 0x40 ? 1 : 0, drop1 & 0x20 ? 1 : 0, drop1 & 0x10 ? 1 : 0, //
  119. drop1 & 0x08 ? 1 : 0, drop1 & 0x04 ? 1 : 0, drop1 & 0x02 ? 1 : 0, drop1 & 0x01 ? 1 : 0 //
  120. );
  121. return state;
  122. }
  123. #endif
  124. static void prvf_change_to_sample_state() { //
  125. if (m_device_state == kdevice_state_sampling) {
  126. return;
  127. }
  128. DeviceCtrl_change_to_state(kdevice_state_sampling);
  129. }
  130. static void app_event_listener(void* p_event_data, uint16_t event_size) { //
  131. app_event_t* event = (app_event_t*)p_event_data;
  132. // static bool inited;
  133. // if (!inited) {
  134. // nrf_gpio_cfg_input(BUTTON_PIN, NRF_GPIO_PIN_NOPULL);
  135. // inited = true;
  136. // }
  137. // ZLOGI("button %d", nrf_gpio_pin_read(BUTTON_PIN));
  138. /*******************************************************************************
  139. * ״̬޹¼ *
  140. *******************************************************************************/
  141. if (event->eventType == kevent_ble_connect_event) {
  142. BoardLight_setGreenLightEffect(kLightEffect_slowFlash);
  143. return;
  144. } else if (event->eventType == kevent_ble_disconnect_event) {
  145. BoardLight_setGreenLightEffect(kLightEffect_quickFlash);
  146. return;
  147. }
  148. /*******************************************************************************
  149. * ״̬ *
  150. *******************************************************************************/
  151. // ZLOGI("state %d", m_device_state);
  152. if (m_device_state == kdevice_state_standby) {
  153. if (DeviceCtrl_cur_state_haspassed_ms() > 1500) {
  154. }
  155. NVIC_SystemReset();
  156. }
  157. //
  158. else if (m_device_state == kdevice_state_ready) {
  159. if (!zble_module_is_connected() && DeviceCtrl_cur_state_haspassed_ms() > APP_AUTO_SLEEP_TIMEOUT_MS) {
  160. ZLOGI("auto sleep");
  161. prvf_change_to_standby_state();
  162. } else if (zble_module_is_connected() && zdatachannel_last_rx_data_haspassed_s() >= BLE_UNCONNECTED_OVERTIME_S) {
  163. ZLOGI("auto sleep because ble unconnected");
  164. prvf_change_to_standby_state();
  165. } else if (BoardBattery_get_battery_level() < APP_LOW_BATTERY_LIMIT) {
  166. ZLOGI("auto sleep because low battery");
  167. prvf_change_to_standby_state();
  168. }
  169. if (event->eventType == kevent_start_sample_cmd_event) {
  170. ZLOGI("start sample");
  171. // SD�����ӵ���Ƭ��
  172. SampleDataMgr_changeToLocalMode();
  173. hwss_load();
  174. // �����ļ�
  175. m_sample_data_fd = SampleDataMgr_open(cratefilename(), kwrflag_write_only);
  176. ZASSERT(m_sample_data_fd > 0);
  177. // �л����ɼ�״̬
  178. prvf_change_to_sample_state();
  179. // ��ʼ�ɼ�
  180. hwss_start_capture();
  181. {
  182. static app_event_t event;
  183. event.eventType = kevent_sample_start_event;
  184. AppEvent_pushEvent(&event);
  185. }
  186. BoardBeepCtrl_setEffect(kBoardBeepEffect_oneShortBeep);
  187. }
  188. }
  189. //
  190. else if (m_device_state == kdevice_state_sampling) {
  191. // �ɼ����ֽڳ��ȳ��������ֽڳ��ȣ�ֹͣ�ɼ�
  192. if (event->eventType == kevent_capture_data_block_event) {
  193. ZLOGI("capture data block %d", event->val.block_sensor_data.len);
  194. SampleDataMgr_write(m_sample_data_fd, (uint8_t*)event->val.block_sensor_data.data, event->val.block_sensor_data.len);
  195. }
  196. uint8_t dropdata0 = hwss_get_drop_state0();
  197. uint8_t dropdata1 = hwss_get_drop_state1();
  198. if (DeviceCtrl_cur_state_haspassed_ms() > 3000) {
  199. if ((dropdata0 || dropdata1)) {
  200. BoardBeepCtrl_setEffect(kBoardBeepEffect_continuousShortBeep);
  201. } else {
  202. BoardBeepCtrl_setEffect(kBoardBeepEffect_none);
  203. }
  204. }
  205. bool stopcapture = false;
  206. if (SampleDataMgr_getFileSizeByFd(m_sample_data_fd) > SDCARD_MAX_FILE_SIZE) {
  207. ZLOGI("stop sample because file size is too large");
  208. stopcapture = true;
  209. } else if (event->eventType == kevent_stop_sample_cmd_event) {
  210. ZLOGI("stop sample because stop sample event");
  211. stopcapture = true;
  212. } else if (m_sample_duration_ms != 0 && DeviceCtrl_cur_state_haspassed_ms() > m_sample_duration_ms) {
  213. ZLOGI("stop sample because sample timeout");
  214. stopcapture = true;
  215. } else if (BoardBattery_get_battery_level() < APP_LOW_BATTERY_LIMIT) {
  216. ZLOGI("stop sample because low battery");
  217. stopcapture = true;
  218. }
  219. if (stopcapture) {
  220. // �ر��ļ�
  221. SampleDataMgr_close(m_sample_data_fd);
  222. // SD�����ӵ��ⲿtypec
  223. SampleDataMgr_changeToExtMode();
  224. // ֹͣ�ɼ�
  225. hwss_stop_capture();
  226. // ж���ĵ��ɼ�����
  227. hwss_unload();
  228. // �л�������״̬
  229. prvf_change_to_ready_state();
  230. {
  231. static app_event_t event;
  232. event.eventType = kevent_sample_stop_event;
  233. AppEvent_pushEvent(&event);
  234. }
  235. BoardBeepCtrl_setEffect(kBoardBeepEffect_oneShortBeep);
  236. }
  237. }
  238. }
  239. void DeviceCtrl_startSample(int duration_s) {
  240. ZLOGI("start sample %d", duration_s);
  241. m_sample_duration_ms = duration_s * 1000;
  242. if (duration_s == 0) {
  243. m_sample_duration_ms = 2 * 60 * 1000; // ������
  244. }
  245. static app_event_t event;
  246. event.eventType = kevent_start_sample_cmd_event;
  247. AppEvent_pushEvent(&event);
  248. }
  249. void DeviceCtrl_stopSample() {
  250. ZLOGI("stop sample");
  251. static app_event_t event;
  252. event.eventType = kevent_stop_sample_cmd_event;
  253. AppEvent_pushEvent(&event);
  254. }
  255. /*******************************************************************************
  256. * EXTERN *
  257. *******************************************************************************/
  258. void DeviceCtrl_init() {
  259. // ��������ʼ��
  260. BoardBeepCtrl_init();
  261. // ����ָʾ�Ƴ�ʼ��
  262. BoardLight_Init();
  263. // ������ʼ��
  264. BoardButton_Init(board_button_cb);
  265. // ���س�ʼ��
  266. BoardBattery_init();
  267. // SD����ʼ��
  268. SampleDataMgr_init();
  269. // �ĵ��ɼ�������ʼ��
  270. hwss_init();
  271. // BoardLight_blockFlash(3, 100);
  272. BoardBattery_load();
  273. if (BoardBattery_get_battery_level() < APP_LOW_BATTERY_POWER_ON_LIMIT) {
  274. BoardLight_blockFlash(3, 100);
  275. // ϵͳ��������˯��,��������˯��ǰ��ʹ�ܻ�������
  276. BoardButton_enable_sense();
  277. app_timer_stop_all();
  278. sd_power_system_off();
  279. NVIC_SystemReset();
  280. m_ispoweron = false;
  281. }
  282. // ע���¼�����
  283. AppEvent_regListener(app_event_listener);
  284. // ���������¼�
  285. zble_module_reglistener(zble_event_listener);
  286. // �л�������״̬
  287. prvf_change_to_ready_state();
  288. wd_init();
  289. ZERROR_CHECK(app_timer_create(&m_state_machine_driver_tmr, APP_TIMER_MODE_REPEATED, state_machine_driver_tmr_cb));
  290. ZERROR_CHECK(app_timer_start(m_state_machine_driver_tmr, APP_TIMER_TICKS(100), NULL)); // 200HZ����
  291. }
  292. /*******************************************************************************
  293. * UTILS *
  294. *******************************************************************************/
  295. void DeviceCtrl_change_to_state(device_state_t state) {
  296. ZLOGI("change state from %s to %s", ds2str(m_device_state), ds2str(state));
  297. m_device_state = state;
  298. m_change_to_cur_state_tp = znordic_getpower_on_ms();
  299. }
  300. uint32_t DeviceCtrl_cur_state_haspassed_ms() { return znordic_haspassed_ms(m_change_to_cur_state_tp); }
  301. device_state_t DeviceCtrl_now_state() { return m_device_state; }