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

300 lines
9.5 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. /**
  19. * @brief
  20. */
  21. #define SUPPORT_KEY_INT 1 //是否支持按键
  22. #define KEY_INT1 27
  23. #define KEY_INT2 14
  24. // #define GPIO_KEY_INPUT_PIN_SEL ((1ULL << KEY_INT1) | (1ULL << KEY_INT2))
  25. #define SUPPORT_ELECTRIC_RELAY 1
  26. #define SUPPORT_DEBUG_LIGHT 1
  27. #define SUPPORT_TEMPER_LINE 1
  28. #define SUPPORT_FAN 1
  29. #define ble_uart_tx_size 128
  30. #define ble_uart_rx_size 128
  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. #define DEBUG_LIGHT 12
  35. #define GPIO_DEBUG_LIGHT_OUTPUT_PIN_SEL ((1ULL << DEBUG_LIGHT))
  36. #if SUPPORT_TEMPER_LINE
  37. #define TEMPER_LINE 18
  38. #define GPIO_TEMPER_LINE_INPUT_PIN_SEL ((1ULL << TEMPER_LINE))
  39. #endif
  40. #if SUPPORT_FAN
  41. #define FAN 19
  42. #define GPIO_FAN_OUTPUT_PIN_SEL ((1ULL << FAN))
  43. #endif
  44. typedef struct {
  45. int index;
  46. const char *order;
  47. } RxContext_t;
  48. static uint8_t bluetooth_tx_buffer[ble_uart_tx_size] = {0};
  49. static uint8_t bluetooth_rx_buffer[ble_uart_rx_size] = {0};
  50. bleuart_t ble_uart_init_struct = {
  51. .txbuf = bluetooth_tx_buffer,
  52. .txbufsize = sizeof(bluetooth_tx_buffer),
  53. .rxbuf = bluetooth_rx_buffer,
  54. .rxbufsize = sizeof(bluetooth_rx_buffer),
  55. .rxpacketReceiveOvertime = 200,
  56. .receive_data_processing_flag = false,
  57. .maxClientNum = 1,
  58. .bleName = "yimei_ble",
  59. };
  60. motor_t ble_uart_motor_structer = {.uartNum = UART_NUM_1};
  61. void order_receipt_setPosition(int index, uint8_t setPosition_code) {
  62. uint8_t buffer[256] = {0};
  63. ESP_LOGI(MAIN_LOG_TAG, "Command setPosition execution complete!");
  64. if (setPosition_code == 0) {
  65. sprintf((char *)buffer, "{ \"order\": \"receipt\", \"code\": %d, \"info\": \"success\", \"index\": %d }", setPosition_code, index);
  66. bleuart_notify_send(buffer, strlen((char *)buffer));
  67. } else {
  68. sprintf((char *)buffer, "{ \"order\": \"receipt\", \"code\": %d, \"info\": \"error\", \"index\": %d }", setPosition_code, index);
  69. bleuart_notify_send(buffer, strlen((char *)buffer));
  70. }
  71. }
  72. //
  73. // void order_receipt_getStatus(int index) {
  74. // ESP_LOGI(MAIN_LOG_TAG, "Command getStatus execution complete!");
  75. // uint8_t buffer[256] = {0};
  76. // double motor_position = motor_get_position_degree();
  77. // sprintf((char *)buffer,
  78. // "{ \"order\": \"receipt\", \"index\": %d, \"deviceState\": \"running\", \"deviceException\": 0, \"deviceExceptionInfo\": "
  79. // ", \"position\":%lf }",
  80. // index, motor_position);
  81. // bleuart_notify_send(buffer, strlen((char *)buffer));
  82. // }
  83. void construct_get_status_packet_and_report(int index, double motor_position) {
  84. uint8_t buffer[256] = {0};
  85. snprintf((char *)buffer, 256,
  86. "{ \"order\": \"receipt\", \"index\": %d, \"deviceState\": \"running\", \"deviceException\": 0, \"deviceExceptionInfo\": "
  87. ", \"position\":%lf }",
  88. index, motor_position);
  89. ESP_LOGI(MAIN_LOG_TAG, "report %s", buffer);
  90. bleuart_notify_send(buffer, strlen((char *)buffer));
  91. }
  92. void order_receipt_setMotorCurrentSize(int index) {
  93. ESP_LOGI(MAIN_LOG_TAG, "Command setMotorCurrentSize execution complete!");
  94. uint8_t buffer[256] = {0};
  95. sprintf((char *)buffer, "{ \"order\": \"receipt\", \"index\": %d }", index);
  96. bleuart_notify_send(buffer, strlen((char *)buffer));
  97. }
  98. void process_setPosition(RxContext_t *context, cJSON *rxjson) {
  99. // { "order": "setPosition", "ble_uart_index": 0, "speedLevel": 1, "position": 120.3, "direction": 0 }
  100. int index = 0;
  101. int speedLevel = 0;
  102. float position = 0;
  103. int direction = 0;
  104. uint8_t setPosition_code;
  105. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "position"))) {
  106. position = cJSON_GetObjectItem(rxjson, "position")->valuedouble;
  107. }
  108. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "index"))) {
  109. index = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "index"));
  110. }
  111. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "speedLevel"))) {
  112. speedLevel = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "speedLevel"));
  113. }
  114. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "direction"))) {
  115. direction = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "direction"));
  116. }
  117. ESP_LOGI(MAIN_LOG_TAG, "index = %d,speedLevel = %d,position = %lf,direction = %d", index, speedLevel, position, direction);
  118. setPosition_code = motor_run_to_postion(1, position, 0);
  119. order_receipt_setPosition(index, setPosition_code);
  120. }
  121. void process_getStatus(cJSON *rxjson) {
  122. int index = 0;
  123. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "index"))) {
  124. index = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "index"));
  125. }
  126. double motor_position = motor_get_position_degree();
  127. construct_get_status_packet_and_report(index, motor_position);
  128. }
  129. void process_setMotorCurrentSize(cJSON *rxjson) {
  130. int index = 0;
  131. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "index"))) {
  132. index = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "index"));
  133. }
  134. motor_set_zero_point();
  135. order_receipt_setMotorCurrentSize(index);
  136. }
  137. void processrxjson(cJSON *rxjson) {
  138. if (!cJSON_IsString(cJSON_GetObjectItem(rxjson, "order"))) {
  139. return;
  140. }
  141. RxContext_t rxcontext = {0};
  142. if (cJSON_IsString(cJSON_GetObjectItem(rxjson, "order"))) {
  143. rxcontext.order = cJSON_GetStringValue(cJSON_GetObjectItem(rxjson, "order"));
  144. }
  145. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "index"))) {
  146. rxcontext.index = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "index"));
  147. }
  148. if (strcmp(rxcontext.order, "setPosition") == 0) {
  149. process_setPosition(&rxcontext, rxjson);
  150. } else if (strcmp(rxcontext.order, "getStatus") == 0) {
  151. process_getStatus(rxjson);
  152. } else if (strcmp(rxcontext.order, "setMotorCurrentSize") == 0) {
  153. process_setMotorCurrentSize(rxjson);
  154. }
  155. }
  156. void blerxcb(uint8_t *rxmessage, size_t rxsize) {
  157. rxmessage[rxsize] = 0;
  158. cJSON *rxmessage_json = cJSON_ParseWithLength((char *)rxmessage, rxsize);
  159. if (rxmessage_json == NULL) {
  160. ESP_LOGE(MAIN_LOG_TAG, "cJSON_Parse fail rxmessage_json == null,%s", cJSON_GetErrorPtr());
  161. return;
  162. }
  163. processrxjson(rxmessage_json);
  164. cJSON_Delete(rxmessage_json);
  165. }
  166. void motor_on_event(motor_event_t event) {
  167. //打算使用sprintf
  168. if (event == kRunToPosition) {
  169. ESP_LOGI(MAIN_LOG_TAG, "motor_on_event: kRunToPosition");
  170. uint8_t buffer[256] = {0};
  171. double motor_stop_position = motor_get_position_degree();
  172. //{ "order": "deviceStatusReport", "ble_uart_index": 0, "deviceState": "running", "position":13.9, "deviceException": 0, "deviceExceptionInfo": ""}
  173. sprintf((char *)buffer,
  174. "{ \"order\": \"deviceStatusReport\", \"index\": %d, \"deviceState\": \"idel\", \"position\":%.2lf, \"deviceException\": 0, \"deviceExceptionInfo\": \"\" }", //
  175. /*index :*/ 1, //
  176. /*position :*/ motor_stop_position //
  177. );
  178. bleuart_notify_send(buffer, strlen((char *)buffer));
  179. }
  180. }
  181. #if SUPPORT_KEY_INT
  182. void gpio_input_key_init() {
  183. { // Key1
  184. gpio_config_t gpio_grb_led_structer;
  185. gpio_grb_led_structer.intr_type = GPIO_INTR_DISABLE;
  186. gpio_grb_led_structer.mode = GPIO_MODE_INPUT;
  187. gpio_grb_led_structer.pin_bit_mask = 1ULL << KEY_INT1;
  188. gpio_grb_led_structer.pull_down_en = 0;
  189. gpio_grb_led_structer.pull_up_en = 0;
  190. gpio_config(&gpio_grb_led_structer);
  191. }
  192. { // Key2
  193. gpio_config_t gpio_grb_led_structer;
  194. gpio_grb_led_structer.intr_type = GPIO_INTR_DISABLE;
  195. gpio_grb_led_structer.mode = GPIO_MODE_INPUT;
  196. gpio_grb_led_structer.pin_bit_mask = 1ULL << KEY_INT2;
  197. gpio_grb_led_structer.pull_down_en = 0;
  198. gpio_grb_led_structer.pull_up_en = 0;
  199. gpio_config(&gpio_grb_led_structer);
  200. }
  201. }
  202. #endif
  203. #if SUPPORT_ELECTRIC_RELAY
  204. void gpio_electric_relay_init() {
  205. gpio_config_t gpio_grb_led_structer;
  206. gpio_grb_led_structer.intr_type = GPIO_INTR_DISABLE;
  207. gpio_grb_led_structer.mode = GPIO_MODE_OUTPUT;
  208. gpio_grb_led_structer.pin_bit_mask = GPIO_ELECTRIC_RELAY_OUTPUT_PIN_SEL;
  209. gpio_grb_led_structer.pull_down_en = 0;
  210. gpio_grb_led_structer.pull_up_en = 0;
  211. gpio_config(&gpio_grb_led_structer);
  212. }
  213. #endif
  214. #if SUPPORT_DEBUG_LIGHT
  215. void gpio_output_debug_light_init() {
  216. gpio_config_t gpio_grb_led_structer;
  217. gpio_grb_led_structer.intr_type = GPIO_INTR_DISABLE;
  218. gpio_grb_led_structer.mode = GPIO_MODE_INPUT_OUTPUT;
  219. gpio_grb_led_structer.pin_bit_mask = GPIO_DEBUG_LIGHT_OUTPUT_PIN_SEL;
  220. gpio_grb_led_structer.pull_down_en = 0;
  221. gpio_grb_led_structer.pull_up_en = 0;
  222. gpio_config(&gpio_grb_led_structer);
  223. }
  224. void port_do_debug_light_state() {
  225. static uint32_t debug_light_time;
  226. if (port_haspassedms(debug_light_time) > 500) {
  227. gpio_set_level(DEBUG_LIGHT, !gpio_get_level(DEBUG_LIGHT));
  228. debug_light_time = port_get_ticket();
  229. }
  230. }
  231. #endif
  232. void app_main(void) {
  233. bleuart_init(&ble_uart_init_struct);
  234. bleuart_reg_cb(blerxcb);
  235. ble_set_motor_reg_cb(motor_run_to_postion);
  236. motor_init(&ble_uart_motor_structer);
  237. motor_reg_event_cb(motor_on_event);
  238. #if SUPPORT_DEBUG_LIGHT
  239. gpio_output_debug_light_init();
  240. #endif
  241. while (true) {
  242. #if SUPPORT_DEBUG_LIGHT
  243. port_do_debug_light_state();
  244. #endif
  245. bleuart_schedule();
  246. motor_module_schedule();
  247. }
  248. }