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

268 lines
7.9 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. #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 order_receipt_setMotorCurrentSize(int index) {
  82. ESP_LOGI(MAIN_LOG_TAG, "Command setMotorCurrentSize execution complete!");
  83. uint8_t buffer[256] = {0};
  84. sprintf((char *)buffer, "{ \"order\": \"receipt\", \"index\": %d }", index);
  85. bleuart_notify_send(buffer, strlen((char *)buffer));
  86. }
  87. void process_setPosition(cJSON *rxjson) {
  88. // { "order": "setPosition", "ble_uart_index": 0, "speedLevel": 1, "position": 120.3, "direction": 0 }
  89. int index = 0;
  90. int speedLevel = 0;
  91. float position = 0;
  92. int direction = 0;
  93. uint8_t setPosition_code;
  94. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "position"))) {
  95. position = cJSON_GetObjectItem(rxjson, "position")->valuedouble;
  96. }
  97. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "index"))) {
  98. index = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "index"));
  99. }
  100. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "speedLevel"))) {
  101. speedLevel = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "speedLevel"));
  102. }
  103. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "direction"))) {
  104. direction = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "direction"));
  105. }
  106. ESP_LOGI(MAIN_LOG_TAG, "index = %d,speedLevel = %d,position = %lf,direction = %d", index, speedLevel, position, direction);
  107. setPosition_code = motor_run_to_postion(1, position, 0);
  108. order_receipt_setPosition(index, setPosition_code);
  109. }
  110. void process_getStatus(cJSON *rxjson) {
  111. int index = 0;
  112. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "index"))) {
  113. index = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "index"));
  114. }
  115. order_receipt_getStatus(index);
  116. }
  117. void process_setMotorCurrentSize(cJSON *rxjson) {
  118. int index = 0;
  119. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "index"))) {
  120. index = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "index"));
  121. }
  122. motor_set_zero_point();
  123. order_receipt_setMotorCurrentSize(index);
  124. }
  125. void processrxjson(cJSON *rxjson) {
  126. if (!cJSON_IsString(cJSON_GetObjectItem(rxjson, "order"))) {
  127. return;
  128. }
  129. const char *order = cJSON_GetStringValue(cJSON_GetObjectItem(rxjson, "order"));
  130. if (strcmp(order, "setPosition") == 0) {
  131. process_setPosition(rxjson);
  132. } else if (strcmp(order, "getStatus") == 0) {
  133. process_getStatus(rxjson);
  134. } else if (strcmp(order, "setMotorCurrentSize") == 0) {
  135. process_setMotorCurrentSize(rxjson);
  136. }
  137. }
  138. void blerxcb(uint8_t *rxmessage, size_t rxsize) {
  139. rxmessage[rxsize] = 0;
  140. cJSON *rxmessage_json = cJSON_ParseWithLength((char *)rxmessage, rxsize);
  141. if (rxmessage_json == NULL) {
  142. ESP_LOGE(MAIN_LOG_TAG, "cJSON_Parse fail rxmessage_json == null,%s", cJSON_GetErrorPtr());
  143. return;
  144. }
  145. processrxjson(rxmessage_json);
  146. cJSON_Delete(rxmessage_json);
  147. }
  148. void motor_on_event(motor_event_t event) {
  149. //打算使用sprintf
  150. if (event == kRunToPosition) {
  151. ESP_LOGI(MAIN_LOG_TAG, "kRunToPosition ok!");
  152. uint8_t buffer[256] = {0};
  153. double motor_stop_position = motor_get_position_degree();
  154. //{ "order": "deviceStatusReport", "ble_uart_index": 0, "deviceState": "running", "position":13.9, "deviceException": 0, "deviceExceptionInfo": ""}
  155. sprintf((char *)buffer,
  156. "{ \"order\": \"deviceStatusReport\", \"index\": %d, \"deviceState\": \"idel\", \"position\":%.2lf, \"deviceException\": 0, \"deviceExceptionInfo\": \"\" }", //
  157. 1, motor_stop_position);
  158. bleuart_notify_send(buffer, strlen((char *)buffer));
  159. }
  160. }
  161. #if SUPPORT_KEY_INT
  162. void gpio_input_key_init() {
  163. gpio_config_t gpio_grb_led_structer;
  164. gpio_grb_led_structer.intr_type = GPIO_INTR_DISABLE;
  165. gpio_grb_led_structer.mode = GPIO_MODE_INPUT;
  166. gpio_grb_led_structer.pin_bit_mask = GPIO_KEY_INPUT_PIN_SEL;
  167. gpio_grb_led_structer.pull_down_en = 0;
  168. gpio_grb_led_structer.pull_up_en = 0;
  169. gpio_config(&gpio_grb_led_structer);
  170. }
  171. #endif
  172. #if SUPPORT_ELECTRIC_RELAY
  173. void gpio_electric_relay_init() {
  174. gpio_config_t gpio_grb_led_structer;
  175. gpio_grb_led_structer.intr_type = GPIO_INTR_DISABLE;
  176. gpio_grb_led_structer.mode = GPIO_MODE_OUTPUT;
  177. gpio_grb_led_structer.pin_bit_mask = GPIO_ELECTRIC_RELAY_OUTPUT_PIN_SEL;
  178. gpio_grb_led_structer.pull_down_en = 0;
  179. gpio_grb_led_structer.pull_up_en = 0;
  180. gpio_config(&gpio_grb_led_structer);
  181. }
  182. #endif
  183. #if SUPPORT_DEBUG_LIGHT
  184. void gpio_output_debug_light_init() {
  185. gpio_config_t gpio_grb_led_structer;
  186. gpio_grb_led_structer.intr_type = GPIO_INTR_DISABLE;
  187. gpio_grb_led_structer.mode = GPIO_MODE_INPUT_OUTPUT;
  188. gpio_grb_led_structer.pin_bit_mask = GPIO_DEBUG_LIGHT_OUTPUT_PIN_SEL;
  189. gpio_grb_led_structer.pull_down_en = 0;
  190. gpio_grb_led_structer.pull_up_en = 0;
  191. gpio_config(&gpio_grb_led_structer);
  192. }
  193. void port_do_debug_light_state() {
  194. static uint32_t debug_light_time;
  195. if (port_haspassedms(debug_light_time) > 500) {
  196. gpio_set_level(DEBUG_LIGHT, !gpio_get_level(DEBUG_LIGHT));
  197. debug_light_time = port_get_ticket();
  198. }
  199. }
  200. #endif
  201. void app_main(void) {
  202. bleuart_init(&ble_uart_init_struct);
  203. bleuart_reg_cb(blerxcb);
  204. motor_init(&ble_uart_motor_structer);
  205. motor_reg_event_cb(motor_on_event);
  206. #if SUPPORT_DEBUG_LIGHT
  207. gpio_output_debug_light_init();
  208. #endif
  209. while (true) {
  210. #if SUPPORT_DEBUG_LIGHT
  211. port_do_debug_light_state();
  212. #endif
  213. bleuart_schedule();
  214. motor_module_schedule();
  215. }
  216. }