exception.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*Includes ----------------------------------------------*/
  2. #include "exception.h"
  3. #include "system.h"
  4. #include "app_flash.h"
  5. #include "hal_imu.h"
  6. #include "hal_led.h"
  7. #include "hal_mode_manage.h"
  8. #include "bsp_time.h"
  9. /*Private macro ------------------------------------------------*/
  10. #define EXCEPT_LED_ON_ACC_Z 1500 //加速度Z轴触发异常灯开启
  11. #define EXCEPT_LED_OFF_ACC_Z -1500 //加速度Z轴触发异常灯关闭
  12. #define LED_PROCESS_CYCLE 200 //异常灯线程周期,ms为单位
  13. #define EXCEPT_LED_TOGGLE_TIME 200 //异常灯翻转周期,ms为单位
  14. #define EXCEPT_LED_DISPLAY_TIME 10000 //异常灯显示时长,ms为单位
  15. /*STRUCTION ----------------------------------------------------*/
  16. typedef enum{
  17. EXCEPT_LED_ON, //异常灯 - 开
  18. EXCEPT_LED_OFF, //异常灯 - 关
  19. } EXCEPT_LED_SWITCH_e;
  20. typedef struct exception{
  21. /*private member*/
  22. EXCEPT_LED_SWITCH_e led_switch; //异常灯开关
  23. uint16_t led_display_time; //异常灯显示时长
  24. uint16_t led_toggle_time; //异常灯翻转时长
  25. bool is_change; //更改标志位
  26. } Exception_t;
  27. /*Local Variable ----------------------------------------------*/
  28. static Exception_t ob_exception;
  29. /*Local Functions ----------------------------------------------*/
  30. static void Exception_Led_Process(void);
  31. static void Except_Led_Close(void)
  32. {
  33. //全功率关闭
  34. Process_SetHoldOn(Exception_Led_Process,0);
  35. ob_exception.led_switch = EXCEPT_LED_OFF;
  36. ob_exception.led_display_time = EXCEPT_LED_DISPLAY_TIME/LED_PROCESS_CYCLE;
  37. ob_exception.led_toggle_time = EXCEPT_LED_TOGGLE_TIME/LED_PROCESS_CYCLE;
  38. LED_Stop(LED_EXCEPT);
  39. }
  40. static void Except_Led_OpenOnce(void)
  41. {
  42. //异常灯显示/关闭
  43. if(ob_exception.led_display_time != 0)
  44. {
  45. //全功率开启
  46. Process_SetHoldOn(Exception_Led_Process,1);
  47. if(ob_exception.led_toggle_time == 0)
  48. {
  49. if(ob_exception.led_switch == EXCEPT_LED_ON)ob_exception.led_switch = EXCEPT_LED_OFF;
  50. else ob_exception.led_switch = EXCEPT_LED_ON;
  51. ob_exception.led_toggle_time = EXCEPT_LED_TOGGLE_TIME/LED_PROCESS_CYCLE;
  52. }
  53. else
  54. {
  55. if(ob_exception.led_switch == EXCEPT_LED_ON)LED_Start(LED_EXCEPT,COLOR_ORANGE);
  56. if(ob_exception.led_switch == EXCEPT_LED_OFF)LED_Start(LED_EXCEPT,COLOR_BLACK);
  57. ob_exception.led_toggle_time--;
  58. }
  59. ob_exception.led_display_time--;
  60. }
  61. }
  62. static void Exception_Led_Process(void)
  63. {
  64. int16_t f_acc[3];
  65. //读取ACC值
  66. if(IMU_Get_Front_Data_Num() >= 1)IMU_Get_Front_Data(IMU_Get_Front_Data_Num()-1, NULL, f_acc, NULL, NULL);
  67. //把鞋子倒转平放,且存在异常
  68. if(f_acc[2] >= EXCEPT_LED_ON_ACC_Z && mFlash.exception.except_number_of_exist > 0)
  69. {
  70. Except_Led_OpenOnce();
  71. }//把鞋子正放
  72. else if(f_acc[2] <= EXCEPT_LED_OFF_ACC_Z)
  73. {
  74. Except_Led_Close();
  75. }
  76. }
  77. /*API ----------------------------------------------*/
  78. void Exception_Init(void)
  79. {
  80. ob_exception.led_switch = EXCEPT_LED_OFF;
  81. ob_exception.led_display_time = EXCEPT_LED_DISPLAY_TIME/LED_PROCESS_CYCLE;
  82. ob_exception.led_toggle_time = EXCEPT_LED_TOGGLE_TIME/LED_PROCESS_CYCLE;
  83. ob_exception.is_change = false;
  84. Process_Start(LED_PROCESS_CYCLE,"Exception_Led_Process",Exception_Led_Process);
  85. }
  86. void Except_SetExceptype(ExcepType_t excep_type)
  87. {
  88. switch(excep_type)
  89. {
  90. case EXCEPT_DATA_BACK_MAG:
  91. if(mFlash.exception.except_data_back_mag != 1){
  92. mFlash.exception.except_data_back_mag = 1;
  93. mFlash.exception.except_number_of_exist++;
  94. ob_exception.is_change = true;
  95. }
  96. break;
  97. case EXCEPT_DATA_FRONT_ACC:
  98. if(mFlash.exception.except_data_front_acc != 1){
  99. mFlash.exception.except_data_front_acc = 1;
  100. mFlash.exception.except_number_of_exist++;
  101. ob_exception.is_change = true;
  102. }
  103. break;
  104. case EXCEPT_DATA_FRONT_GRY:
  105. if(mFlash.exception.except_data_front_gry != 1){
  106. mFlash.exception.except_data_front_gry = 1;
  107. mFlash.exception.except_number_of_exist++;
  108. ob_exception.is_change = true;
  109. }
  110. break;
  111. case EXCEPT_DATA_FRONT_MAG:
  112. if(mFlash.exception.except_data_front_mag != 1){
  113. mFlash.exception.except_data_front_mag = 1;
  114. mFlash.exception.except_number_of_exist++;
  115. ob_exception.is_change = true;
  116. }
  117. break;
  118. case EXCEPT_DATA_CHARGE:
  119. if(mFlash.exception.except_data_charge != 1){
  120. mFlash.exception.except_data_charge = 1;
  121. mFlash.exception.except_number_of_exist++;
  122. ob_exception.is_change = true;
  123. }
  124. break;
  125. case EXCEPT_DATA_BATTERY:
  126. if(mFlash.exception.except_data_battery != 1){
  127. mFlash.exception.except_data_battery = 1;
  128. mFlash.exception.except_number_of_exist++;
  129. ob_exception.is_change = true;
  130. }
  131. break;
  132. case EXCEPT_MODE_SUSPEND_OVERFLOW:
  133. if(mFlash.exception.except_mode_suspend_overflow != 1){
  134. mFlash.exception.except_mode_suspend_overflow = 1;
  135. mFlash.exception.except_number_of_exist++;
  136. ob_exception.is_change = true;
  137. }
  138. break;
  139. case EXCEPT_ANT_DAMAGE:
  140. if(mFlash.exception.except_ant_damage != 1){
  141. mFlash.exception.except_ant_damage = 1;
  142. mFlash.exception.except_number_of_exist++;
  143. ob_exception.is_change = true;
  144. }
  145. break;
  146. }
  147. }
  148. void Except_ClearExceptype(ExcepType_t excep_type)
  149. {
  150. switch(excep_type)
  151. {
  152. case EXCEPT_DATA_BACK_MAG:
  153. if(mFlash.exception.except_data_back_mag == 1){
  154. mFlash.exception.except_data_back_mag = 0;
  155. if(mFlash.exception.except_number_of_exist != 0)mFlash.exception.except_number_of_exist--;
  156. ob_exception.is_change = true;
  157. }
  158. break;
  159. case EXCEPT_DATA_FRONT_ACC:
  160. if(mFlash.exception.except_data_front_acc == 1){
  161. mFlash.exception.except_data_front_acc = 0;
  162. if(mFlash.exception.except_number_of_exist != 0)mFlash.exception.except_number_of_exist--;
  163. ob_exception.is_change = true;
  164. }
  165. break;
  166. case EXCEPT_DATA_FRONT_GRY:
  167. if(mFlash.exception.except_data_front_gry == 1){
  168. mFlash.exception.except_data_front_gry = 0;
  169. if(mFlash.exception.except_number_of_exist != 0)mFlash.exception.except_number_of_exist--;
  170. ob_exception.is_change = true;
  171. }
  172. break;
  173. case EXCEPT_DATA_FRONT_MAG:
  174. if(mFlash.exception.except_data_front_mag == 1){
  175. mFlash.exception.except_data_front_mag = 0;
  176. if(mFlash.exception.except_number_of_exist != 0)mFlash.exception.except_number_of_exist--;
  177. ob_exception.is_change = true;
  178. }
  179. break;
  180. case EXCEPT_DATA_CHARGE:
  181. if(mFlash.exception.except_data_charge == 1){
  182. mFlash.exception.except_data_charge = 0;
  183. if(mFlash.exception.except_number_of_exist != 0)mFlash.exception.except_number_of_exist--;
  184. ob_exception.is_change = true;
  185. }
  186. break;
  187. case EXCEPT_DATA_BATTERY:
  188. if(mFlash.exception.except_data_battery == 1){
  189. mFlash.exception.except_data_battery = 0;
  190. if(mFlash.exception.except_number_of_exist != 0)mFlash.exception.except_number_of_exist--;
  191. ob_exception.is_change = true;
  192. }
  193. break;
  194. case EXCEPT_MODE_SUSPEND_OVERFLOW:
  195. if(mFlash.exception.except_mode_suspend_overflow == 1){
  196. mFlash.exception.except_mode_suspend_overflow = 0;
  197. if(mFlash.exception.except_number_of_exist != 0)mFlash.exception.except_number_of_exist--;
  198. ob_exception.is_change = true;
  199. }
  200. break;
  201. case EXCEPT_ANT_DAMAGE:
  202. if(mFlash.exception.except_ant_damage == 1){
  203. mFlash.exception.except_ant_damage = 0;
  204. if(mFlash.exception.except_number_of_exist != 0)mFlash.exception.except_number_of_exist--;
  205. ob_exception.is_change = true;
  206. }
  207. break;
  208. }
  209. }
  210. bool Except_SaveExceptype(void)
  211. {
  212. if(ob_exception.is_change)
  213. {
  214. if(Flash_SaveInfomation() != ZONE_OP_SUCCESS)
  215. {
  216. return false;
  217. }
  218. ob_exception.is_change = false;
  219. }
  220. return true;
  221. }
  222. bool Except_IsError(ExcepType_t excep_type)
  223. {
  224. switch(excep_type)
  225. {
  226. case EXCEPT_DATA_BACK_MAG:
  227. if(mFlash.exception.except_data_back_mag == 1){
  228. return true;
  229. }
  230. break;
  231. case EXCEPT_DATA_FRONT_ACC:
  232. if(mFlash.exception.except_data_front_acc == 1){
  233. return true;
  234. }
  235. break;
  236. case EXCEPT_DATA_FRONT_GRY:
  237. if(mFlash.exception.except_data_front_gry == 1){
  238. return true;
  239. }
  240. break;
  241. case EXCEPT_DATA_FRONT_MAG:
  242. if(mFlash.exception.except_data_front_mag == 1){
  243. return true;
  244. }
  245. break;
  246. case EXCEPT_DATA_CHARGE:
  247. if(mFlash.exception.except_data_charge == 1){
  248. return true;
  249. }
  250. break;
  251. case EXCEPT_DATA_BATTERY:
  252. if(mFlash.exception.except_data_battery == 1){
  253. return true;
  254. }
  255. break;
  256. case EXCEPT_MODE_SUSPEND_OVERFLOW:
  257. if(mFlash.exception.except_mode_suspend_overflow == 1){
  258. return true;
  259. }
  260. break;
  261. case EXCEPT_ANT_DAMAGE:
  262. if(mFlash.exception.except_ant_damage == 1){
  263. return true;
  264. }
  265. break;
  266. }
  267. return false;
  268. }
  269. bool Except_IsErrorExist(void)
  270. {
  271. if(mFlash.exception.except_number_of_exist > 0)return true;
  272. return false;
  273. }