app_Authen.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "usr_config.h"
  2. #include "app_Authen.h"
  3. #include "system.h"
  4. #include "hal_ble_client.h"
  5. #include "hal_ble_host.h"
  6. #include "bsp_time.h"
  7. #include "ble_comm.h"
  8. #include "bll_imu.h"
  9. #include "app_flash.h"
  10. /************************ º¯ÊýÉùÃ÷ ***********************************/
  11. static uint8_t flag =0;
  12. static uint32_t tim =0;
  13. static void cb_BLE_Client_BLE_AUTHEN(void* handle){
  14. BLE_Client_Rx_t* target = handle;
  15. flag = target->pDat[0];
  16. tim = TIME_GetTicks();
  17. }
  18. static void cb_BLE_Host_BLE_AUTHEN(void* handle){
  19. BLE_Host_Rx_t* target = handle;
  20. BLE_Client_Tx_Send(0,BLE_AUTHEN,target->pDat,target->datLen);
  21. }
  22. static BLE_Client_Tx_t m_Client_BLE_AUTHEN = {
  23. .n = 10,
  24. .t = 100,
  25. };
  26. static void app_Authen_Process(void){
  27. static uint8_t state =0;
  28. bll_imu_data_t data= {0};
  29. static uint8_t LIFE_SIGNAL =0;
  30. uint8_t buf =0;
  31. switch(state){
  32. case 0:
  33. if(1 == flag){state =1;}
  34. break;
  35. case 1:
  36. if(2 == flag){
  37. if(mFlash.isHost){
  38. state =2;
  39. flag =1;
  40. }
  41. else {
  42. state =0;
  43. flag =0;
  44. }
  45. }
  46. else if(TIME_GetTicks() - tim >=3000){
  47. state =0;
  48. flag =0;
  49. if(slave_isconnect())slave_disconnect();
  50. }
  51. break;
  52. case 2:
  53. if(!slave_isconnect()){
  54. state =0;
  55. flag =0;
  56. }
  57. else {
  58. int16_t data_size = 0;
  59. data_size = bll_imu_get_data_num(BLL_IMU_DIR_FRONT);
  60. if(data_size > 0){
  61. bll_imu_get_data(BLL_IMU_DIR_FRONT, data_size -1 , &data);
  62. int32_t acc_norm = (data.acc[0] * data.acc[0] +data.acc[1] * data.acc[1] + data.acc[2] * data.acc[2]);
  63. if(abs(acc_norm - 4194304) < 362144 && data.acc[2] > 1536){
  64. LIFE_SIGNAL = 3;
  65. }
  66. else if(LIFE_SIGNAL > 0)LIFE_SIGNAL --;
  67. if(abs(acc_norm - 4194304) < 362144 && data.acc[2] < -1024)LIFE_SIGNAL = 0;
  68. if(LIFE_SIGNAL > 0){
  69. buf =3;
  70. BLE_Client_Tx_Send(&m_Client_BLE_AUTHEN,BLE_AUTHEN,&buf,1);
  71. state =0;
  72. flag =0;
  73. }
  74. }
  75. }
  76. break;
  77. default:state=0;break;
  78. }
  79. }
  80. void app_Authen_Init(void)
  81. {
  82. Process_Start(1000,"app_charge",app_Authen_Process);
  83. BLE_Client_Rx_Regist(BLE_AUTHEN,cb_BLE_Client_BLE_AUTHEN);
  84. BLE_Host_Rx_Regist(BLE_AUTHEN,cb_BLE_Host_BLE_AUTHEN);
  85. }