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

199 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
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. #include "port.h"
  17. #define MAIN_LOG_TAG "MAIN"
  18. #define ble_uart_tx_size 128
  19. #define ble_uart_rx_size 128
  20. #define construct_cmd_packet_buffer_size 256
  21. typedef struct {
  22. int index;
  23. const char *order;
  24. } RxContext_t;
  25. static uint8_t bluetooth_tx_buffer[ble_uart_tx_size] = {0};
  26. static uint8_t bluetooth_rx_buffer[ble_uart_rx_size] = {0};
  27. bleuart_t ble_uart_init_struct = {
  28. .txbuf = bluetooth_tx_buffer,
  29. .txbufsize = sizeof(bluetooth_tx_buffer),
  30. .rxbuf = bluetooth_rx_buffer,
  31. .rxbufsize = sizeof(bluetooth_rx_buffer),
  32. .rxpacketReceiveOvertime = 200,
  33. .receive_data_processing_flag = false,
  34. .maxClientNum = 1,
  35. .bleName = "yimei_ble",
  36. };
  37. motor_t ble_uart_motor_structer = {.uartNum = UART_NUM_1};
  38. void construct_set_position_packet_and_report(int index, uint8_t setPosition_code) {
  39. uint8_t buffer[construct_cmd_packet_buffer_size] = {0};
  40. ESP_LOGI(MAIN_LOG_TAG, "Command setPosition execution complete!");
  41. if (setPosition_code == 0) {
  42. snprintf((char *)buffer, construct_cmd_packet_buffer_size, "{ \"order\": \"receipt\", \"code\": %d, \"info\": \"success\", \"index\": %d }", setPosition_code, index);
  43. bleuart_notify_send(buffer, strlen((char *)buffer));
  44. } else {
  45. snprintf((char *)buffer, construct_cmd_packet_buffer_size, "{ \"order\": \"receipt\", \"code\": %d, \"info\": \"error\", \"index\": %d }", setPosition_code, index);
  46. bleuart_notify_send(buffer, strlen((char *)buffer));
  47. }
  48. }
  49. void construct_get_status_packet_and_report(int index, double motor_position) {
  50. uint8_t buffer[construct_cmd_packet_buffer_size] = {0};
  51. if (motor_turning == false) {
  52. snprintf((char *)buffer, construct_cmd_packet_buffer_size,
  53. "{ \"order\": \"receipt\", \"index\": %d, \"deviceState\": \"%s\", \"deviceException\": 0, \"deviceExceptionInfo\": "
  54. ", \"position\":%lf }",
  55. index, "idel", motor_position);
  56. } else {
  57. snprintf((char *)buffer, construct_cmd_packet_buffer_size,
  58. "{ \"order\": \"receipt\", \"index\": %d, \"deviceState\": \"%s\", \"deviceException\": 0, \"deviceExceptionInfo\": "
  59. ", \"position\":%lf }",
  60. index, "rotating", motor_position);
  61. }
  62. ESP_LOGI(MAIN_LOG_TAG, "report %s", buffer);
  63. bleuart_notify_send(buffer, strlen((char *)buffer));
  64. }
  65. void construct_set_motorCurrentSize_packet_and_report(int index) {
  66. uint8_t buffer[construct_cmd_packet_buffer_size] = {0};
  67. snprintf((char *)buffer, construct_cmd_packet_buffer_size, "{ \"order\": \"receipt\", \"index\": %d }", index);
  68. ESP_LOGI(MAIN_LOG_TAG, "report %s", buffer);
  69. bleuart_notify_send(buffer, strlen((char *)buffer));
  70. }
  71. void process_setPosition(RxContext_t *context, cJSON *rxjson) {
  72. // { "order": "setPosition", "ble_uart_index": 0, "speedLevel": 1, "position": 120.3, "direction": 0 }
  73. int index = 0;
  74. int speedLevel = 0;
  75. float position = 0;
  76. int direction = 0;
  77. uint8_t setPosition_code;
  78. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "position"))) {
  79. position = cJSON_GetObjectItem(rxjson, "position")->valuedouble;
  80. }
  81. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "index"))) {
  82. index = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "index"));
  83. }
  84. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "speedLevel"))) {
  85. speedLevel = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "speedLevel"));
  86. }
  87. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "direction"))) {
  88. direction = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "direction"));
  89. }
  90. ESP_LOGI(MAIN_LOG_TAG, "index = %d,speedLevel = %d,position = %lf,direction = %d", index, speedLevel, position, direction);
  91. setPosition_code = motor_run_to_postion(1, position, 0);
  92. motor_turning = true;
  93. construct_set_position_packet_and_report(index, setPosition_code);
  94. }
  95. void process_getStatus(cJSON *rxjson) {
  96. int index = 0;
  97. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "index"))) {
  98. index = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "index"));
  99. }
  100. double motor_position = motor_get_position_degree();
  101. construct_get_status_packet_and_report(index, motor_position);
  102. }
  103. void process_setMotorCurrentSize(cJSON *rxjson) {
  104. int index = 0;
  105. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "index"))) {
  106. index = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "index"));
  107. }
  108. motor_set_zero_point();
  109. construct_set_motorCurrentSize_packet_and_report(index);
  110. }
  111. void processrxjson(cJSON *rxjson) {
  112. if (!cJSON_IsString(cJSON_GetObjectItem(rxjson, "order"))) {
  113. return;
  114. }
  115. RxContext_t rxcontext = {0};
  116. if (cJSON_IsString(cJSON_GetObjectItem(rxjson, "order"))) {
  117. rxcontext.order = cJSON_GetStringValue(cJSON_GetObjectItem(rxjson, "order"));
  118. }
  119. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "index"))) {
  120. rxcontext.index = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "index"));
  121. }
  122. if (strcmp(rxcontext.order, "setPosition") == 0) {
  123. process_setPosition(&rxcontext, rxjson);
  124. } else if (strcmp(rxcontext.order, "getStatus") == 0) {
  125. process_getStatus(rxjson);
  126. } else if (strcmp(rxcontext.order, "setMotorCurrentSize") == 0) {
  127. process_setMotorCurrentSize(rxjson);
  128. }
  129. }
  130. void blerxcb(uint8_t *rxmessage, size_t rxsize) {
  131. rxmessage[rxsize] = 0;
  132. cJSON *rxmessage_json = cJSON_ParseWithLength((char *)rxmessage, rxsize);
  133. if (rxmessage_json == NULL) {
  134. ESP_LOGE(MAIN_LOG_TAG, "cJSON_Parse fail rxmessage_json == null,%s", cJSON_GetErrorPtr());
  135. return;
  136. }
  137. processrxjson(rxmessage_json);
  138. cJSON_Delete(rxmessage_json);
  139. }
  140. void motor_on_event(motor_event_t event) {
  141. //打算使用sprintf
  142. if (event == kRunToPosition) {
  143. ESP_LOGI(MAIN_LOG_TAG, "motor_on_event: kRunToPosition");
  144. motor_turning = false;
  145. uint8_t buffer[construct_cmd_packet_buffer_size] = {0};
  146. double motor_stop_position = motor_get_position_degree();
  147. //{ "order": "deviceStatusReport", "ble_uart_index": 0, "deviceState": "running", "position":13.9, "deviceException": 0, "deviceExceptionInfo": ""}
  148. snprintf((char *)buffer, construct_cmd_packet_buffer_size,
  149. "{ \"order\": \"deviceStatusReport\", \"index\": %d, \"deviceState\": \"idel\", \"position\":%.2lf, \"deviceException\": 0, \"deviceExceptionInfo\": \"\" }", //
  150. /*index :*/ 1, //
  151. /*position :*/ motor_stop_position //
  152. );
  153. bleuart_notify_send(buffer, strlen((char *)buffer));
  154. }
  155. }
  156. void app_main(void) {
  157. bleuart_init(&ble_uart_init_struct);
  158. bleuart_reg_cb(blerxcb);
  159. ble_set_motor_reg_cb(motor_run_to_postion);
  160. motor_init(&ble_uart_motor_structer);
  161. motor_reg_event_cb(motor_on_event);
  162. gpio_output_debug_light_init();
  163. while (true) {
  164. port_do_debug_light_state();
  165. bleuart_schedule();
  166. motor_module_schedule();
  167. }
  168. }