app_client.c 6.7 KB

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