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

203 lines
5.6 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
  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 SUPPORT_KEY_INT 0
  19. #define SUPPORT_ELECTRIC_RELAY 0
  20. #define SUPPORT_DEBUG_LIGHT 1
  21. #define ble_uart_tx_size 128
  22. #define ble_uart_rx_size 128
  23. #if SUPPORT_DEBUG_LIGHT
  24. #define DEBUG_LIGHT 12
  25. #define GPIO_DEBUG_LIGHT_OUTPUT_PIN_SEL ((1ULL << DEBUG_LIGHT))
  26. #endif
  27. #if SUPPORT_KEY_INT
  28. #define KEY_INT1 27
  29. #define KEY_INT2 14
  30. #define GPIO_KEY_INPUT_PIN_SEL ((1ULL << KEY_INT1) | (1ULL << KEY_INT2))
  31. #endif
  32. #if 0 //预留
  33. #define TEMPER_LINE 18 //单总线温度传感器
  34. #define FAN 19 //风扇控制
  35. #endif
  36. #if SUPPORT_ELECTRIC_RELAY
  37. #define ELECTRIC_RELAY1 25
  38. #define ELECTRIC_RELAY2 26
  39. #define GPIO_ELECTRIC_RELAY_OUTPUT_PIN_SEL ((1ULL << ELECTRIC_RELAY1) | (1ULL << ELECTRIC_RELAY2))
  40. #endif
  41. static uint8_t bluetooth_tx_buffer[ble_uart_tx_size] = {0};
  42. static uint8_t bluetooth_rx_buffer[ble_uart_rx_size] = {0};
  43. bleuart_t ble_uart_init_struct = {
  44. .txbuf = bluetooth_tx_buffer,
  45. .txbufsize = sizeof(bluetooth_tx_buffer),
  46. .rxbuf = bluetooth_rx_buffer,
  47. .rxbufsize = sizeof(bluetooth_rx_buffer),
  48. .rxpacketReceiveOvertime = 200,
  49. .receive_data_processing_flag = false,
  50. .maxClientNum = 1,
  51. .bleName = "yimei_ble",
  52. };
  53. motor_t ble_uart_motor_structer = {.uartNum = UART_NUM_1};
  54. void process_setPosition(cJSON *rxjson) {
  55. // { "order": "setPosition", "ble_uart_index": 0, "speedLevel": 1, "position": 120.3, "direction": 0 }
  56. int index = 0;
  57. int speedLevel = 0;
  58. float position = 0;
  59. int direction = 0;
  60. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "position"))) {
  61. position = cJSON_GetObjectItem(rxjson, "position")->valuedouble;
  62. }
  63. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "index"))) {
  64. index = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "index"));
  65. }
  66. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "speedLevel"))) {
  67. speedLevel = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "speedLevel"));
  68. }
  69. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "direction"))) {
  70. direction = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "direction"));
  71. }
  72. motor_run_to_postion(1, position, 0);
  73. }
  74. void processrxjson(cJSON *rxjson) {
  75. if (!cJSON_IsString(cJSON_GetObjectItem(rxjson, "order"))) {
  76. return;
  77. }
  78. const char *order = cJSON_GetStringValue(cJSON_GetObjectItem(rxjson, "order"));
  79. if (strcmp(order, "setPosition") == 0) {
  80. process_setPosition(rxjson);
  81. } else if (strcmp(order, "getStatus") == 0) {
  82. }
  83. }
  84. void blerxcb(uint8_t *rxmessage, size_t rxsize) {
  85. rxmessage[rxsize] = 0;
  86. cJSON *rxmessage_json = cJSON_ParseWithLength((char *)rxmessage, rxsize);
  87. if (rxmessage_json == NULL) {
  88. ESP_LOGE(MAIN_LOG_TAG, "cJSON_Parse fail rxmessage_json == null,%s", cJSON_GetErrorPtr());
  89. return;
  90. }
  91. processrxjson(rxmessage_json);
  92. cJSON_Delete(rxmessage_json);
  93. }
  94. void motor_on_event(motor_event_t event) {
  95. //打算使用sprintf
  96. if (event == kRunToPosition) {
  97. ESP_LOGI(MAIN_LOG_TAG, "kRunToPosition ok!");
  98. uint8_t buffer[256] = {0};
  99. double motor_stop_position = motor_get_position_degree();
  100. //{ "order": "deviceStatusReport", "ble_uart_index": 0, "deviceState": "running", "position":13.9, "deviceException": 0, "deviceExceptionInfo": ""}
  101. sprintf((char *)buffer,
  102. "{ \"order\": \"deviceStatusReport\", \"index\": %d, \"deviceState\": \"idel\", \"position\":%.2lf, \"deviceException\": 0, \"deviceExceptionInfo\": \"\" }", //
  103. 1, motor_stop_position);
  104. bleuart_notify_send(buffer, strlen((char *)buffer));
  105. }
  106. }
  107. #if SUPPORT_KEY_INT
  108. void gpio_input_key_init() {
  109. gpio_config_t gpio_grb_led_structer;
  110. gpio_grb_led_structer.intr_type = GPIO_INTR_DISABLE;
  111. gpio_grb_led_structer.mode = GPIO_MODE_INPUT;
  112. gpio_grb_led_structer.pin_bit_mask = GPIO_KEY_INPUT_PIN_SEL;
  113. gpio_grb_led_structer.pull_down_en = 0;
  114. gpio_grb_led_structer.pull_up_en = 0;
  115. gpio_config(&gpio_grb_led_structer);
  116. }
  117. #endif
  118. #if SUPPORT_ELECTRIC_RELAY
  119. void gpio_electric_relay_init() {
  120. gpio_config_t gpio_grb_led_structer;
  121. gpio_grb_led_structer.intr_type = GPIO_INTR_DISABLE;
  122. gpio_grb_led_structer.mode = GPIO_MODE_OUTPUT;
  123. gpio_grb_led_structer.pin_bit_mask = GPIO_ELECTRIC_RELAY_OUTPUT_PIN_SEL;
  124. gpio_grb_led_structer.pull_down_en = 0;
  125. gpio_grb_led_structer.pull_up_en = 0;
  126. gpio_config(&gpio_grb_led_structer);
  127. }
  128. #endif
  129. #if SUPPORT_DEBUG_LIGHT
  130. void gpio_output_debug_light_init() {
  131. gpio_config_t gpio_grb_led_structer;
  132. gpio_grb_led_structer.intr_type = GPIO_INTR_DISABLE;
  133. gpio_grb_led_structer.mode = GPIO_MODE_INPUT_OUTPUT;
  134. gpio_grb_led_structer.pin_bit_mask = GPIO_DEBUG_LIGHT_OUTPUT_PIN_SEL;
  135. gpio_grb_led_structer.pull_down_en = 0;
  136. gpio_grb_led_structer.pull_up_en = 0;
  137. gpio_config(&gpio_grb_led_structer);
  138. }
  139. void port_do_debug_light_state() {
  140. static uint32_t debug_light_time;
  141. if (port_haspassedms(debug_light_time) > 500) {
  142. gpio_set_level(DEBUG_LIGHT, !gpio_get_level(DEBUG_LIGHT));
  143. debug_light_time = port_get_ticket();
  144. }
  145. }
  146. #endif
  147. void app_main(void) {
  148. bleuart_init(&ble_uart_init_struct);
  149. bleuart_reg_cb(blerxcb);
  150. motor_init(&ble_uart_motor_structer);
  151. motor_reg_event_cb(motor_on_event);
  152. #if SUPPORT_DEBUG_LIGHT
  153. gpio_output_debug_light_init();
  154. #endif
  155. while (true) {
  156. #if SUPPORT_DEBUG_LIGHT
  157. port_do_debug_light_state();
  158. #endif
  159. bleuart_schedule();
  160. motor_module_schedule();
  161. }
  162. }