hal_mode_manage.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. send_signal_flag = 0;
  78. critical_zone = 0; //解锁
  79. }else{
  80. if(IMU_Get_Front_Data_Num() >= 1 && IMU_Get_Front_Data_Num() < 10){
  81. cur_mode = HAL_MODE_GAME;
  82. send_signal_flag = 0;
  83. critical_zone = 0; //解锁
  84. }
  85. }
  86. break;
  87. case HAL_MODE_REALSTEP:
  88. if(IMU_GetCurrentMode() != STATE_FULL_POWER_MODE){
  89. send_signal_flag = 0;
  90. critical_zone = 0; //解锁
  91. }else{
  92. if(IMU_Get_Front_Data_Num() >= 1 && IMU_Get_Front_Data_Num() < 10){
  93. cur_mode = HAL_MODE_REALSTEP;
  94. send_signal_flag = 0;
  95. critical_zone = 0; //解锁
  96. }
  97. }
  98. break;
  99. case HAL_MODE_STANDBY:
  100. if(IMU_GetCurrentMode() != STATE_STANDBY_POWER_MODE){
  101. send_signal_flag = 0;
  102. critical_zone = 0; //解锁
  103. }else{
  104. if(IMU_Get_Front_Data_Num() >= 1 && IMU_Get_Front_Data_Num() < 10){
  105. cur_mode = HAL_MODE_STANDBY;
  106. send_signal_flag = 0;
  107. critical_zone = 0; //解锁
  108. }
  109. }
  110. break;
  111. case HAL_MODE_NORMAL:
  112. if(IMU_GetCurrentMode() != STATE_LOW_POWER_MODE){
  113. send_signal_flag = 0;
  114. critical_zone = 0; //解锁
  115. }else{
  116. if(IMU_Get_Front_Data_Num() >= 10 && IMU_Get_Front_Data_Num() < 30){
  117. cur_mode = HAL_MODE_NORMAL;
  118. send_signal_flag = 0;
  119. critical_zone = 0; //解锁
  120. }
  121. }
  122. break;
  123. case HAL_MODE_SELF_CHECK:
  124. if(IMU_GetCurrentMode() != STATE_SELF_CHECK_MODE){
  125. send_signal_flag = 0;
  126. critical_zone = 0; //解锁
  127. }else{
  128. if(IMU_Get_Front_Data_Num() >= 1 && IMU_Get_Front_Data_Num() < 10){
  129. cur_mode = HAL_MODE_SELF_CHECK;
  130. send_signal_flag = 0;
  131. critical_zone = 0; //解锁
  132. }
  133. }
  134. break;
  135. default:
  136. break;
  137. }
  138. }
  139. //转换规则
  140. static void hal_mode_manage_Discipline(hal_mode *source_mode, hal_mode target_mode)
  141. {
  142. switch(*source_mode)
  143. {
  144. case HAL_MODE_STANDBY:
  145. *source_mode = target_mode; //待机模式可转为任意模式
  146. break;
  147. case HAL_MODE_NORMAL:
  148. *source_mode = target_mode; //正常模式可转为任意模式
  149. break;
  150. case HAL_MODE_SELF_CHECK:
  151. if(target_mode == HAL_MODE_NORMAL){
  152. *source_mode = target_mode; //自检模式只能转为正常模式
  153. }
  154. break;
  155. case HAL_MODE_GAME:
  156. if(target_mode == HAL_MODE_NORMAL){
  157. *source_mode = target_mode; //游戏模式只能转为正常模式
  158. }
  159. break;
  160. case HAL_MODE_REALSTEP:
  161. if(target_mode == HAL_MODE_NORMAL){
  162. *source_mode = target_mode; //实时计步模式只能转为正常模式
  163. }
  164. break;
  165. }
  166. }
  167. static void hal_mode_manage_Process(void)
  168. {
  169. //锁上期间,不更新 process_mode
  170. if(!critical_zone){
  171. hal_mode_manage_Discipline(&process_mode, change_mode);
  172. critical_zone = 1; //锁上
  173. }
  174. //没有信号的时候处理
  175. if(IMU_IsNoSignal()){
  176. if(!send_signal_flag){ //发送信号
  177. Send_Signal(process_mode);
  178. }else{ //配置判断
  179. Change_Mode(process_mode);
  180. }
  181. }
  182. }
  183. /********************** 外部接口声明区 *************************/
  184. void hal_mode_set(hal_mode mode)
  185. {
  186. hal_mode_manage_Discipline(&change_mode, mode);
  187. }
  188. hal_mode hal_mode_get(void)
  189. {
  190. return cur_mode;
  191. }
  192. static void hal_mode_manage_test_Process(void)
  193. {
  194. 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);
  195. }
  196. void hal_mode_manage_Init(void)
  197. {
  198. if(INIT_MODE == HAL_MODE_NORMAL){
  199. cur_mode = HAL_MODE_NORMAL;
  200. change_mode = HAL_MODE_NORMAL;
  201. process_mode = HAL_MODE_NORMAL;
  202. }
  203. else if(INIT_MODE == HAL_MODE_STANDBY){
  204. cur_mode = HAL_MODE_STANDBY;
  205. change_mode = HAL_MODE_STANDBY;
  206. process_mode = HAL_MODE_STANDBY;
  207. }
  208. Process_Start(0,"hal_mode_manage_Process",hal_mode_manage_Process);
  209. Process_Start(1000,"hal_mode_manage_test_Process",hal_mode_manage_test_Process);
  210. }