You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

273 lines
10 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. #include "ble_parse_data.h"
  2. #include "motor_drive.h"
  3. #define BLE_PARSE_DATA_TAG "BLE_PARSE_DATA"
  4. #define cmd_length_set_position 5
  5. #define cmd_length_get_status 2
  6. #define cmd_length_set_motor_current_size 2
  7. #define cmd_length_set_motor_to_position 5
  8. static double encoder_befor_num;
  9. static bluetooth_processer_t *parse_bluetooth_processer;
  10. uint8_t bluetooth_rx_buffer_len = 0;
  11. void constructor_bluetooth_processer(bluetooth_processer_t *bluetooth_processer) { parse_bluetooth_processer = bluetooth_processer; }
  12. void bluetooth_gatts_try_process_data() {
  13. cJSON *json_tmp;
  14. // cJSON *ch;
  15. //开始接收
  16. if (parse_bluetooth_processer->bluetooth_rx_buffer_start_receving) {
  17. //开启定时器
  18. parse_bluetooth_processer->port_delay_ms(parse_bluetooth_processer->bluetooth_baundrate_one_packet_delay_ms);
  19. // port_timer_delay_ms(kbluetooth_baundrate_one_packet_delay_ms);
  20. parse_bluetooth_processer->bluetooth_rx_buffer_processing = true;
  21. //打印输出
  22. // ESP_LOGI(BLE_PARSE_DATA_TAG, "%s", parse_bluetooth_processer->bluetooth_processer_rx_buf);
  23. //验证解析数据是否正确
  24. if (parse_rxbuffer_and_validation_data(&json_tmp)) {
  25. // JSON解析到结构体,如果order更改表示有指令传输进来,并且更改指令标志位(cmd_flag)为true
  26. if (parse_json_to_struct(json_tmp->child)) {
  27. ESP_LOGI(BLE_PARSE_DATA_TAG, "order:%s ,index:%d speedLevel:%d position:%f direction:%d", parse_bluetooth_processer->order, parse_bluetooth_processer->index,
  28. parse_bluetooth_processer->speedLevel, parse_bluetooth_processer->position, parse_bluetooth_processer->direction);
  29. if (strcmp(parse_bluetooth_processer->order, set_position) == 0) {
  30. parse_bluetooth_processer->auto_report_flag = true;
  31. ESP_LOGI(BLE_PARSE_DATA_TAG, set_position);
  32. encoder_befor_num = motor_drive_read_encoder();
  33. if (encoder_befor_num >= 0) {
  34. if (motor_drive_set_packages_ctr(parse_bluetooth_processer->position, parse_bluetooth_processer->direction) == 0) {
  35. ets_delay_us(50000);
  36. if (encoder_befor_num == motor_drive_read_encoder()) {
  37. ESP_LOGW(BLE_PARSE_DATA_TAG, "motor no turning");
  38. } else {
  39. ESP_LOGI(BLE_PARSE_DATA_TAG, "motor turning");
  40. }
  41. }
  42. }
  43. // receipt_json_set_position();
  44. }
  45. if (strcmp(parse_bluetooth_processer->order, get_status) == 0) {
  46. ESP_LOGI(BLE_PARSE_DATA_TAG, get_status);
  47. receipt_json_get_status();
  48. }
  49. if (strcmp(parse_bluetooth_processer->order, set_motor_current_size) == 0) {
  50. ESP_LOGI(BLE_PARSE_DATA_TAG, set_motor_current_size);
  51. motor_drive_set_motor_current_size();
  52. // receipt_json_get_status();
  53. }
  54. if (strcmp(parse_bluetooth_processer->order, set_motor_to_position) == 0) {
  55. ESP_LOGI(BLE_PARSE_DATA_TAG, set_motor_to_position);
  56. motor_drive_set_motor_to_angle(parse_bluetooth_processer->direction, parse_bluetooth_processer->position, //
  57. parse_bluetooth_processer->speedLevel);
  58. // receipt_json_get_status();
  59. }
  60. }
  61. }
  62. //释放空间
  63. cJSON_Delete(json_tmp);
  64. // buffer置0
  65. buffer_all_init();
  66. //未在处理数据
  67. parse_bluetooth_processer->cmd_flag = false;
  68. parse_bluetooth_processer->bluetooth_rx_buffer_start_receving = false;
  69. parse_bluetooth_processer->bluetooth_rx_buffer_processing = false;
  70. }
  71. }
  72. void start_receive_data_to_buffer(uint16_t length, uint8_t *value) {
  73. parse_bluetooth_processer->bluetooth_rx_buffer_start_receving = true;
  74. timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0);
  75. //判断是否buffer越界
  76. if ((length + bluetooth_rx_buffer_len) > profile_b_buffer_size) {
  77. return;
  78. }
  79. if (!parse_bluetooth_processer->bluetooth_rx_buffer_processing) {
  80. //写入到buffer
  81. for (int i = 0; i < length; i++) {
  82. parse_bluetooth_processer->bluetooth_processer_rx_buf[bluetooth_rx_buffer_len++] = value[i];
  83. }
  84. }
  85. }
  86. void buffer_all_init() {
  87. bluetooth_rx_buffer_len = 0;
  88. memset(parse_bluetooth_processer->bluetooth_processer_rx_buf, 0, profile_b_buffer_size);
  89. }
  90. bool parse_rxbuffer_and_validation_data(cJSON **json_tmp) {
  91. *json_tmp = cJSON_Parse(parse_bluetooth_processer->bluetooth_processer_rx_buf);
  92. if (*json_tmp == NULL) {
  93. ESP_LOGE(BLE_PARSE_DATA_TAG, "parse rxbuffer null or redundant symbol ',','{' ");
  94. return false;
  95. }
  96. return true;
  97. }
  98. bool parse_json_to_struct(cJSON *ch) {
  99. uint8_t cmd_length = 0;
  100. while (ch != NULL) {
  101. // ESP_LOGI(BLE_PARSE_DATA_TAG, "%s", ch->string);
  102. if (strcmp(ch->string, "order") == 0) {
  103. parse_bluetooth_processer->order = ch->valuestring;
  104. if (strcmp(ch->valuestring, set_position) == 0) {
  105. cmd_length = cmd_length_set_position;
  106. }
  107. if (strcmp(ch->valuestring, get_status) == 0) {
  108. cmd_length = cmd_length_get_status;
  109. }
  110. if (strcmp(ch->valuestring, set_motor_current_size) == 0) {
  111. cmd_length = cmd_length_set_motor_current_size;
  112. }
  113. if (strcmp(ch->valuestring, set_motor_to_position) == 0) {
  114. cmd_length = cmd_length_set_motor_to_position;
  115. }
  116. cmd_length--;
  117. }
  118. if (strcmp(ch->string, "index") == 0) {
  119. parse_bluetooth_processer->index = ch->valueint;
  120. cmd_length--;
  121. }
  122. if (strcmp(ch->string, "speedLevel") == 0) {
  123. parse_bluetooth_processer->speedLevel = ch->valueint;
  124. cmd_length--;
  125. }
  126. if (strcmp(ch->string, "position") == 0) {
  127. parse_bluetooth_processer->position = ch->valuedouble;
  128. cmd_length--;
  129. }
  130. if (strcmp(ch->string, "direction") == 0) {
  131. parse_bluetooth_processer->direction = ch->valueint;
  132. cmd_length--;
  133. }
  134. ch = ch->next;
  135. }
  136. if (cmd_length == 0) {
  137. parse_bluetooth_processer->cmd_flag = true;
  138. } else {
  139. ESP_LOGE(BLE_PARSE_DATA_TAG, "JSON directive missing or exceeded");
  140. }
  141. return parse_bluetooth_processer->cmd_flag;
  142. }
  143. bool validation_param(cJSON *object, char *param) {
  144. cJSON *current_element = object->child;
  145. while (current_element->string != NULL) {
  146. if (current_element->string == param) {
  147. return true;
  148. }
  149. current_element = current_element->next;
  150. }
  151. return false;
  152. }
  153. void receipt_json_set_position() {
  154. cJSON *pRoot = cJSON_CreateObject(); //创建一个对象
  155. if (!pRoot) {
  156. return;
  157. }
  158. cJSON_AddStringToObject(pRoot, "order", "receipt"); //添加一个节点
  159. cJSON_AddNumberToObject(pRoot, "code", parse_bluetooth_processer->code);
  160. cJSON_AddStringToObject(pRoot, "info", "success");
  161. cJSON_AddNumberToObject(pRoot, "index", parse_bluetooth_processer->index);
  162. char *szJson = cJSON_Print(pRoot);
  163. if (szJson != NULL) {
  164. ESP_LOGI(BLE_PARSE_DATA_TAG, "%s", szJson);
  165. free(szJson);
  166. }
  167. cJSON_Delete(pRoot);
  168. }
  169. void receipt_json_get_status() {
  170. cJSON *pRoot = cJSON_CreateObject(); //创建一个对象
  171. if (!pRoot) {
  172. return;
  173. }
  174. parse_bluetooth_processer->motor_drive_turn_flag = true;
  175. cJSON_AddStringToObject(pRoot, "order", "receipt"); //添加一个节点
  176. cJSON_AddNumberToObject(pRoot, "index", parse_bluetooth_processer->index);
  177. cJSON_AddStringToObject(pRoot, "deviceState", parse_bluetooth_processer->deviceState);
  178. cJSON_AddNumberToObject(pRoot, "deviceException", parse_bluetooth_processer->deviceException);
  179. cJSON_AddStringToObject(pRoot, "deviceExceptionInfo", parse_bluetooth_processer->deviceExceptionInfo);
  180. cJSON_AddNumberToObject(pRoot, "position", parse_bluetooth_processer->position);
  181. char *szJson = cJSON_Print(pRoot);
  182. if (szJson != NULL) {
  183. ESP_LOGI(BLE_PARSE_DATA_TAG, "%s", szJson);
  184. free(szJson);
  185. }
  186. cJSON_Delete(pRoot);
  187. }
  188. void bluetooth_auto_report_format_receipt() {
  189. sprintf(parse_bluetooth_processer->bluetooth_processer_tx_buf, "{ \"order\": \"receipt\", \"index\": %d, \"speedLevel\": %d, \"position\": %.2lf, \"direction\": %d }", //
  190. parse_bluetooth_processer->index, parse_bluetooth_processer->speedLevel, parse_bluetooth_processer->position, parse_bluetooth_processer->direction);
  191. }
  192. void bluetooth_tx_buffer_send_indicate(cb_t format) {
  193. char temp_buffer[20] = {0};
  194. uint8_t temp_lenght = 0;
  195. uint8_t temp_count_total = 0;
  196. uint8_t temp_last_count_not_15 = false;
  197. uint8_t temp_count_remainder = 0;
  198. uint8_t i = 0;
  199. format();
  200. temp_lenght = strlen(parse_bluetooth_processer->bluetooth_processer_tx_buf);
  201. temp_count_total = temp_lenght / 15;
  202. if ((temp_lenght % 15) != 0) {
  203. temp_count_remainder = temp_lenght % 15;
  204. temp_last_count_not_15 = true;
  205. }
  206. for (i = 0; i < temp_count_total; i++) {
  207. string_copy_by_num(temp_buffer, parse_bluetooth_processer->bluetooth_processer_tx_buf, i, 15);
  208. esp_ble_gatts_send_indicate(parse_bluetooth_processer->table_gatts_if_m, parse_bluetooth_processer->table_conn_id_m, //
  209. parse_bluetooth_processer->table_handle_m, strlen(temp_buffer), (uint8_t *)temp_buffer, false);
  210. }
  211. if (temp_last_count_not_15 == true) {
  212. string_copy_by_num(temp_buffer, parse_bluetooth_processer->bluetooth_processer_tx_buf, temp_count_total, temp_count_remainder);
  213. esp_ble_gatts_send_indicate(parse_bluetooth_processer->table_gatts_if_m, parse_bluetooth_processer->table_conn_id_m, //
  214. parse_bluetooth_processer->table_handle_m, strlen(temp_buffer), (uint8_t *)temp_buffer, false);
  215. }
  216. }
  217. void string_copy_by_num(char *dest, const char *src, uint8_t count, uint8_t num) {
  218. int i = 0;
  219. memset(dest, '\0', 15);
  220. if (dest == NULL || src == NULL || num == 0) {
  221. /* code */
  222. ESP_LOGW(BLE_PARSE_DATA_TAG, "string_copy_by_num function parameter is empty");
  223. }
  224. for (i = 0; i < num; i++) {
  225. dest[i] = src[(count * 15) + i];
  226. }
  227. }
  228. void bluetooth_active_notify(uint8_t *buffer, uint8_t buffer_size) {
  229. if (parse_bluetooth_processer->table_handle_m != 0) {
  230. esp_ble_gatts_send_indicate(parse_bluetooth_processer->table_gatts_if_m, parse_bluetooth_processer->table_conn_id_m, //
  231. parse_bluetooth_processer->table_handle_m, buffer_size, buffer, false);
  232. }
  233. }