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

147 lines
4.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
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 KEY_INT1 27
  21. #define KEY_INT2 14
  22. #define DEBUG_LIGHT 12
  23. #define GPIO_KEY_INPUT_PIN_SEL ((1ULL << KEY_INT1) | (1ULL << KEY_INT2))
  24. #define GPIO_DEBUG_LIGHT_OUTPUT_PIN_SEL ((1ULL << DEBUG_LIGHT))
  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 process_setPosition(cJSON *rxjson) {
  39. // { "order": "setPosition", "ble_uart_index": 0, "speedLevel": 1, "position": 120.3, "direction": 0 }
  40. int index = 0;
  41. int speedLevel = 0;
  42. float position = 0;
  43. int direction = 0;
  44. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "position"))) {
  45. position = cJSON_GetObjectItem(rxjson, "position")->valuedouble;
  46. }
  47. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "index"))) {
  48. index = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "index"));
  49. }
  50. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "speedLevel"))) {
  51. speedLevel = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "speedLevel"));
  52. }
  53. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "direction"))) {
  54. direction = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "direction"));
  55. }
  56. motor_run_to_postion(1, position, 0);
  57. }
  58. void processrxjson(cJSON *rxjson) {
  59. if (!cJSON_IsString(cJSON_GetObjectItem(rxjson, "order"))) {
  60. return;
  61. }
  62. const char *order = cJSON_GetStringValue(cJSON_GetObjectItem(rxjson, "order"));
  63. if (strcmp(order, "setPosition") == 0) {
  64. process_setPosition(rxjson);
  65. } else if (strcmp(order, "getStatus") == 0) {
  66. }
  67. }
  68. void blerxcb(uint8_t *rxmessage, size_t rxsize) {
  69. rxmessage[rxsize] = 0;
  70. cJSON *rxmessage_json = cJSON_ParseWithLength((char *)rxmessage, rxsize);
  71. if (rxmessage_json == NULL) {
  72. ESP_LOGE(MAIN_LOG_TAG, "cJSON_Parse fail rxmessage_json == null,%s", cJSON_GetErrorPtr());
  73. return;
  74. }
  75. processrxjson(rxmessage_json);
  76. cJSON_Delete(rxmessage_json);
  77. }
  78. void motor_on_event(motor_event_t event) {
  79. //打算使用sprintf
  80. if (event == kRunToPosition) {
  81. ESP_LOGI(MAIN_LOG_TAG, "kRunToPosition ok!");
  82. uint8_t buffer[128] = {0};
  83. //组包
  84. //上报
  85. //{ "order": "deviceStatusReport", "ble_uart_index": 0, "deviceState": "running", "position":13.9, "deviceException": 0, "deviceExceptionInfo": "", }
  86. sprintf((char *)buffer, "{ \"order\": \"receipt\", \"code\": 0, \"info\": \"success\", \"index\": %d }", 1);
  87. bleuart_notify_send(buffer, strlen((char *)buffer));
  88. }
  89. }
  90. void gpio_input_key_init() {
  91. gpio_config_t gpio_grb_led_structer;
  92. gpio_grb_led_structer.intr_type = GPIO_INTR_DISABLE;
  93. gpio_grb_led_structer.mode = GPIO_MODE_INPUT;
  94. gpio_grb_led_structer.pin_bit_mask = GPIO_KEY_INPUT_PIN_SEL;
  95. gpio_grb_led_structer.pull_down_en = 0;
  96. gpio_grb_led_structer.pull_up_en = 0;
  97. gpio_config(&gpio_grb_led_structer);
  98. }
  99. void gpio_output_debug_light_init() {
  100. gpio_config_t gpio_grb_led_structer;
  101. gpio_grb_led_structer.intr_type = GPIO_INTR_DISABLE;
  102. gpio_grb_led_structer.mode = GPIO_MODE_INPUT_OUTPUT;
  103. gpio_grb_led_structer.pin_bit_mask = GPIO_DEBUG_LIGHT_OUTPUT_PIN_SEL;
  104. gpio_grb_led_structer.pull_down_en = 0;
  105. gpio_grb_led_structer.pull_up_en = 0;
  106. gpio_config(&gpio_grb_led_structer);
  107. }
  108. void port_do_debug_light_state() {
  109. static uint32_t debug_light_time;
  110. if (port_haspassedms(debug_light_time) > 500) {
  111. gpio_set_level(DEBUG_LIGHT, !gpio_get_level(DEBUG_LIGHT));
  112. debug_light_time = port_get_ticket();
  113. }
  114. }
  115. void app_main(void) {
  116. bleuart_init(&ble_uart_init_struct);
  117. bleuart_reg_cb(blerxcb);
  118. motor_init(&ble_uart_motor_structer);
  119. motor_reg_event_cb(motor_on_event);
  120. gpio_output_debug_light_init();
  121. while (true) {
  122. port_do_debug_light_state();
  123. bleuart_schedule();
  124. motor_module_schedule();
  125. }
  126. }