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

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