exception.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. } 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. }
  38. static void Except_Led_OpenOnce(void)
  39. {
  40. //异常灯显示/关闭
  41. if(ob_exception.led_display_time != 0)
  42. {
  43. //全功率开启
  44. Process_SetHoldOn(Exception_Led_Process,1);
  45. if(ob_exception.led_toggle_time == 0)
  46. {
  47. if(ob_exception.led_switch == EXCEPT_LED_ON)ob_exception.led_switch = EXCEPT_LED_OFF;
  48. else ob_exception.led_switch = EXCEPT_LED_ON;
  49. ob_exception.led_toggle_time = EXCEPT_LED_TOGGLE_TIME/LED_PROCESS_CYCLE;
  50. }
  51. else
  52. {
  53. if(ob_exception.led_switch == EXCEPT_LED_ON)LED_Start(LED_EXCEPT,COLOR_ORANGE);
  54. if(ob_exception.led_switch == EXCEPT_LED_OFF)LED_Start(LED_EXCEPT,COLOR_BLACK);
  55. ob_exception.led_toggle_time--;
  56. }
  57. ob_exception.led_display_time--;
  58. }
  59. }
  60. static void Exception_Led_Process(void)
  61. {
  62. int16_t f_acc[3];
  63. //读取ACC值
  64. if(IMU_Get_Front_Data_Num() >= 1)IMU_Get_Front_Data(IMU_Get_Front_Data_Num()-1, NULL, f_acc, NULL, NULL);
  65. //把鞋子倒转平放,且存在异常
  66. if(f_acc[2] >= EXCEPT_LED_ON_ACC_Z && mFlash.exception.except_number_of_exist > 0)
  67. {
  68. Except_Led_OpenOnce();
  69. }//把鞋子正放
  70. else if(f_acc[2] <= EXCEPT_LED_OFF_ACC_Z)
  71. {
  72. Except_Led_Close();
  73. }
  74. }
  75. /*API ----------------------------------------------*/
  76. void Exception_Init(void)
  77. {
  78. ob_exception.led_switch = EXCEPT_LED_OFF;
  79. ob_exception.led_display_time = EXCEPT_LED_DISPLAY_TIME/LED_PROCESS_CYCLE;
  80. ob_exception.led_toggle_time = EXCEPT_LED_TOGGLE_TIME/LED_PROCESS_CYCLE;
  81. Process_Start(LED_PROCESS_CYCLE,"Exception_Led_Process",Exception_Led_Process);
  82. }
  83. void Except_SaveExceptype(ExcepType_t excep_type)
  84. {
  85. switch(excep_type)
  86. {
  87. case EXCEPT_DATA_BACK_MAG:
  88. if(mFlash.exception.except_data_back_mag != 1){
  89. mFlash.exception.except_data_back_mag = 1;
  90. mFlash.exception.except_number_of_exist++;
  91. Flash_SaveInfomation();
  92. }
  93. break;
  94. case EXCEPT_DATA_FRONT_ACC:
  95. if(mFlash.exception.except_data_front_acc != 1){
  96. mFlash.exception.except_data_front_acc = 1;
  97. mFlash.exception.except_number_of_exist++;
  98. Flash_SaveInfomation();
  99. }
  100. break;
  101. case EXCEPT_DATA_FRONT_GRY:
  102. if(mFlash.exception.except_data_front_gry != 1){
  103. mFlash.exception.except_data_front_gry = 1;
  104. mFlash.exception.except_number_of_exist++;
  105. Flash_SaveInfomation();
  106. }
  107. break;
  108. case EXCEPT_DATA_FRONT_MAG:
  109. if(mFlash.exception.except_data_front_mag != 1){
  110. mFlash.exception.except_data_front_mag = 1;
  111. mFlash.exception.except_number_of_exist++;
  112. Flash_SaveInfomation();
  113. }
  114. break;
  115. case EXCEPT_DATA_CHARGE:
  116. if(mFlash.exception.except_data_charge != 1){
  117. mFlash.exception.except_data_charge = 1;
  118. mFlash.exception.except_number_of_exist++;
  119. Flash_SaveInfomation();
  120. }
  121. break;
  122. case EXCEPT_DATA_BATTERY:
  123. if(mFlash.exception.except_data_battery != 1){
  124. mFlash.exception.except_data_battery = 1;
  125. mFlash.exception.except_number_of_exist++;
  126. Flash_SaveInfomation();
  127. }
  128. break;
  129. case EXCEPT_MODE_SUSPEND_OVERFLOW:
  130. if(mFlash.exception.except_mode_suspend_overflow != 1){
  131. mFlash.exception.except_mode_suspend_overflow = 1;
  132. mFlash.exception.except_number_of_exist++;
  133. Flash_SaveInfomation();
  134. }
  135. break;
  136. default:
  137. break;
  138. }
  139. }
  140. uint32_t Except_IsError(ExcepType_t excep_type)
  141. {
  142. switch(excep_type)
  143. {
  144. case EXCEPT_DATA_BACK_MAG:
  145. if(mFlash.exception.except_data_back_mag == 1){
  146. return 1;
  147. }
  148. break;
  149. case EXCEPT_DATA_FRONT_ACC:
  150. if(mFlash.exception.except_data_front_acc == 1){
  151. return 1;
  152. }
  153. break;
  154. case EXCEPT_DATA_FRONT_GRY:
  155. if(mFlash.exception.except_data_front_gry == 1){
  156. return 1;
  157. }
  158. break;
  159. case EXCEPT_DATA_FRONT_MAG:
  160. if(mFlash.exception.except_data_front_mag == 1){
  161. return 1;
  162. }
  163. break;
  164. case EXCEPT_DATA_CHARGE:
  165. if(mFlash.exception.except_data_charge == 1){
  166. return 1;
  167. }
  168. break;
  169. case EXCEPT_DATA_BATTERY:
  170. if(mFlash.exception.except_data_battery == 1){
  171. return 1;
  172. }
  173. break;
  174. case EXCEPT_MODE_SUSPEND_OVERFLOW:
  175. if(mFlash.exception.except_mode_suspend_overflow == 1){
  176. return 1;
  177. }
  178. break;
  179. default:
  180. break;
  181. }
  182. return 0;
  183. }
  184. bool Except_IsErrorExist(void)
  185. {
  186. if(mFlash.exception.except_number_of_exist > 0)return true;
  187. return false;
  188. }