app_baseinfo.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include "usr.h"
  2. #include "app_baseinfo.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 "bsp_battery.h"
  9. #include "drv_ms5611.h"
  10. /*************************************************/
  11. #define SHOES_NAME "SMART SHOE"
  12. #define SHOES_NAME_LEN 64
  13. #define HARDWARE_VERSION 0x0100
  14. #define SOFTWARE_VERSION 0x0100
  15. #define BASEINFOBUFF_LEN 80
  16. static uint8_t baseinfoBuff[128];
  17. static uint8_t basedata[16];
  18. static uint8_t isGetClientInfo = 0;
  19. void Host_GetClientInfo(void)
  20. {
  21. static uint8_t buf[16];
  22. buf[0] = BLE_Host_T_UPDATE_BASEINFO;
  23. BLE_Host_Tx_Send(BLE_Host_T_UPDATE,buf,9,1,0,0,0);
  24. }
  25. void BASEINFO_HostGetClientData(void)
  26. {
  27. static uint32_t tim=0;
  28. if(TIME_GetTicks()-tim>=10000){ tim = TIME_GetTicks();
  29. if(isGetClientInfo==0) Host_GetClientInfo();
  30. }
  31. }
  32. //>> 0xA1: 查询
  33. void cb_BLE_Host_R_UPDATE(void* handle)
  34. {
  35. BLE_Host_Rx_t *target = handle;
  36. uint8_t _cmd = target->pDat[0];
  37. switch(_cmd){
  38. case BLE_Client_R_UPDATE_BASEINFO:{
  39. //>> 0(设备基本信息): 开始时间ms(8)
  40. //<< 0(子命令): 设备型号(64)+左鞋蓝牙地址(6)+硬件版本(2)+软件版本(2)+左鞋蓝牙地址(6)+硬件版本(6)+软件版本(2)
  41. //>> AA 06 F9 A1 00 4A
  42. //<< 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
  43. BLE_Client_Tx_Send(BLE_Client_T_UPDATE,baseinfoBuff,BASEINFOBUFF_LEN,1,0,0,0);
  44. break;}
  45. case BLE_Client_R_UPDATE_DATA:{
  46. //>> 1(设备数据)
  47. //<< 1(子命令): 左鞋电量(1)+左鞋温度(1)+左鞋压力(4)+右鞋电量(1)+右鞋温度(1)+右鞋压力(4)
  48. //>> AA 06 F9 A1 01 4B
  49. //<< AA 12 ED A1 01 00 90 12 34 56 78 00 90 12 34 56 78 93
  50. break;}
  51. case BLE_Client_R_UPDATE_STEPNUM:{//>> 2: 查询步数 + 0 + byte[8]: 更新时间ms + byte[1]:距离上一个整点的分钟数
  52. // << 2 + 0: 开始 + byte[8]: 开始时间ms + byte[1]: 包的数量
  53. // << 2 + byte[1]: 包序号 + byte[4]: 每小时步数(前面两个byte为步数,后面两个byte为距离) + byte[4]: 每小时步数(前面两个byte为步数,后面两个byte为距离) + byte[4]: 每小时步数(前面两个byte为步数,后面两个byte为距离) ..
  54. // >> 2 + byte[1]: 包序号(表示收到反馈)
  55. // << 2 + 2: byte[4]: 每小时步数 + byte[4]: 每小时步数 + byte[4]: 每小时步数 ..
  56. // >> 2 + byte[1]: 包序号(表示收到反馈).. 包的数量 == 序号 结束流程
  57. break;}
  58. default:break;
  59. }
  60. }
  61. void BASEINFO_SendUpdateData(void)
  62. {
  63. //>> 1(设备数据)
  64. //<< 1(子命令): 左鞋电量(1)+左鞋温度(1)+左鞋压力(4)+右鞋电量(1)+右鞋温度(1)+右鞋压力(4)
  65. //>> AA 06 F9 A1 01 4B
  66. //<< AA 12 ED A1 01 00 90 12 34 56 78 00 90 12 34 56 78 93
  67. uint8_t L=0;
  68. int32_t temperature;
  69. uint32_t press;
  70. sd_temp_get(&temperature);
  71. temperature = temperature>>2;
  72. press = MS5611_ReadPressure();
  73. SEGGER_RTT_printf(0,"temperature=%d\n",temperature);
  74. SEGGER_RTT_printf(0,"press=%d\n",press);
  75. temperature = 0x90;
  76. press = 0x12345678;
  77. //子命令
  78. basedata[L++] = BLE_Client_T_UPDATE_DATA;
  79. //左鞋
  80. basedata[L++] = ADC_GetBatteryPersent();
  81. basedata[L++] = (uint8_t)temperature;
  82. basedata[L++] = (uint8_t)(press>>24);
  83. basedata[L++] = (uint8_t)(press>>16);
  84. basedata[L++] = (uint8_t)(press>>8);
  85. basedata[L++] = (uint8_t)(press>>0);
  86. //右鞋
  87. basedata[L++] = ADC_GetBatteryPersent();
  88. basedata[L++] = (uint8_t)temperature;
  89. basedata[L++] = (uint8_t)(press>>24);
  90. basedata[L++] = (uint8_t)(press>>16);
  91. basedata[L++] = (uint8_t)(press>>8);
  92. basedata[L++] = (uint8_t)(press>>0);
  93. BLE_Client_Tx_Send(BLE_Client_T_UPDATE,basedata,L,1,0,0,0);
  94. }
  95. //>> 0xA1: 查询
  96. void cb_BLE_Client_R_UPDATE(void* handle)
  97. {
  98. BLE_Client_Rx_t *target = handle;
  99. uint8_t _cmd = target->pDat[0];
  100. switch(_cmd){
  101. case BLE_Client_R_UPDATE_BASEINFO:{
  102. //>> 0(设备基本信息): 开始时间ms(8)
  103. //<< 0(子命令): 设备型号(64)+左鞋蓝牙地址(6)+硬件版本(2)+软件版本(2)+左鞋蓝牙地址(6)+硬件版本(6)+软件版本(2)
  104. //>> AA 06 F9 A1 00 4A
  105. //<< 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
  106. BLE_Client_Tx_Send(BLE_Client_T_UPDATE,baseinfoBuff,BASEINFOBUFF_LEN,1,0,0,0);
  107. break;}
  108. case BLE_Client_R_UPDATE_DATA:{
  109. //>> 1(设备数据)
  110. //<< 1(子命令): 左鞋电量(1)+左鞋温度(1)+左鞋压力(4)+右鞋电量(1)+右鞋温度(1)+右鞋压力(4)
  111. //>> AA 06 F9 A1 01 4B
  112. //<< AA 12 ED A1 01 00 90 12 34 56 78 00 90 12 34 56 78 93
  113. BASEINFO_SendUpdateData();
  114. break;}
  115. case BLE_Client_R_UPDATE_STEPNUM:{//>> 2: 查询步数 + 0 + byte[8]: 更新时间ms + byte[1]:距离上一个整点的分钟数
  116. // << 2 + 0: 开始 + byte[8]: 开始时间ms + byte[1]: 包的数量
  117. // << 2 + byte[1]: 包序号 + byte[4]: 每小时步数(前面两个byte为步数,后面两个byte为距离) + byte[4]: 每小时步数(前面两个byte为步数,后面两个byte为距离) + byte[4]: 每小时步数(前面两个byte为步数,后面两个byte为距离) ..
  118. // >> 2 + byte[1]: 包序号(表示收到反馈)
  119. // << 2 + 2: byte[4]: 每小时步数 + byte[4]: 每小时步数 + byte[4]: 每小时步数 ..
  120. // >> 2 + byte[1]: 包序号(表示收到反馈).. 包的数量 == 序号 结束流程
  121. break;}
  122. default:break;
  123. }
  124. }
  125. void BASEINFO_Set(void)
  126. { //<< 0(设备基本信息): 设备型号(64)+左鞋蓝牙地址(6)+硬件版本(2)+软件版本(2)+左鞋蓝牙地址(6)+硬件版本(6)+软件版本(2)
  127. ble_gap_addr_t mAddr;
  128. uint32_t err_code;
  129. uint8_t L = SHOES_NAME_LEN+1;
  130. memset(baseinfoBuff,0,sizeof(baseinfoBuff));
  131. //子命令
  132. baseinfoBuff[0] = BLE_Client_T_UPDATE_BASEINFO;
  133. //设备名称
  134. for(int i=0;i<sizeof(SHOES_NAME);i++){
  135. baseinfoBuff[i+1] = SHOES_NAME[i];
  136. }
  137. //MAC 地址
  138. err_code = sd_ble_gap_addr_get(&mAddr); APP_ERROR_CHECK(err_code);
  139. SEGGER_RTT_printf(0,"mac addr:");for(int i=0;i<6;i++){SEGGER_RTT_printf(0,"%02X ",mAddr.addr[i]);}SEGGER_RTT_printf(0,"\n");
  140. for(int i=0;i<6;i++){
  141. baseinfoBuff[L++] = mAddr.addr[5-i];
  142. }
  143. //硬件版本
  144. baseinfoBuff[L++] = (uint8_t)((uint16_t)HARDWARE_VERSION>>8);
  145. baseinfoBuff[L++] = (uint8_t)((uint16_t)HARDWARE_VERSION>>0);
  146. //软件版本
  147. baseinfoBuff[L++] = (uint8_t)((uint16_t)SOFTWARE_VERSION>>8);
  148. baseinfoBuff[L++] = (uint8_t)((uint16_t)SOFTWARE_VERSION>>0);
  149. }
  150. void BASEINFO_Initialize(void)
  151. {
  152. BASEINFO_Set();
  153. BLE_Client_Rx_Regist(BLE_Client_R_UPDATE,cb_BLE_Client_R_UPDATE);
  154. BLE_Host_Rx_Regist(BLE_Host_R_UPDATE,cb_BLE_Host_R_UPDATE);
  155. Process_Regist(BASEINFO_HostGetClientData);
  156. }