app_client.c 6.5 KB

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