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.

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