app_client.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include "system.h"
  2. #include "app_client.h"
  3. #include "hal_ble_client.h"
  4. #include "hal_ble_host.h"
  5. #include "bsp_time.h"
  6. #include "hal_led.h"
  7. #include "ble_gap.h"
  8. #include "app_host.h"
  9. #include "app_flash.h"
  10. #include "app_client_step.h"
  11. #include "hal_battery.h"
  12. #include "ble_comm.h"
  13. #include "app_game.h"
  14. #include "app_step.h"
  15. void app_client_DataUpdate_Send(void)
  16. {
  17. //>> 1(设备数据)
  18. //<< 1(子命令): 左鞋电量(1)+左鞋温度(1)+左鞋压力(4)+左鞋步数(4)+右鞋电量(1)+右鞋温度(1)+右鞋压力(4)+右鞋步数(4)
  19. uint8_t buf[32];
  20. uint8_t L=0;
  21. int32_t temperature;
  22. memset(buf,0,sizeof(buf));
  23. sd_temp_get(&temperature);
  24. temperature = temperature>>2;
  25. //子命令
  26. buf[L++] = BLE_UPDATE_DATA;
  27. //左鞋
  28. buf[L++] = GetBatteryPersent();
  29. buf[L++] = (uint8_t)temperature;
  30. //压力数据
  31. L +=4;
  32. buf[L++] = (uint8_t)(mFlash.mStep.stepCur[0]>>24);
  33. buf[L++] = (uint8_t)(mFlash.mStep.stepCur[0]>>16);
  34. buf[L++] = (uint8_t)(mFlash.mStep.stepCur[0]>>8);
  35. buf[L++] = (uint8_t)(mFlash.mStep.stepCur[0]>>0);
  36. //右鞋电量,温度,压力值
  37. buf[L++] = app_host_GetVol_R();
  38. buf[L++] = app_host_GetTemp_R();
  39. L +=4;
  40. buf[L++] = (uint8_t)(mFlash.mStep.stepCur[1]>>24);
  41. buf[L++] = (uint8_t)(mFlash.mStep.stepCur[1]>>16);
  42. buf[L++] = (uint8_t)(mFlash.mStep.stepCur[1]>>8);
  43. buf[L++] = (uint8_t)(mFlash.mStep.stepCur[1]>>0);
  44. int16_t left_adc = hal_GetBatttery_Adc();
  45. buf[L++] = (uint8_t)(left_adc>>8);
  46. buf[L++] = (uint8_t)(left_adc);
  47. buf[L++] = (uint8_t)(app_host_GetVolAdc_R()>>8);
  48. buf[L++] = (uint8_t)(app_host_GetVolAdc_R());
  49. BLE_Client_Tx_Send(0,BLE_UPDATE,buf,L);
  50. }
  51. void app_client_infomation_Send(void)
  52. {
  53. //<< 0(设备基本信息): 设备型号(64)+左鞋蓝牙地址(6)+硬件版本(2)+软件版本(2)+左鞋蓝牙地址(6)+硬件版本(6)+软件版本(2)
  54. uint8_t buf[128];
  55. uint8_t L = SHOES_NAME_LEN+1;
  56. int i=0;
  57. memset(buf,0,sizeof(buf));
  58. //子命令
  59. buf[0] = BLE_UPDATE_BASEINFO;
  60. //设备名称
  61. for(i=0;i<sizeof(SHOES_NAME);i++){
  62. buf[i+1] = SHOES_NAME[i];
  63. }
  64. //MAC 地址
  65. Get_MACaddr(&buf[L]);
  66. //DEBUG_LOG("mac addr:");for(int i=0;i<6;i++){DEBUG_LOG("%02X ",buf[L+i]);}DEBUG_LOG("\n");
  67. L +=6;
  68. //硬件版本
  69. buf[L++] = (uint8_t)((uint32_t)HARDWARE_VERSION>>24);
  70. buf[L++] = (uint8_t)((uint32_t)HARDWARE_VERSION>>16);
  71. buf[L++] = (uint8_t)((uint32_t)HARDWARE_VERSION>>8);
  72. buf[L++] = (uint8_t)((uint32_t)HARDWARE_VERSION>>0);
  73. //软件版本
  74. buf[L++] = (uint8_t)(SOFTWARE_VERSION>>8);
  75. buf[L++] = (uint8_t)(SOFTWARE_VERSION>>0);
  76. //右鞋mac地址
  77. for(i=0;i<6;i++) buf[L++] = mFlash.mClient.macAddr[i];
  78. // DEBUG_LOG("mac addr:");for(int i=0;i<6;i++){DEBUG_LOG("%02X ",mFlash.mClient.macAddr[i]);}DEBUG_LOG("\n");
  79. //右鞋硬件版本
  80. buf[L++] = (uint8_t)((uint32_t)mFlash.mClient.hardVersion>>24);
  81. buf[L++] = (uint8_t)((uint32_t)mFlash.mClient.hardVersion>>16);
  82. buf[L++] = (uint8_t)((uint32_t)mFlash.mClient.hardVersion>>8);
  83. buf[L++] = (uint8_t)((uint32_t)mFlash.mClient.hardVersion>>0);
  84. //右鞋软件版本
  85. buf[L++] = (uint8_t)(mFlash.mClient.sotfVersion>>8);
  86. buf[L++] = (uint8_t)(mFlash.mClient.sotfVersion>>0);
  87. BLE_Client_Tx_Send(0,BLE_UPDATE,buf,L);
  88. }
  89. //>> 0xA1: 查询
  90. void cb_BLE_Client_R_UPDATE(void* handle)
  91. {
  92. BLE_Client_Rx_t *target = handle;
  93. uint8_t _cmd = target->pDat[0];
  94. switch(_cmd){
  95. case BLE_UPDATE_BASEINFO:{
  96. //>> 0(设备基本信息): 开始时间ms(8)
  97. //<< 0(子命令): 设备型号(64)+左鞋蓝牙地址(6)+硬件版本(2)+软件版本(2)+右鞋蓝牙地址(6)+硬件版本(6)+软件版本(2)
  98. //>> AA 06 F9 A1 00 4A
  99. //<< AA 55 AA A1 00 53 4D 41 52 54 20 53 48 4F 45 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 C1 EB D0 E3 87 47 01 00 01 00 00 00 00 00 00 4F
  100. if(1 == mFlash.isHost){
  101. app_host_GetClientInfo();
  102. }
  103. else app_client_infomation_Send();
  104. // DEBUG_LOG("!!!!!!!!!!!!!!!=======>app_client_infomation_Send\n");
  105. break;}
  106. case BLE_UPDATE_DATA:{
  107. //>> 1(设备数据)
  108. //<< 1(子命令): 左鞋电量(1)+左鞋温度(1)+左鞋压力(4)+左鞋步数(4)+右鞋电量(1)+右鞋温度(1)+右鞋压力(4)+右鞋步数(4)
  109. if(1 == mFlash.isHost){
  110. app_host_GetClientData(20);
  111. }
  112. else app_client_DataUpdate_Send();
  113. break;}
  114. case BLE_UPDATE_STEPNUM:{
  115. //>> 2(查询步数): 包序号[0全部](2)
  116. //<< 2(查询步数): +开始时间ms(8)+包的数量(2)+包序号(2)+每小时步数(4)+...
  117. if(!app_step_Real_Get())app_client_SendStep(target);
  118. break;}
  119. case BLE_UPDATE_STEPNUM_CLERA:{
  120. //>> 3(删除步数): 开始时间ms(8)+距离上一个整点的分钟数(1)
  121. //<< 3(删除步数): 开始时间ms(8)+距离上一个整点的分钟数(1)
  122. BLE_Client_Tx_Send(0,BLE_UPDATE,target->pDat,target->datLen);
  123. app_client_DeleteStep(target);
  124. break;}
  125. default:break;
  126. }
  127. }
  128. static void app_client_Process(void)
  129. {
  130. static uint8_t cnt =0;
  131. if(mFlash.isHost)return;
  132. if(slave_isconnect()){//右鞋连接上左鞋
  133. if(app_game_GetGameMode()){
  134. if(cnt++ >= 4){cnt =0;
  135. app_client_DataUpdate_Send();
  136. }
  137. }
  138. else app_client_DataUpdate_Send();
  139. }
  140. }
  141. void app_client_Initialize(void)
  142. {
  143. BLE_Client_Rx_Regist(BLE_UPDATE,cb_BLE_Client_R_UPDATE);
  144. app_client_step_Initialize();
  145. Process_Start(2000,"app_client",app_client_Process);
  146. }