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

108 lines
3.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
  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. bleuart_t ble_uart_init_struct = {
  22. .txbuf = bluetooth_tx_buffer,
  23. .txbufsize = sizeof(bluetooth_tx_buffer),
  24. .rxbuf = bluetooth_rx_buffer,
  25. .rxbufsize = sizeof(bluetooth_rx_buffer),
  26. .rxpacketReceiveOvertime = 200,
  27. .receive_data_processing_flag = false,
  28. .maxClientNum = 1,
  29. .bleName = "yimei_ble",
  30. };
  31. motor_t ble_uart_motor_structer = {.uartNum = UART_NUM_1};
  32. void process_setPosition(cJSON *rxjson) {
  33. // { "order": "setPosition", "ble_uart_index": 0, "speedLevel": 1, "position": 120.3, "direction": 0 }
  34. int index = 0;
  35. int speedLevel = 0;
  36. float position = 0;
  37. int direction = 0;
  38. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "position"))) {
  39. position = cJSON_GetObjectItem(rxjson, "position")->valuedouble;
  40. }
  41. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "index"))) {
  42. index = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "index"));
  43. }
  44. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "speedLevel"))) {
  45. speedLevel = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "speedLevel"));
  46. }
  47. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "direction"))) {
  48. direction = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "direction"));
  49. }
  50. motor_run_to_postion(1, position, 1);
  51. }
  52. void processrxjson(cJSON *rxjson) {
  53. if (!cJSON_IsString(cJSON_GetObjectItem(rxjson, "order"))) {
  54. return;
  55. }
  56. const char *order = cJSON_GetStringValue(cJSON_GetObjectItem(rxjson, "order"));
  57. if (strcmp(order, "setPosition") == 0) {
  58. process_setPosition(rxjson);
  59. } else if (strcmp(order, "getStatus") == 0) {
  60. }
  61. }
  62. void blerxcb(uint8_t *rxmessage, size_t rxsize) {
  63. rxmessage[rxsize] = 0;
  64. cJSON *rxmessage_json = cJSON_ParseWithLength((char *)rxmessage, rxsize);
  65. if (rxmessage_json == NULL) {
  66. ESP_LOGE(MAIN_LOG_TAG, "cJSON_Parse fail rxmessage_json == null,%s", cJSON_GetErrorPtr());
  67. return;
  68. }
  69. processrxjson(rxmessage_json);
  70. cJSON_Delete(rxmessage_json);
  71. }
  72. void motor_on_event(motor_event_t event) {
  73. //打算使用sprintf
  74. if (event == kRunToPosition) {
  75. ESP_LOGI(MAIN_LOG_TAG, "kRunToPosition ok!");
  76. uint8_t buffer[128] = {0};
  77. //组包
  78. //上报
  79. //{ "order": "deviceStatusReport", "ble_uart_index": 0, "deviceState": "running", "position":13.9, "deviceException": 0, "deviceExceptionInfo": "", }
  80. sprintf((char *)buffer, "{ \"order\": \"receipt\", \"code\": 0, \"info\": \"success\", \"index\": %d }", 1);
  81. bleuart_notify_send(buffer,sizeof(buffer));
  82. }
  83. }
  84. void app_main(void) {
  85. bleuart_init(&ble_uart_init_struct);
  86. bleuart_reg_cb(blerxcb);
  87. motor_init(&ble_uart_motor_structer);
  88. motor_reg_event_cb(motor_on_event);
  89. while (true) {
  90. bleuart_schedule();
  91. motor_module_schedule();
  92. }
  93. }