exception.c 8.6 KB

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