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

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