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

201 lines
7.8 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
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 ble_uart_tx_size 128
  19. #define ble_uart_rx_size 128
  20. #define construct_cmd_packet_buffer_size 256
  21. typedef struct {
  22. int index;
  23. const char *order;
  24. } RxContext_t;
  25. static uint8_t bluetooth_tx_buffer[ble_uart_tx_size] = {0};
  26. static uint8_t bluetooth_rx_buffer[ble_uart_rx_size] = {0};
  27. bleuart_t ble_uart_init_struct = {
  28. .txbuf = bluetooth_tx_buffer,
  29. .txbufsize = sizeof(bluetooth_tx_buffer),
  30. .rxbuf = bluetooth_rx_buffer,
  31. .rxbufsize = sizeof(bluetooth_rx_buffer),
  32. .rxpacketReceiveOvertime = 200,
  33. .receive_data_processing_flag = false,
  34. .maxClientNum = 1,
  35. .bleName = "yimei_ble",
  36. .ble_connect_flag = false,
  37. };
  38. motor_t ble_uart_motor_structer = {.uartNum = UART_NUM_1};
  39. void construct_set_position_packet_and_report(int index, uint8_t setPosition_code) {
  40. uint8_t buffer[construct_cmd_packet_buffer_size] = {0};
  41. ESP_LOGI(MAIN_LOG_TAG, "Command setPosition execution complete!");
  42. if (setPosition_code == 0) {
  43. snprintf((char *)buffer, construct_cmd_packet_buffer_size, "{ \"order\": \"receipt\", \"code\": %d, \"info\": \"success\", \"index\": %d }", //
  44. /* code :*/ setPosition_code, //
  45. /* index :*/ index //
  46. );
  47. bleuart_notify_send(buffer, strlen((char *)buffer));
  48. } else {
  49. snprintf((char *)buffer, construct_cmd_packet_buffer_size, "{ \"order\": \"receipt\", \"code\": %d, \"info\": \"error\", \"index\": %d }", //
  50. /* code :*/ setPosition_code, //
  51. /* index :*/ index //
  52. );
  53. bleuart_notify_send(buffer, strlen((char *)buffer));
  54. }
  55. }
  56. void construct_get_status_packet_and_report(int index, double motor_position) {
  57. uint8_t buffer[construct_cmd_packet_buffer_size] = {0};
  58. if (motor_turning == false) {
  59. snprintf((char *)buffer, construct_cmd_packet_buffer_size,
  60. "{ \"order\": \"receipt\", \"index\": %d, \"deviceState\": \"%s\", \"deviceException\": 0, \"deviceExceptionInfo\": "
  61. ", \"position\":%lf }",
  62. /* index :*/ index, //
  63. /* deviceState :*/ "idel", //
  64. /* positon :*/ motor_position //
  65. );
  66. } else {
  67. snprintf((char *)buffer, construct_cmd_packet_buffer_size,
  68. "{ \"order\": \"receipt\", \"index\": %d, \"deviceState\": \"%s\", \"deviceException\": 0, \"deviceExceptionInfo\": "
  69. ", \"position\":%lf }",
  70. /* index :*/ index, //
  71. /* deviceState :*/ "rotating", //
  72. /* positon :*/ motor_position //
  73. );
  74. }
  75. ESP_LOGI(MAIN_LOG_TAG, "report %s", buffer);
  76. bleuart_notify_send(buffer, strlen((char *)buffer));
  77. }
  78. void construct_set_motorCurrentSize_packet_and_report(int index) {
  79. uint8_t buffer[construct_cmd_packet_buffer_size] = {0};
  80. snprintf((char *)buffer, construct_cmd_packet_buffer_size, "{ \"order\": \"receipt\", \"index\": %d }", index);
  81. ESP_LOGI(MAIN_LOG_TAG, "report %s", buffer);
  82. bleuart_notify_send(buffer, strlen((char *)buffer));
  83. }
  84. void process_setPosition(RxContext_t *context, cJSON *rxjson) {
  85. // { "order": "setPosition", "ble_uart_index": 0, "speedLevel": 1, "position": 120.3, "direction": 0 }
  86. int speedLevel = 0;
  87. float position = 0;
  88. int direction = 0;
  89. uint8_t setPosition_code;
  90. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "position"))) {
  91. position = cJSON_GetObjectItem(rxjson, "position")->valuedouble;
  92. }
  93. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "speedLevel"))) {
  94. speedLevel = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "speedLevel"));
  95. }
  96. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "direction"))) {
  97. direction = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "direction"));
  98. }
  99. ESP_LOGI(MAIN_LOG_TAG, "order = %s,index = %d,speedLevel = %d,position = %lf,direction = %d", context->order, context->index, speedLevel, position, direction);
  100. setPosition_code = motor_run_to_postion(1, position, 0);
  101. motor_turning = true;
  102. construct_set_position_packet_and_report(context->index, setPosition_code);
  103. }
  104. void process_getStatus(RxContext_t *context, cJSON *rxjson) {
  105. double motor_position = motor_get_position_degree();
  106. construct_get_status_packet_and_report(context->index, motor_position);
  107. }
  108. void process_setMotorCurrentSize(RxContext_t *context, cJSON *rxjson) {
  109. motor_set_zero_point();
  110. construct_set_motorCurrentSize_packet_and_report(context->index);
  111. }
  112. void processrxjson(cJSON *rxjson) {
  113. if (!cJSON_IsString(cJSON_GetObjectItem(rxjson, "order"))) {
  114. return;
  115. }
  116. RxContext_t rxcontext = {0};
  117. if (cJSON_IsString(cJSON_GetObjectItem(rxjson, "order"))) {
  118. rxcontext.order = cJSON_GetStringValue(cJSON_GetObjectItem(rxjson, "order"));
  119. }
  120. if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "index"))) {
  121. rxcontext.index = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "index"));
  122. }
  123. if (strcmp(rxcontext.order, "setPosition") == 0) {
  124. process_setPosition(&rxcontext, rxjson);
  125. } else if (strcmp(rxcontext.order, "getStatus") == 0) {
  126. process_getStatus(&rxcontext, rxjson);
  127. } else if (strcmp(rxcontext.order, "setMotorCurrentSize") == 0) {
  128. process_setMotorCurrentSize(&rxcontext, rxjson);
  129. }
  130. }
  131. void blerxcb(uint8_t *rxmessage, size_t rxsize) {
  132. rxmessage[rxsize] = 0;
  133. cJSON *rxmessage_json = cJSON_ParseWithLength((char *)rxmessage, rxsize);
  134. if (rxmessage_json == NULL) {
  135. ESP_LOGE(MAIN_LOG_TAG, "cJSON_Parse fail rxmessage_json == null,%s", cJSON_GetErrorPtr());
  136. return;
  137. }
  138. processrxjson(rxmessage_json);
  139. cJSON_Delete(rxmessage_json);
  140. }
  141. void motor_on_event(motor_event_t event) {
  142. //打算使用sprintf
  143. if (event == kRunToPosition) {
  144. ESP_LOGI(MAIN_LOG_TAG, "motor_on_event: kRunToPosition");
  145. motor_turning = false;
  146. uint8_t buffer[construct_cmd_packet_buffer_size] = {0};
  147. double motor_stop_position = motor_get_position_degree();
  148. //{ "order": "deviceStatusReport", "ble_uart_index": 0, "deviceState": "running", "position":13.9, "deviceException": 0, "deviceExceptionInfo": ""}
  149. snprintf((char *)buffer, construct_cmd_packet_buffer_size,
  150. "{ \"order\": \"deviceStatusReport\", \"index\": %d, \"deviceState\": \"idel\", \"position\":%.2lf, \"deviceException\": 0, \"deviceExceptionInfo\": \"\" }", //
  151. /*index :*/ 1, //
  152. /*position :*/ motor_stop_position //
  153. );
  154. bleuart_notify_send(buffer, strlen((char *)buffer));
  155. }
  156. }
  157. void app_main(void) {
  158. bleuart_init(&ble_uart_init_struct);
  159. bleuart_reg_cb(blerxcb);
  160. ble_set_motor_reg_cb(motor_run_to_postion);
  161. motor_init(&ble_uart_motor_structer);
  162. motor_reg_event_cb(motor_on_event);
  163. gpio_output_debug_light_init();
  164. while (true) {
  165. if (ble_uart_init_struct.ble_connect_flag == true) {
  166. port_do_debug_light_state(300);
  167. }else{
  168. port_do_debug_light_state(1000);
  169. }
  170. bleuart_schedule();
  171. motor_module_schedule();
  172. }
  173. }