app_overturn.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #include "usr_config.h"
  2. #include "hal_battery.h"
  3. #include "nrf_drv_saadc.h"
  4. #include "bsp_time.h"
  5. #include "exception.h"
  6. #include "system.h"
  7. #include "hal_led.h"
  8. #include "app_overturn.h"
  9. #include "hal_wearshoes.h"
  10. #include "bll_imu.h"
  11. #include "app_flash.h"
  12. #include "ble_comm.h"
  13. #include "hal_qma.h"
  14. #include "hal_ble_client.h"
  15. #define SLIDE_WINDOW_LEN 5
  16. typedef struct
  17. {
  18. int16_t max_temp;
  19. uint16_t time_count;
  20. }ACC_TIME_TYPE;
  21. typedef struct
  22. {
  23. ACC_TIME_TYPE slide_window[SLIDE_WINDOW_LEN];
  24. int window_length;
  25. }SLIDE_WINDOW;
  26. int cal_unsigned_D_value(uint16_t left_value, uint16_t right_value)
  27. {
  28. if(left_value <= right_value)
  29. {
  30. return right_value - left_value;
  31. }
  32. else
  33. {
  34. return 65535-left_value + 1 + right_value;
  35. }
  36. }
  37. static uint8_t flag_move =0;
  38. static uint8_t AUTHEN_VAUL =2;
  39. static BLE_Client_Tx_t m_Client_BLE_AUTHEN = {
  40. .n = 3,
  41. .t = 300,
  42. };
  43. void sort_silde_window(SLIDE_WINDOW *max_slide_window, SLIDE_WINDOW *min_slide_window, int16_t acc_val, uint16_t time_count)
  44. {
  45. ACC_TIME_TYPE acc_time_type_temp = {acc_val, time_count};
  46. //检测当前的值是是否是最大值
  47. if(max_slide_window->window_length == 0)
  48. {
  49. max_slide_window->slide_window[0] = acc_time_type_temp;
  50. max_slide_window->window_length = 1;
  51. }
  52. else
  53. {
  54. //先判断长度是否溢出来了
  55. while(cal_unsigned_D_value(max_slide_window->slide_window[0].time_count, time_count)>= SLIDE_WINDOW_LEN)
  56. {
  57. memcpy(max_slide_window->slide_window, max_slide_window->slide_window + 1, (SLIDE_WINDOW_LEN - 1) *sizeof(ACC_TIME_TYPE));
  58. max_slide_window->window_length --;
  59. }
  60. //再检测新的元素需要前面的元素小
  61. while(max_slide_window->window_length > 0)
  62. {
  63. if(max_slide_window->slide_window[max_slide_window->window_length - 1].max_temp < acc_val)
  64. {
  65. max_slide_window->window_length --;
  66. }
  67. else
  68. {
  69. break;
  70. }
  71. }
  72. max_slide_window->slide_window[max_slide_window->window_length] = acc_time_type_temp;
  73. max_slide_window->window_length ++;
  74. }
  75. //检测当前的值是是否是最大值
  76. if(min_slide_window->window_length == 0)
  77. {
  78. min_slide_window->slide_window[0] = acc_time_type_temp;
  79. min_slide_window->window_length = 1;
  80. }
  81. else
  82. {
  83. //先判断长度是否溢出来了
  84. while(cal_unsigned_D_value(min_slide_window->slide_window[0].time_count, time_count)>= SLIDE_WINDOW_LEN)
  85. {
  86. memcpy(min_slide_window->slide_window, min_slide_window->slide_window + 1, (SLIDE_WINDOW_LEN - 1) *sizeof(ACC_TIME_TYPE));
  87. min_slide_window->window_length --;
  88. }
  89. //再检测新的元素需要前面的元素小
  90. while(min_slide_window->window_length > 0)
  91. {
  92. if(min_slide_window->slide_window[min_slide_window->window_length - 1].max_temp > acc_val)
  93. {
  94. min_slide_window->window_length --;
  95. }
  96. else
  97. {
  98. break;
  99. }
  100. }
  101. min_slide_window->slide_window[min_slide_window->window_length] = acc_time_type_temp;
  102. min_slide_window->window_length ++;
  103. }
  104. }
  105. /************************ 踮脚显示电量 ***********************************/
  106. //没穿鞋的情况下
  107. void app_BatDispaly_Process_N(void)
  108. {
  109. bll_imu_data_t data= {0};
  110. static uint8_t lED_LIFE_SIGNAL = 0;
  111. static uint8_t lED_LIFE_SIGNAL_2 = 0;
  112. static uint8_t lED_LIFE_TIME = 0;
  113. // DEBUG_LOG("app_BatDispaly_Process_N;\n");
  114. if(hal_wearshoes_is_wearshoes())return;
  115. //获取最新一组前脚加速度
  116. int16_t data_size = 0;
  117. data_size = bll_imu_get_data_num(BLL_IMU_DIR_FRONT);
  118. if(data_size > 0){
  119. bll_imu_get_data(BLL_IMU_DIR_FRONT, data_size -1 , &data);
  120. int32_t acc_norm = ((int32_t)data.acc[0] * (int32_t)data.acc[0] +(int32_t)data.acc[1] * (int32_t)data.acc[1] + (int32_t)data.acc[2] * (int32_t)data.acc[2]);
  121. //
  122. // DEBUG_LOG("data.acc[0] : %d, data.acc[1] : %d, data.acc[2] : %d ;\n", data.acc[0], data.acc[1], data.acc[2]);
  123. if(abs(acc_norm - 4194304) < 362144 && (data.acc[2] > 1536))
  124. {
  125. lED_LIFE_SIGNAL = 3;
  126. }
  127. else if(lED_LIFE_SIGNAL > 0)lED_LIFE_SIGNAL --;
  128. if(abs(acc_norm - 4194304) < 362144 && (data.acc[0] <-900))
  129. {
  130. lED_LIFE_SIGNAL_2 = 3;
  131. }
  132. else if(lED_LIFE_SIGNAL_2 > 0)lED_LIFE_SIGNAL_2 --;
  133. if(abs(acc_norm - 4194304) < 362144 && data.acc[2] < -1024)
  134. {
  135. lED_LIFE_SIGNAL = 0;
  136. }
  137. if(abs(acc_norm - 4194304) < 362144 && abs(data.acc[0]) < 400)
  138. {
  139. lED_LIFE_SIGNAL_2 = 0;
  140. }
  141. // DEBUG_LOG("acc_norm:%d,lED_LIFE_SIGNAL:%d,data.acc2:%d,lED_LIFE_TIME:%d\n",abs(acc_norm - 4194304),lED_LIFE_SIGNAL,data.acc[2],lED_LIFE_TIME);
  142. if(lED_LIFE_SIGNAL > 0 || lED_LIFE_SIGNAL_2 > 0)
  143. {
  144. // DEBUG_LOG("unwearshoes LED_Start(LED_OVERTURN,COLOR_GREEN);;\n");
  145. if(lED_LIFE_TIME == 0){
  146. lED_LIFE_TIME = (10000/StandByPower_Interval);
  147. if(GetBatteryPersent()>20){
  148. LED_Start(LED_OVERTURN,COLOR_GREEN);
  149. }else{
  150. LED_Start(LED_OVERTURN,COLOR_ORANGE);
  151. }
  152. Process_SetHoldOn(app_BatDispaly_Process_N,1);
  153. if(mFlash.isHost)BLE_Client_Tx_Send(&m_Client_BLE_AUTHEN,BLE_AUTHEN,&AUTHEN_VAUL,1);
  154. }
  155. else if (2 == lED_LIFE_TIME){
  156. LED_Stop(LED_OVERTURN);
  157. Process_SetHoldOn(app_BatDispaly_Process_N,0);
  158. }
  159. if (lED_LIFE_TIME > 1)lED_LIFE_TIME--;
  160. }
  161. else
  162. {
  163. // DEBUG_LOG("unwearshoes LED_Stop(LED_OVERTURN);\n");
  164. if(0 != lED_LIFE_TIME ){lED_LIFE_TIME =0;
  165. LED_Stop(LED_OVERTURN);
  166. Process_SetHoldOn(app_BatDispaly_Process_N,0);
  167. }
  168. if(1 == flag_move){flag_move =0;
  169. LED_Stop(LED_OVERTURN);
  170. Process_SetHoldOn(app_BatDispaly_Process_N,0);
  171. }
  172. }
  173. }
  174. }
  175. //穿鞋的情况下
  176. void app_BatDispaly_Process(void)
  177. {
  178. bll_imu_data_t data={0};
  179. // qma_data_t qma_data={0};
  180. static uint16_t time_count = 0;
  181. static uint16_t station_count =0;
  182. static uint16_t overturn_180_count = 0;
  183. static uint8_t lED_LIFE_SIGNAL = 0;
  184. static uint8_t lED_LIFE_TIME = 0;
  185. static SLIDE_WINDOW max_slide_window = { {-3096,0},0 };
  186. static SLIDE_WINDOW min_slide_window = { {3096,0},0 };
  187. // DEBUG_LOG("app_BatDispaly_Process;\n");
  188. if(!hal_wearshoes_is_wearshoes())return;
  189. // DEBUG_LOG("app_BatDispaly_Process;\n");
  190. //获取最新一组前脚加速度, 有fifo
  191. int16_t data_size = 0;
  192. data_size = bll_imu_get_data_num(BLL_IMU_DIR_FRONT);
  193. if(data_size > 0){
  194. bll_imu_get_data(BLL_IMU_DIR_FRONT, data_size-1 , &data);
  195. //耗时较少的计算滑动窗口的最大值、最小值
  196. sort_silde_window(&max_slide_window, &min_slide_window, data.acc[2], time_count);
  197. if(time_count > SLIDE_WINDOW_LEN)
  198. {
  199. if(data.acc[2] - min_slide_window.slide_window[0].max_temp < 256 && max_slide_window.slide_window[0].max_temp - data.acc[2] < 256
  200. && max_slide_window.slide_window[0].max_temp - min_slide_window.slide_window[0].max_temp < 256 && data.acc[0] < -600)
  201. {
  202. station_count++;
  203. }
  204. else station_count =0;
  205. }
  206. time_count++;
  207. //在穿鞋状态下,被翻转了
  208. int32_t acc_norm = ((int32_t)data.acc[0] * (int32_t)data.acc[0] +(int32_t)data.acc[1] * (int32_t)data.acc[1] + (int32_t)data.acc[2] * (int32_t)data.acc[2]);
  209. if(abs(acc_norm - 4194304) < 362144 && data.acc[2] > 1536)
  210. //if(abs(acc_norm - 16777216) < 1448576 && data.acc[2] > 3072)
  211. {
  212. //DEBUG_LOG(" wear shoes if(abs(acc_norm - 4194304) < 262144 && data.acc[2] > 1536);\n");
  213. overturn_180_count ++;
  214. }
  215. else
  216. {
  217. overturn_180_count = 0;
  218. }
  219. if(overturn_180_count >= 5 || station_count > 4)
  220. {
  221. // DEBUG_LOG(" overturn_180_count > 5;\n");
  222. lED_LIFE_SIGNAL = 6;
  223. }
  224. if(lED_LIFE_SIGNAL > 0)lED_LIFE_SIGNAL --;
  225. if(lED_LIFE_SIGNAL > 0)
  226. {
  227. // DEBUG_LOG("wearshoes LED_Start(LED_OVERTURN,COLOR_GREEN);;\n");
  228. if(lED_LIFE_TIME == 0){
  229. lED_LIFE_TIME = (10000/LowPower_Interval);
  230. if(GetBatteryPersent()>20){
  231. LED_Start(LED_OVERTURN,COLOR_GREEN);
  232. }else{
  233. LED_Start(LED_OVERTURN,COLOR_ORANGE);
  234. }
  235. Process_SetHoldOn(app_BatDispaly_Process,1);
  236. if(mFlash.isHost)BLE_Client_Tx_Send(&m_Client_BLE_AUTHEN,BLE_AUTHEN,&AUTHEN_VAUL,1);
  237. }
  238. else if (2 == lED_LIFE_TIME){
  239. LED_Stop(LED_OVERTURN);
  240. Process_SetHoldOn(app_BatDispaly_Process,0);
  241. }
  242. if (lED_LIFE_TIME > 1)lED_LIFE_TIME--;
  243. }
  244. else
  245. {
  246. // DEBUG_LOG("wearshoes LED_Stop(LED_OVERTURN);\n");
  247. if(0 != lED_LIFE_TIME ){lED_LIFE_TIME =0;
  248. LED_Stop(LED_OVERTURN);
  249. Process_SetHoldOn(app_BatDispaly_Process,0);
  250. }
  251. if(0 == flag_move){flag_move =1;
  252. LED_Stop(LED_OVERTURN);
  253. Process_SetHoldOn(app_BatDispaly_Process_N,0);
  254. }
  255. }
  256. }
  257. }
  258. void app_overturn_Init(void)
  259. {
  260. Process_Start(LowPower_Interval,"BatDispaly",app_BatDispaly_Process);
  261. Process_Start(StandByPower_Interval,"BatDispaly_N",app_BatDispaly_Process_N);
  262. }