hal_mode_manage.c 6.0 KB

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