diff --git a/CMakeLists.txt b/CMakeLists.txt index 6086504..8fbb643 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,5 +5,7 @@ cmake_minimum_required(VERSION 3.16) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) add_compile_options(-w) # Turn off warnings until esp_tinyusb is updated IEC-86 +include_directories(module) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(Air58Printer) diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index f6e219f..1c0033f 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,3 +1,19 @@ -idf_component_register(SRCS "main.c" "module/Printer_run.c" "module/Printer_bsp.c" "module/Font24x24ASCII.c" "module/button.c" - PRIV_REQUIRES spi_flash driver - INCLUDE_DIRS ".") +# 指定引用库的头文件路径,否则会提示找不到头文件 +set(srcs + "main.c" + ) + +set(module_srcs + "module/Printer_run.c" + "module/Printer_bsp.c" + "module/Font24x24ASCII.c" + "module/button.c" + "module/USB/usb_cdc.c" + "module/esp_sleep.c" +) + + +idf_component_register(SRCS ${srcs} ${module_srcs} + PRIV_REQUIRES spi_flash driver esp_event + INCLUDE_DIRS "." + INCLUDE_DIRS "module") diff --git a/main/main.c b/main/main.c index d857b85..7bb4dda 100644 --- a/main/main.c +++ b/main/main.c @@ -14,39 +14,19 @@ #include "module/include/Sys_Printer.h" #include "module/Printer_Config.h" +#include "esp_adc/adc_continuous.h" - -static const char *TAG = "example"; +static const char *TAG = "main"; #define BLINK_GPIO 4 +extern void Usb_Virtual_serial_port_init(void); +QueueHandle_t User_data_queue; +user_data_t Receive_All_data; -static uint8_t buf[CONFIG_TINYUSB_CDC_RX_BUFSIZE + 1]; - -void tinyusb_cdc_rx_callback(int itf, cdcacm_event_t *event) +void User_xQueueCreate() { - /* initialization */ - size_t rx_size = 0; - - /* read */ - esp_err_t ret = tinyusb_cdcacm_read(itf, buf, CONFIG_TINYUSB_CDC_RX_BUFSIZE, &rx_size); - if (ret == ESP_OK) { - ESP_LOGI(TAG, "Data from channel %d:", itf); - ESP_LOG_BUFFER_HEXDUMP(TAG, buf, rx_size, ESP_LOG_INFO); - } else { - ESP_LOGE(TAG, "Read error"); - } - - /* write back */ - tinyusb_cdcacm_write_queue(itf, buf, rx_size); - tinyusb_cdcacm_write_flush(itf, 0); -} - -void tinyusb_cdc_line_state_changed_callback(int itf, cdcacm_event_t *event) -{ - int dtr = event->line_state_changed_data.dtr; - int rts = event->line_state_changed_data.rts; - ESP_LOGI(TAG, "Line state changed on channel %d: DTR:%d, RTS:%d", itf, dtr, rts); + User_data_queue = xQueueCreate(128, sizeof(user_data_t));//64K } void app_main(void) @@ -54,6 +34,7 @@ void app_main(void) /* Print chip information */ esp_chip_info_t chip_info; uint32_t flash_size; + User_xQueueCreate(); esp_chip_info(&chip_info); printf("This is %s chip with %d CPU core(s), %s%s%s%s, ", CONFIG_IDF_TARGET, @@ -75,50 +56,7 @@ void app_main(void) (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external"); printf("Minimum free heap size: %" PRIu32 " bytes\n", esp_get_minimum_free_heap_size()); - - - ESP_LOGI(TAG, "USB initialization"); - const tinyusb_config_t tusb_cfg = { - .device_descriptor = NULL, - .string_descriptor = NULL, - .external_phy = false, -#if (TUD_OPT_HIGH_SPEED) - .fs_configuration_descriptor = NULL, - .hs_configuration_descriptor = NULL, - .qualifier_descriptor = NULL, -#else - .configuration_descriptor = NULL, -#endif // TUD_OPT_HIGH_SPEED - }; - - ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg)); - - tinyusb_config_cdcacm_t acm_cfg = { - .usb_dev = TINYUSB_USBDEV_0, - .cdc_port = TINYUSB_CDC_ACM_0, - .rx_unread_buf_sz = 64, - .callback_rx = &tinyusb_cdc_rx_callback, // the first way to register a callback - .callback_rx_wanted_char = NULL, - .callback_line_state_changed = NULL, - .callback_line_coding_changed = NULL - }; - - ESP_ERROR_CHECK(tusb_cdc_acm_init(&acm_cfg)); - /* the second way to register a callback */ - ESP_ERROR_CHECK(tinyusb_cdcacm_register_callback( - TINYUSB_CDC_ACM_0, - CDC_EVENT_LINE_STATE_CHANGED, - &tinyusb_cdc_line_state_changed_callback)); - -#if (CONFIG_TINYUSB_CDC_COUNT > 1) - acm_cfg.cdc_port = TINYUSB_CDC_ACM_1; - ESP_ERROR_CHECK(tusb_cdc_acm_init(&acm_cfg)); - ESP_ERROR_CHECK(tinyusb_cdcacm_register_callback( - TINYUSB_CDC_ACM_1, - CDC_EVENT_LINE_STATE_CHANGED, - &tinyusb_cdc_line_state_changed_callback)); -#endif - ESP_LOGI(TAG, "USB initialization DONE"); + Usb_Virtual_serial_port_init(); Printer_group = xEventGroupCreate(); @@ -127,20 +65,15 @@ void app_main(void) Printer_run_init(); while (1) { - // Motor_GO(1); - /* Toggle the LED state */ - // if(gpio_get_level(FEED_KEY.pin_num) == 0) - // { - // // ESP_LOGI(TAG, "FEED_KEY:%d",0); - // //Motor_GO(1); - // xEventGroupSetBits(Printer_group,EVENT_Motor_Test); - // while (gpio_get_level(FEED_KEY.pin_num) == 0) { - // vTaskDelay(10 / portTICK_PERIOD_MS); - // } - // }else{ - // // Motor_GO(0xff); - // } - vTaskDelay(1000 / portTICK_PERIOD_MS); + if(xQueueReceive(User_data_queue, &Receive_All_data, portMAX_DELAY)) + { + uint16_t Spaces = uxQueueSpacesAvailable(User_data_queue); + ESP_LOGI(TAG,"Spaces:%d Type:%d Size:%d",Spaces,Receive_All_data.type,Receive_All_data.size); + if(Receive_All_data.type == USB) + { + input_Print_data(CMD_CUSTON,&Receive_All_data.data[0],Receive_All_data.size); + } + } // ESP_LOGI(TAG, "LED State:%d",!s_led_state); } fflush(stdout); diff --git a/main/module/Printer_run.c b/main/module/Printer_run.c index b5edaef..317039a 100644 --- a/main/module/Printer_run.c +++ b/main/module/Printer_run.c @@ -127,8 +127,9 @@ void Fill_char2block_Buff(char ch,uint8_t SetPosX,uint16_t SetPosY,FontType_t fo free(Rebuff); } -void Addchar2PrintArea(char ch) +uint16_t Addchar2PrintArea(char ch) { + ESP_LOGI(TAG,"ch:%c",ch); Fill_char2block_Buff(ch,Heat58MM.Heat_block.PosX,Heat58MM.Heat_block.PosY,FontASCII_24x24); Heat58MM.Heat_block.PosX += FontASCII_24x24.width/8; if(Heat58MM.Heat_block.PosX >= TH_ACTUAL_WIDTH ||ch == '\n') @@ -136,6 +137,9 @@ void Addchar2PrintArea(char ch) Heat58MM.Heat_block.PosX = 0; Heat58MM.Heat_block.PosY += FontASCII_24x24.hight; } + Heat58MM.Heat_block.hight = Heat58MM.Heat_block.PosY + FontASCII_24x24.hight; + + return Heat58MM.Heat_block.hight; } void Fill_Page_Buff(char ch) @@ -225,7 +229,26 @@ void Timer_State_machine_cb(uint16_t time_ms) counts = 0; } } +uint16_t input_Print_data(Cmd_Type cmd,char* data,size_t size) +{ + int i=0; + int block_hight = 0; + for(i=0;i +#include "esp_log.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "tinyusb.h" +#include "tusb_cdc_acm.h" +#include "sdkconfig.h" +#include "include/Sys_Printer.h" + + +static const char *TAG = "usb_cdc"; + +extern QueueHandle_t User_data_queue; +user_data_t Receive_usb_data; + +// static uint8_t buf[CONFIG_TINYUSB_CDC_RX_BUFSIZE + 1]; + +void tinyusb_cdc_rx_callback(int itf, cdcacm_event_t *event) +{ + /* initialization */ + size_t rx_size = 0; + + /* read */ + esp_err_t ret = tinyusb_cdcacm_read(itf, &Receive_usb_data.data[0], CONFIG_TINYUSB_CDC_RX_BUFSIZE, &rx_size); + if (ret == ESP_OK) { + // ESP_LOGI(TAG, "Data from channel %d:", itf); + // ESP_LOG_BUFFER_HEXDUMP(TAG, buf, rx_size, ESP_LOG_INFO); + Receive_usb_data.type = USB; + Receive_usb_data.size = rx_size; + xQueueSend(User_data_queue, &Receive_usb_data, portMAX_DELAY); + + } else { + ESP_LOGE(TAG, "Read error"); + } + + /* write back */ + // tinyusb_cdcacm_write_queue(itf, buf, rx_size); + // tinyusb_cdcacm_write_flush(itf, 0); +} + +void tinyusb_cdc_line_state_changed_callback(int itf, cdcacm_event_t *event) +{ + int dtr = event->line_state_changed_data.dtr; + int rts = event->line_state_changed_data.rts; + ESP_LOGI(TAG, "Line state changed on channel %d: DTR:%d, RTS:%d", itf, dtr, rts); +} + + +void Usb_Virtual_serial_port_init(void) +{ + ESP_LOGI(TAG, "USB initialization"); + const tinyusb_config_t tusb_cfg = { + .device_descriptor = NULL, + .string_descriptor = NULL, + .external_phy = false, + .configuration_descriptor = NULL, + }; + + ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg)); + + tinyusb_config_cdcacm_t acm_cfg = { + .usb_dev = TINYUSB_USBDEV_0, + .cdc_port = TINYUSB_CDC_ACM_0, + .rx_unread_buf_sz = 64, + .callback_rx = &tinyusb_cdc_rx_callback, // the first way to register a callback + .callback_rx_wanted_char = NULL, + .callback_line_state_changed = NULL, + .callback_line_coding_changed = NULL + }; + + ESP_ERROR_CHECK(tusb_cdc_acm_init(&acm_cfg)); + /* the second way to register a callback */ + ESP_ERROR_CHECK(tinyusb_cdcacm_register_callback( + TINYUSB_CDC_ACM_0, + CDC_EVENT_LINE_STATE_CHANGED, + &tinyusb_cdc_line_state_changed_callback)); + ESP_LOGI(TAG, "USB initialization DONE"); +} \ No newline at end of file diff --git a/main/module/include/Sys_Printer.h b/main/module/include/Sys_Printer.h index 5eb80af..d20f1c0 100644 --- a/main/module/include/Sys_Printer.h +++ b/main/module/include/Sys_Printer.h @@ -56,6 +56,23 @@ enum{ #define KEY_EVENT BIT7 +enum{ + TYPE_MES_HEAD = 0, + TYPE_USB_CDC = 0x01, + TYPE_USB_MSC, + TYPE_WIFI_TCP, + TYPE_WIFI_HTTP, +}; +typedef enum Type_t{ + CMD_CUSTON = 0, +}Cmd_Type; + +typedef struct +{ + uint8_t type; + uint32_t size; + uint8_t data[1024]; +}user_data_t; typedef struct { @@ -94,4 +111,6 @@ extern void button_init(void); extern void Feed_Key_motion_cb(void *arg, void *data); extern void Sys_Key_motion_cb(void *arg, void *data); extern void Timer_State_machine_cb(uint16_t time_ms); +extern uint16_t input_Print_data(Cmd_Type cmd,char* data,size_t size); + #endif \ No newline at end of file