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

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