app_client.c 6.7 KB

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