hal_mode_manage.c 7.1 KB

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