hal_mode_manage.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #include "hal_mode_manage.h"
  2. #include "hal_imu.h"
  3. #include "usr_config.h"
  4. #include "bsp_time.h"
  5. #include "system.h"
  6. #include "hal_wearshoes.h"
  7. /*********************************************************************
  8. * LOCAL VARIABLES
  9. */
  10. static hal_mode cur_mode;
  11. static hal_mode change_mode;
  12. static hal_mode process_mode;
  13. static uint8_t send_signal_flag = 0;
  14. static uint8_t critical_zone = 0;
  15. static void hal_mode_manage_Process(void);
  16. /*********************************************************************
  17. * LOCAL FUCTIONS
  18. */
  19. static void Send_Signal(hal_mode mode)
  20. {
  21. switch(mode)
  22. {
  23. case HAL_MODE_GAME:
  24. if(IMU_GetCurrentMode() != STATE_FULL_POWER_MODE){
  25. IMU_SetSignal(SIGNAL_FULL_POWER, IMU_SIGNAL_ON);
  26. send_signal_flag = 1;
  27. cur_mode = HAL_MODE_IN_CONFIG;
  28. Process_SetHoldOn(hal_mode_manage_Process,1);
  29. }else{
  30. critical_zone = 0; //解锁
  31. cur_mode = HAL_MODE_GAME;
  32. }
  33. break;
  34. case HAL_MODE_REALSTEP:
  35. if(IMU_GetCurrentMode() != STATE_FULL_POWER_MODE){
  36. IMU_SetSignal(SIGNAL_FULL_POWER, IMU_SIGNAL_ON);
  37. send_signal_flag = 1;
  38. cur_mode = HAL_MODE_IN_CONFIG;
  39. Process_SetHoldOn(hal_mode_manage_Process,1);
  40. }else{
  41. critical_zone = 0; //解锁
  42. cur_mode = HAL_MODE_REALSTEP;
  43. }
  44. break;
  45. case HAL_MODE_STANDBY:
  46. if(IMU_GetCurrentMode() != STATE_STANDBY_POWER_MODE){
  47. IMU_SetSignal(SIGNAL_STANDBY_POWER, IMU_SIGNAL_ON);
  48. send_signal_flag = 1;
  49. cur_mode = HAL_MODE_IN_CONFIG;
  50. Process_SetHoldOn(hal_mode_manage_Process,1);
  51. }else{
  52. critical_zone = 0; //解锁
  53. cur_mode = HAL_MODE_STANDBY;
  54. }
  55. break;
  56. case HAL_MODE_NORMAL:
  57. if(IMU_GetCurrentMode() != STATE_LOW_POWER_MODE){
  58. IMU_SetSignal(SIGNAL_LOW_POWER, IMU_SIGNAL_ON);
  59. send_signal_flag = 1;
  60. cur_mode = HAL_MODE_IN_CONFIG;
  61. Process_SetHoldOn(hal_mode_manage_Process,1);
  62. }else{
  63. critical_zone = 0; //解锁
  64. cur_mode = HAL_MODE_NORMAL;
  65. }
  66. break;
  67. case HAL_MODE_SELF_CHECK:
  68. if(IMU_GetCurrentMode() != STATE_SELF_CHECK_MODE){
  69. IMU_SetSignal(SIGNAL_SELF_CHECK, IMU_SIGNAL_ON);
  70. send_signal_flag = 1;
  71. cur_mode = HAL_MODE_IN_CONFIG;
  72. Process_SetHoldOn(hal_mode_manage_Process,1);
  73. }else{
  74. critical_zone = 0; //解锁
  75. cur_mode = HAL_MODE_SELF_CHECK;
  76. }
  77. break;
  78. default:
  79. break;
  80. }
  81. }
  82. static void Change_Mode(hal_mode mode)
  83. {
  84. switch(mode)
  85. {
  86. case HAL_MODE_GAME:
  87. if(IMU_GetCurrentMode() != STATE_FULL_POWER_MODE){ //配置失败,进入异常挂起模式
  88. cur_mode = HAL_MODE_EXCEPT_SUSPEND;
  89. send_signal_flag = 0;
  90. critical_zone = 0; //解锁
  91. }else if(IMU_Get_Front_Data_Num() >= 1 && IMU_Get_Front_Data_Num() < 10){
  92. cur_mode = HAL_MODE_GAME;
  93. send_signal_flag = 0;
  94. critical_zone = 0; //解锁
  95. }
  96. Process_SetHoldOn(hal_mode_manage_Process,0);
  97. break;
  98. case HAL_MODE_REALSTEP:
  99. if(IMU_GetCurrentMode() != STATE_FULL_POWER_MODE){
  100. cur_mode = HAL_MODE_EXCEPT_SUSPEND;
  101. send_signal_flag = 0;
  102. critical_zone = 0; //解锁
  103. }else if(IMU_Get_Front_Data_Num() >= 1 && IMU_Get_Front_Data_Num() < 10){
  104. cur_mode = HAL_MODE_REALSTEP;
  105. send_signal_flag = 0;
  106. critical_zone = 0; //解锁
  107. }
  108. Process_SetHoldOn(hal_mode_manage_Process,0);
  109. break;
  110. case HAL_MODE_STANDBY:
  111. if(IMU_GetCurrentMode() != STATE_STANDBY_POWER_MODE){
  112. cur_mode = HAL_MODE_EXCEPT_SUSPEND;
  113. send_signal_flag = 0;
  114. critical_zone = 0; //解锁
  115. }else if(IMU_Get_Front_Data_Num() >= 1 && IMU_Get_Front_Data_Num() < 10){
  116. cur_mode = HAL_MODE_STANDBY;
  117. send_signal_flag = 0;
  118. critical_zone = 0; //解锁
  119. }
  120. Process_SetHoldOn(hal_mode_manage_Process,0);
  121. break;
  122. case HAL_MODE_NORMAL:
  123. if(IMU_GetCurrentMode() != STATE_LOW_POWER_MODE){
  124. cur_mode = HAL_MODE_EXCEPT_SUSPEND;
  125. send_signal_flag = 0;
  126. critical_zone = 0; //解锁
  127. }else if(IMU_Get_Front_Data_Num() >= 10 && IMU_Get_Front_Data_Num() < 30){
  128. cur_mode = HAL_MODE_NORMAL;
  129. send_signal_flag = 0;
  130. critical_zone = 0; //解锁
  131. }
  132. Process_SetHoldOn(hal_mode_manage_Process,0);
  133. break;
  134. case HAL_MODE_SELF_CHECK:
  135. if(IMU_GetCurrentMode() != STATE_SELF_CHECK_MODE){
  136. cur_mode = HAL_MODE_EXCEPT_SUSPEND;
  137. send_signal_flag = 0;
  138. critical_zone = 0; //解锁
  139. }else if(IMU_Get_Front_Data_Num() >= 1 && IMU_Get_Front_Data_Num() < 10){
  140. cur_mode = HAL_MODE_SELF_CHECK;
  141. send_signal_flag = 0;
  142. critical_zone = 0; //解锁
  143. }
  144. Process_SetHoldOn(hal_mode_manage_Process,0);
  145. break;
  146. default:
  147. break;
  148. }
  149. }
  150. //转换规则
  151. static void hal_mode_manage_Discipline(hal_mode *source_mode, hal_mode target_mode)
  152. {
  153. switch(*source_mode)
  154. {
  155. case HAL_MODE_EXCEPT_SUSPEND:
  156. *source_mode = target_mode; //异常挂起模式可转为任意模式
  157. break;
  158. case HAL_MODE_STANDBY:
  159. *source_mode = target_mode; //待机模式可转为任意模式
  160. break;
  161. case HAL_MODE_NORMAL:
  162. *source_mode = target_mode; //正常模式可转为任意模式
  163. break;
  164. case HAL_MODE_SELF_CHECK:
  165. if(target_mode == HAL_MODE_NORMAL){
  166. *source_mode = target_mode; //自检模式只能转为正常模式
  167. }
  168. break;
  169. case HAL_MODE_GAME:
  170. if(target_mode == HAL_MODE_NORMAL || target_mode == HAL_MODE_REALSTEP){
  171. *source_mode = target_mode; //游戏模式只能转为正常和实时计步模式
  172. }
  173. break;
  174. case HAL_MODE_REALSTEP:
  175. if(target_mode == HAL_MODE_NORMAL || target_mode == HAL_MODE_GAME){
  176. *source_mode = target_mode; //实时计步模式只能转为正常和游戏模式
  177. }
  178. break;
  179. default:
  180. break;
  181. }
  182. }
  183. static void hal_mode_manage_Process(void)
  184. {
  185. //锁上期间,不更新 process_mode
  186. if(!critical_zone){
  187. hal_mode_manage_Discipline(&process_mode, change_mode);
  188. critical_zone = 1; //锁上
  189. }
  190. //没有信号的时候处理
  191. if(IMU_IsNoSignal()){
  192. if(!send_signal_flag){ //发送信号
  193. Send_Signal(process_mode);
  194. }else{ //配置判断
  195. Change_Mode(process_mode);
  196. }
  197. }
  198. }
  199. /********************** 外部接口声明区 *************************/
  200. void hal_mode_set(hal_mode mode)
  201. {
  202. hal_mode_manage_Discipline(&change_mode, mode);
  203. }
  204. hal_mode hal_mode_get(void)
  205. {
  206. return cur_mode;
  207. }
  208. static void hal_mode_manage_test_Process(void)
  209. {
  210. SEGGER_RTT_printf(0,"IMU_IsNoSignal():%d change_mode=%d cur_mode=%d process_mode=%d critical_zone:%d IMU_GetCurrentMode():%d IMU_Get_Front_Data_Num():%d send_signal_flag:%d\r\n",IMU_IsNoSignal(),change_mode,cur_mode,process_mode,critical_zone,IMU_GetCurrentMode(),IMU_Get_Front_Data_Num(),send_signal_flag);
  211. }
  212. void hal_mode_manage_Init(void)
  213. {
  214. if(INIT_MODE == HAL_MODE_NORMAL){
  215. cur_mode = HAL_MODE_NORMAL;
  216. change_mode = HAL_MODE_NORMAL;
  217. process_mode = HAL_MODE_NORMAL;
  218. }
  219. else if(INIT_MODE == HAL_MODE_STANDBY){
  220. cur_mode = HAL_MODE_STANDBY;
  221. change_mode = HAL_MODE_STANDBY;
  222. process_mode = HAL_MODE_STANDBY;
  223. }
  224. Process_Start(0,"hal_mode_manage_Process",hal_mode_manage_Process);
  225. // Process_Start(1000,"hal_mode_manage_test_Process",hal_mode_manage_test_Process);
  226. }