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

247 lines
7.1 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
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 order_receipt_setPosition(int index, uint8_t setPosition_code) {
  61. uint8_t buffer[256] = {0};
  62. ESP_LOGI(MAIN_LOG_TAG, "Command setPosition execution complete!");
  63. if (setPosition_code == 0) {
  64. sprintf((char *)buffer, "{ \"order\": \"receipt\", \"code\": %d, \"info\": \"success\", \"index\": %d }", setPosition_code, index);
  65. bleuart_notify_send(buffer, strlen((char *)buffer));
  66. } else {
  67. sprintf((char *)buffer, "{ \"order\": \"receipt\", \"code\": %d, \"info\": \"error\", \"index\": %d }", setPosition_code, index);
  68. bleuart_notify_send(buffer, strlen((char *)buffer));
  69. }
  70. }
  71. void order_receipt_getStatus(int index) {
  72. ESP_LOGI(MAIN_LOG_TAG, "Command getStatus execution complete!");
  73. uint8_t buffer[256] = {0};
  74. double motor_position = motor_get_position_degree();
  75. sprintf((char *)buffer,
  76. "{ \"order\": \"receipt\", \"index\": %d, \"deviceState\": \"running\", \"deviceException\": 0, \"deviceExceptionInfo\": "
  77. ", \"position\":%lf }",
  78. index, motor_position);
  79. bleuart_notify_send(buffer, strlen((char *)buffer));
  80. }
  81. void process_setPosition(cJSON *rxjson) {
  82. // { "order": "setPosition", "ble_uart_index": 0, "speedLevel": 1, "position": 120.3, "direction": 0 }
  83. int index = 0;
  84. int speedLevel = 0;
  85. float position = 0;
  86. int direction = 0;
  87. uint8_t setPosition_code;
  88. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "position"))) {
  89. position = cJSON_GetObjectItem(rxjson, "position")->valuedouble;
  90. }
  91. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "index"))) {
  92. index = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "index"));
  93. }
  94. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "speedLevel"))) {
  95. speedLevel = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "speedLevel"));
  96. }
  97. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "direction"))) {
  98. direction = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "direction"));
  99. }
  100. setPosition_code = motor_run_to_postion(1, position, 0);
  101. order_receipt_setPosition(index, setPosition_code);
  102. }
  103. void process_getStatus(cJSON *rxjson) {
  104. int index = 0;
  105. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "index"))) {
  106. index = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "index"));
  107. }
  108. order_receipt_getStatus(index);
  109. }
  110. void processrxjson(cJSON *rxjson) {
  111. if (!cJSON_IsString(cJSON_GetObjectItem(rxjson, "order"))) {
  112. return;
  113. }
  114. const char *order = cJSON_GetStringValue(cJSON_GetObjectItem(rxjson, "order"));
  115. if (strcmp(order, "setPosition") == 0) {
  116. process_setPosition(rxjson);
  117. } else if (strcmp(order, "getStatus") == 0) {
  118. process_getStatus(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, "kRunToPosition ok!");
  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. 1, motor_stop_position);
  141. bleuart_notify_send(buffer, strlen((char *)buffer));
  142. }
  143. }
  144. #if SUPPORT_KEY_INT
  145. void gpio_input_key_init() {
  146. gpio_config_t gpio_grb_led_structer;
  147. gpio_grb_led_structer.intr_type = GPIO_INTR_DISABLE;
  148. gpio_grb_led_structer.mode = GPIO_MODE_INPUT;
  149. gpio_grb_led_structer.pin_bit_mask = GPIO_KEY_INPUT_PIN_SEL;
  150. gpio_grb_led_structer.pull_down_en = 0;
  151. gpio_grb_led_structer.pull_up_en = 0;
  152. gpio_config(&gpio_grb_led_structer);
  153. }
  154. #endif
  155. #if SUPPORT_ELECTRIC_RELAY
  156. void gpio_electric_relay_init() {
  157. gpio_config_t gpio_grb_led_structer;
  158. gpio_grb_led_structer.intr_type = GPIO_INTR_DISABLE;
  159. gpio_grb_led_structer.mode = GPIO_MODE_OUTPUT;
  160. gpio_grb_led_structer.pin_bit_mask = GPIO_ELECTRIC_RELAY_OUTPUT_PIN_SEL;
  161. gpio_grb_led_structer.pull_down_en = 0;
  162. gpio_grb_led_structer.pull_up_en = 0;
  163. gpio_config(&gpio_grb_led_structer);
  164. }
  165. #endif
  166. #if SUPPORT_DEBUG_LIGHT
  167. void gpio_output_debug_light_init() {
  168. gpio_config_t gpio_grb_led_structer;
  169. gpio_grb_led_structer.intr_type = GPIO_INTR_DISABLE;
  170. gpio_grb_led_structer.mode = GPIO_MODE_INPUT_OUTPUT;
  171. gpio_grb_led_structer.pin_bit_mask = GPIO_DEBUG_LIGHT_OUTPUT_PIN_SEL;
  172. gpio_grb_led_structer.pull_down_en = 0;
  173. gpio_grb_led_structer.pull_up_en = 0;
  174. gpio_config(&gpio_grb_led_structer);
  175. }
  176. void port_do_debug_light_state() {
  177. static uint32_t debug_light_time;
  178. if (port_haspassedms(debug_light_time) > 500) {
  179. gpio_set_level(DEBUG_LIGHT, !gpio_get_level(DEBUG_LIGHT));
  180. debug_light_time = port_get_ticket();
  181. }
  182. }
  183. #endif
  184. void app_main(void) {
  185. bleuart_init(&ble_uart_init_struct);
  186. bleuart_reg_cb(blerxcb);
  187. motor_init(&ble_uart_motor_structer);
  188. motor_reg_event_cb(motor_on_event);
  189. #if SUPPORT_DEBUG_LIGHT
  190. gpio_output_debug_light_init();
  191. #endif
  192. while (true) {
  193. #if SUPPORT_DEBUG_LIGHT
  194. port_do_debug_light_state();
  195. #endif
  196. bleuart_schedule();
  197. motor_module_schedule();
  198. }
  199. }