app_client.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. sd_temp_get(&temperature);
  23. temperature = temperature>>2;
  24. //子命令
  25. buf[L++] = BLE_UPDATE_DATA;
  26. //左鞋
  27. buf[L++] = GetBatteryPersent();
  28. buf[L++] = (uint8_t)temperature;
  29. //压力数据
  30. buf[L++] = 0;
  31. buf[L++] = 0;
  32. buf[L++] = 0;
  33. buf[L++] = 0;
  34. buf[L++] = (uint8_t)(mFlash.mStep.stepCur[0]>>24);
  35. buf[L++] = (uint8_t)(mFlash.mStep.stepCur[0]>>16);
  36. buf[L++] = (uint8_t)(mFlash.mStep.stepCur[0]>>8);
  37. buf[L++] = (uint8_t)(mFlash.mStep.stepCur[0]>>0);
  38. //右鞋
  39. buf[L++] = app_host_GetVol_R();
  40. buf[L++] = app_host_GetTemp_R();
  41. buf[L++] = (uint8_t)(app_host_GetPress_R()>>24);
  42. buf[L++] = (uint8_t)(app_host_GetPress_R()>>16);
  43. buf[L++] = (uint8_t)(app_host_GetPress_R()>>8);
  44. buf[L++] = (uint8_t)(app_host_GetPress_R()>>0);
  45. buf[L++] = (uint8_t)(mFlash.mStep.stepCur[1]>>24);
  46. buf[L++] = (uint8_t)(mFlash.mStep.stepCur[1]>>16);
  47. buf[L++] = (uint8_t)(mFlash.mStep.stepCur[1]>>8);
  48. buf[L++] = (uint8_t)(mFlash.mStep.stepCur[1]>>0);
  49. int16_t left_adc = hal_GetBatttery_Adc();
  50. buf[L++] = (uint8_t)(left_adc>>8);
  51. buf[L++] = (uint8_t)(left_adc);
  52. buf[L++] = (uint8_t)(app_host_GetVolAdc_R()>>8);
  53. buf[L++] = (uint8_t)(app_host_GetVolAdc_R());
  54. BLE_Client_Tx_Send(0,BLE_UPDATE,buf,L);
  55. }
  56. void app_client_infomation_Send_defineName(uint8_t name[],uint8_t lenth)
  57. {
  58. //<< 0(设备基本信息): 设备型号(64)+左鞋蓝牙地址(6)+硬件版本(2)+软件版本(2)+左鞋蓝牙地址(6)+硬件版本(6)+软件版本(2)
  59. uint8_t buf[128];
  60. uint8_t L = SHOES_NAME_LEN+1;
  61. memset(buf,0,sizeof(buf));
  62. //子命令
  63. buf[0] = BLE_UPDATE_BASEINFO;
  64. //设备名称
  65. for(int i=0;i<lenth;i++){
  66. buf[i+1] = name[i];
  67. }
  68. //MAC 地址
  69. Get_MACaddr(&buf[L]);
  70. //DEBUG_LOG("mac addr:");for(int i=0;i<6;i++){DEBUG_LOG("%02X ",buf[L+i]);}DEBUG_LOG("\n");
  71. L +=6;
  72. //硬件版本
  73. buf[L++] = (uint8_t)((uint32_t)HARDWARE_VERSION>>24);
  74. buf[L++] = (uint8_t)((uint32_t)HARDWARE_VERSION>>16);
  75. buf[L++] = (uint8_t)((uint32_t)HARDWARE_VERSION>>8);
  76. buf[L++] = (uint8_t)((uint32_t)HARDWARE_VERSION>>0);
  77. //软件版本
  78. buf[L++] = (uint8_t)(SOFTWARE_VERSION>>8);
  79. buf[L++] = (uint8_t)(SOFTWARE_VERSION>>0);
  80. //右鞋mac地址
  81. for(int i=0;i<6;i++) buf[L++] = mFlash.mClient.macAddr[i];
  82. //右鞋硬件版本
  83. buf[L++] = (uint8_t)((uint32_t)mFlash.mClient.hardVersion>>24);
  84. buf[L++] = (uint8_t)((uint32_t)mFlash.mClient.hardVersion>>16);
  85. buf[L++] = (uint8_t)((uint32_t)mFlash.mClient.hardVersion>>8);
  86. buf[L++] = (uint8_t)((uint32_t)mFlash.mClient.hardVersion>>0);
  87. //右鞋软件版本
  88. buf[L++] = (uint8_t)(mFlash.mClient.sotfVersion>>8);
  89. buf[L++] = (uint8_t)(mFlash.mClient.sotfVersion>>0);
  90. BLE_Client_Tx_Send(0,BLE_UPDATE,buf,L);
  91. }
  92. void app_client_infomation_Send(void)
  93. {
  94. //<< 0(设备基本信息): 设备型号(64)+左鞋蓝牙地址(6)+硬件版本(2)+软件版本(2)+左鞋蓝牙地址(6)+硬件版本(6)+软件版本(2)
  95. uint8_t buf[128];
  96. uint8_t L = SHOES_NAME_LEN+1;
  97. memset(buf,0,sizeof(buf));
  98. //子命令
  99. buf[0] = BLE_UPDATE_BASEINFO;
  100. //设备名称
  101. for(int i=0;i<sizeof(SHOES_NAME);i++){
  102. buf[i+1] = SHOES_NAME[i];
  103. }
  104. //MAC 地址
  105. Get_MACaddr(&buf[L]);
  106. //DEBUG_LOG("mac addr:");for(int i=0;i<6;i++){DEBUG_LOG("%02X ",buf[L+i]);}DEBUG_LOG("\n");
  107. L +=6;
  108. //硬件版本
  109. buf[L++] = (uint8_t)((uint32_t)HARDWARE_VERSION>>24);
  110. buf[L++] = (uint8_t)((uint32_t)HARDWARE_VERSION>>16);
  111. buf[L++] = (uint8_t)((uint32_t)HARDWARE_VERSION>>8);
  112. buf[L++] = (uint8_t)((uint32_t)HARDWARE_VERSION>>0);
  113. //软件版本
  114. buf[L++] = (uint8_t)(SOFTWARE_VERSION>>8);
  115. buf[L++] = (uint8_t)(SOFTWARE_VERSION>>0);
  116. //右鞋mac地址
  117. for(int i=0;i<6;i++) buf[L++] = mFlash.mClient.macAddr[i];
  118. // DEBUG_LOG("mac addr:");for(int i=0;i<6;i++){DEBUG_LOG("%02X ",mFlash.mClient.macAddr[i]);}DEBUG_LOG("\n");
  119. //右鞋硬件版本
  120. buf[L++] = (uint8_t)((uint32_t)mFlash.mClient.hardVersion>>24);
  121. buf[L++] = (uint8_t)((uint32_t)mFlash.mClient.hardVersion>>16);
  122. buf[L++] = (uint8_t)((uint32_t)mFlash.mClient.hardVersion>>8);
  123. buf[L++] = (uint8_t)((uint32_t)mFlash.mClient.hardVersion>>0);
  124. //右鞋软件版本
  125. //右鞋软件版本
  126. buf[L++] = (uint8_t)(mFlash.mClient.sotfVersion>>8);
  127. buf[L++] = (uint8_t)(mFlash.mClient.sotfVersion>>0);
  128. BLE_Client_Tx_Send(0,BLE_UPDATE,buf,L);
  129. }
  130. //>> 0xA1: 查询
  131. void cb_BLE_Client_R_UPDATE(void* handle)
  132. {
  133. BLE_Client_Rx_t *target = handle;
  134. uint8_t _cmd = target->pDat[0];
  135. switch(_cmd){
  136. case BLE_UPDATE_BASEINFO:{
  137. //>> 0(设备基本信息): 开始时间ms(8)
  138. //<< 0(子命令): 设备型号(64)+左鞋蓝牙地址(6)+硬件版本(2)+软件版本(2)+右鞋蓝牙地址(6)+硬件版本(6)+软件版本(2)
  139. //>> AA 06 F9 A1 00 4A
  140. //<< 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
  141. if(1 == mFlash.isHost){
  142. app_host_GetClientInfo();
  143. }
  144. else app_client_infomation_Send();
  145. // DEBUG_LOG("!!!!!!!!!!!!!!!=======>app_client_infomation_Send\n");
  146. break;}
  147. case BLE_UPDATE_DATA:{
  148. //>> 1(设备数据)
  149. //<< 1(子命令): 左鞋电量(1)+左鞋温度(1)+左鞋压力(4)+左鞋步数(4)+右鞋电量(1)+右鞋温度(1)+右鞋压力(4)+右鞋步数(4)
  150. if(1 == mFlash.isHost){
  151. app_host_GetClientData(20);
  152. }
  153. else app_client_DataUpdate_Send();
  154. break;}
  155. case BLE_UPDATE_STEPNUM:{
  156. //>> 2(查询步数): 包序号[0全部](2)
  157. //<< 2(查询步数): +开始时间ms(8)+包的数量(2)+包序号(2)+每小时步数(4)+...
  158. if(!app_step_Real_Get())app_client_SendStep(target);
  159. break;}
  160. case BLE_UPDATE_STEPNUM_CLERA:{
  161. //>> 3(删除步数): 开始时间ms(8)+距离上一个整点的分钟数(1)
  162. //<< 3(删除步数): 开始时间ms(8)+距离上一个整点的分钟数(1)
  163. BLE_Client_Tx_Send(0,BLE_UPDATE,target->pDat,target->datLen);
  164. app_client_DeleteStep(target);
  165. break;}
  166. default:break;
  167. }
  168. }
  169. static void app_client_Process(void)
  170. {
  171. static uint8_t cnt =0;
  172. if(mFlash.isHost)return;
  173. if(slave_isconnect()){//右鞋连接上左鞋
  174. if(app_game_GetGameMode()){
  175. if(cnt++ >= 4){cnt =0;
  176. app_client_DataUpdate_Send();
  177. }
  178. }
  179. else app_client_DataUpdate_Send();
  180. }
  181. }
  182. void app_client_Initialize(void)
  183. {
  184. BLE_Client_Rx_Regist(BLE_UPDATE,cb_BLE_Client_R_UPDATE);
  185. app_client_step_Initialize();
  186. Process_Start(2000,"app_client",app_client_Process);
  187. }