医美代码重构
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.

228 lines
7.2 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. #include "cJSON.h"
  2. #include "cJSON_Utils.h"
  3. #include "driver/gpio.h"
  4. #include "driver/timer.h"
  5. #include "esp_log.h"
  6. #include "esp_system.h"
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/event_groups.h"
  9. #include "freertos/task.h"
  10. #include "nvs_flash.h"
  11. #include "string.h"
  12. //
  13. #include "ble_spp_server_demo.h"
  14. #include "motor_drive.h"
  15. #define MAIN_LOG_TAG "MAIN"
  16. #define ble_uart_tx_size 128
  17. #define ble_uart_rx_size 128
  18. static uint8_t bluetooth_tx_buffer[ble_uart_tx_size] = {0};
  19. static uint8_t bluetooth_rx_buffer[ble_uart_rx_size] = {0};
  20. uint8_t ble_rx_buffer_now_len = 0;
  21. uint8_t ble_rx_buffer_off = 0;
  22. uint32_t start_time = 0;
  23. uint16_t modbus_uart_rx_off_before = 0;
  24. bleuart_t ble_uart_init_struct = {
  25. .txbuf = bluetooth_tx_buffer,
  26. .txbufsize = sizeof(bluetooth_tx_buffer),
  27. .rxbuf = bluetooth_rx_buffer,
  28. .rxbufsize = sizeof(bluetooth_rx_buffer),
  29. .rxpacketReceiveOvertime = 200,
  30. .receive_data_processing_flag = false,
  31. .maxClientNum = 1,
  32. .bleName = "yimei_ble",
  33. };
  34. static cJSON *json_tmp;
  35. // static double encoder_befor_num;
  36. #define set_position "setPosition"
  37. #define get_status "getStatus"
  38. #define set_motor_current_size "setMotorCurrentSize"
  39. #define set_motor_to_position "setMotorToPosition"
  40. #define cmd_length_set_position 5
  41. #define cmd_length_get_status 2
  42. #define cmd_length_set_motor_current_size 2
  43. #define cmd_length_set_motor_to_position 5
  44. typedef struct {
  45. char *order; //指令名称
  46. int index; //
  47. int speedLevel; //
  48. double position; //角度
  49. int direction; //旋转方向
  50. int code; //错误码
  51. char *info; //错误码信息
  52. char *deviceState; //设备状态
  53. int deviceException; //设备异常编号
  54. char *deviceExceptionInfo; //设备异常信息
  55. bool cmd_flag;
  56. } ble_uart_receive_data_t;
  57. ble_uart_receive_data_t ble_uart_receive_data = {
  58. .order = "order",
  59. .index = 0,
  60. .speedLevel = 0,
  61. .position = 0.0,
  62. .direction = 0,
  63. .code = 0,
  64. .info = "noerror",
  65. .deviceState = "init",
  66. .deviceException = 0,
  67. .deviceExceptionInfo = "noexception",
  68. .cmd_flag = false,
  69. };
  70. void blerxcb(uint8_t *rxmessage, size_t rxsize) {
  71. if ((rxsize + ble_rx_buffer_now_len) > ble_uart_init_struct.rxbufsize) {
  72. ESP_LOGW("MAIN", "rx buffer overstep");
  73. return;
  74. }
  75. for (int i = 0; i < rxsize; i++) {
  76. ble_uart_init_struct.rxbuf[ble_rx_buffer_now_len++] = rxmessage[i];
  77. ble_rx_buffer_off++;
  78. start_time = esp_log_timestamp();
  79. }
  80. }
  81. void buffer_all_init() {
  82. ble_rx_buffer_now_len = 0;
  83. memset(ble_uart_init_struct.rxbuf, 0, ble_uart_init_struct.rxbufsize);
  84. }
  85. void wait_ble_uart_receive_ms(uint16_t wait_time_ms) {
  86. start_time = esp_log_timestamp();
  87. while ((esp_log_timestamp() - start_time) < wait_time_ms) {
  88. }
  89. }
  90. bool parse_rxbuffer_and_validation_data(cJSON **json_tmp) {
  91. *json_tmp = cJSON_Parse((char *)ble_uart_init_struct.rxbuf);
  92. if (*json_tmp == NULL) {
  93. ESP_LOGE(MAIN_LOG_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(MAIN_LOG_TAG, "%s", ch->string);
  102. if (strcmp(ch->string, "order") == 0) {
  103. ble_uart_receive_data.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. ble_uart_receive_data.index = ch->valueint;
  120. cmd_length--;
  121. }
  122. if (strcmp(ch->string, "speedLevel") == 0) {
  123. ble_uart_receive_data.speedLevel = ch->valueint;
  124. cmd_length--;
  125. }
  126. if (strcmp(ch->string, "position") == 0) {
  127. ble_uart_receive_data.position = ch->valuedouble;
  128. cmd_length--;
  129. }
  130. if (strcmp(ch->string, "direction") == 0) {
  131. ble_uart_receive_data.direction = ch->valueint;
  132. cmd_length--;
  133. }
  134. ch = ch->next;
  135. }
  136. if (cmd_length == 0) {
  137. ble_uart_receive_data.cmd_flag = true;
  138. } else {
  139. ESP_LOGE(MAIN_LOG_TAG, "JSON directive missing or exceeded");
  140. }
  141. return ble_uart_receive_data.cmd_flag;
  142. }
  143. void processing_uart_rx_data() {
  144. if (ble_rx_buffer_off != 0) {
  145. modbus_uart_rx_off_before = ble_rx_buffer_off;
  146. wait_ble_uart_receive_ms(100);
  147. if (ble_rx_buffer_off == modbus_uart_rx_off_before) {
  148. ble_uart_init_struct.receive_data_processing_flag = true;
  149. ESP_LOGI("MAIN", "%s", ble_uart_init_struct.rxbuf);
  150. if (parse_rxbuffer_and_validation_data(&json_tmp)) {
  151. if (parse_json_to_struct(json_tmp->child)) {
  152. ESP_LOGI(MAIN_LOG_TAG, "order:%s ,index:%d speedLevel:%d position:%f direction:%d", ble_uart_receive_data.order, ble_uart_receive_data.index, ble_uart_receive_data.speedLevel,
  153. ble_uart_receive_data.position, ble_uart_receive_data.direction);
  154. // if (strcmp(ble_uart_receive_data.order, set_position) == 0) {
  155. // // ble_uart_receive_data.auto_report_flag = true;
  156. // ESP_LOGI(MAIN_LOG_TAG, set_position);
  157. // encoder_befor_num = motor_drive_read_encoder();
  158. // if (encoder_befor_num >= 0) {
  159. // if (motor_drive_set_packages_ctr(ble_uart_receive_data.position, ble_uart_receive_data.direction) == 0) {
  160. // ets_delay_us(50000);
  161. // if (encoder_befor_num == motor_drive_read_encoder()) {
  162. // ESP_LOGW(MAIN_LOG_TAG, "motor no turning");
  163. // } else {
  164. // ESP_LOGI(MAIN_LOG_TAG, "motor turning");
  165. // }
  166. // }
  167. // }
  168. // // receipt_json_set_position();
  169. // }
  170. // if (strcmp(ble_uart_receive_data.order, get_status) == 0) {
  171. // ESP_LOGI(MAIN_LOG_TAG, get_status);
  172. // receipt_json_get_status();
  173. // }
  174. // if (strcmp(ble_uart_receive_data.order, set_motor_current_size) == 0) {
  175. // ESP_LOGI(MAIN_LOG_TAG, set_motor_current_size);
  176. // motor_drive_set_motor_current_size();
  177. // // receipt_json_get_status();
  178. // }
  179. // if (strcmp(ble_uart_receive_data.order, set_motor_to_position) == 0) {
  180. // ESP_LOGI(MAIN_LOG_TAG, set_motor_to_position);
  181. // motor_drive_set_motor_to_angle(ble_uart_receive_data.direction, ble_uart_receive_data.position, //
  182. // ble_uart_receive_data.speedLevel);
  183. // // receipt_json_get_status();
  184. // }
  185. }
  186. }
  187. }
  188. if (ble_uart_init_struct.receive_data_processing_flag == true) {
  189. buffer_all_init();
  190. ble_uart_init_struct.receive_data_processing_flag = false;
  191. ble_rx_buffer_off = 0;
  192. }
  193. }
  194. }
  195. void app_main(void) {
  196. ble_spp_server_demo_app_main(&ble_uart_init_struct);
  197. bleuart_reg_cb(blerxcb);
  198. while (true) {
  199. processing_uart_rx_data();
  200. }
  201. }