增加通过USB虚拟串口打印文本字符功能

This commit is contained in:
Yu Xifeng 2024-08-24 22:20:25 +08:00
parent 2dc0837ab8
commit 8a03a3b16c
6 changed files with 160 additions and 89 deletions

View File

@ -5,5 +5,7 @@ cmake_minimum_required(VERSION 3.16)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
add_compile_options(-w) # Turn off warnings until esp_tinyusb is updated IEC-86 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) include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(Air58Printer) project(Air58Printer)

View File

@ -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 set(srcs
INCLUDE_DIRS ".") "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")

View File

@ -14,39 +14,19 @@
#include "module/include/Sys_Printer.h" #include "module/include/Sys_Printer.h"
#include "module/Printer_Config.h" #include "module/Printer_Config.h"
#include "esp_adc/adc_continuous.h"
static const char *TAG = "main";
static const char *TAG = "example";
#define BLINK_GPIO 4 #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 User_xQueueCreate()
void tinyusb_cdc_rx_callback(int itf, cdcacm_event_t *event)
{ {
/* initialization */ User_data_queue = xQueueCreate(128, sizeof(user_data_t));//64K
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);
} }
void app_main(void) void app_main(void)
@ -54,6 +34,7 @@ void app_main(void)
/* Print chip information */ /* Print chip information */
esp_chip_info_t chip_info; esp_chip_info_t chip_info;
uint32_t flash_size; uint32_t flash_size;
User_xQueueCreate();
esp_chip_info(&chip_info); esp_chip_info(&chip_info);
printf("This is %s chip with %d CPU core(s), %s%s%s%s, ", printf("This is %s chip with %d CPU core(s), %s%s%s%s, ",
CONFIG_IDF_TARGET, CONFIG_IDF_TARGET,
@ -75,50 +56,7 @@ void app_main(void)
(chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external"); (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");
printf("Minimum free heap size: %" PRIu32 " bytes\n", esp_get_minimum_free_heap_size()); printf("Minimum free heap size: %" PRIu32 " bytes\n", esp_get_minimum_free_heap_size());
Usb_Virtual_serial_port_init();
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");
Printer_group = xEventGroupCreate(); Printer_group = xEventGroupCreate();
@ -127,20 +65,15 @@ void app_main(void)
Printer_run_init(); Printer_run_init();
while (1) { while (1) {
// Motor_GO(1); if(xQueueReceive(User_data_queue, &Receive_All_data, portMAX_DELAY))
/* Toggle the LED state */ {
// if(gpio_get_level(FEED_KEY.pin_num) == 0) 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);
// // ESP_LOGI(TAG, "FEED_KEY:%d",0); if(Receive_All_data.type == USB)
// //Motor_GO(1); {
// xEventGroupSetBits(Printer_group,EVENT_Motor_Test); input_Print_data(CMD_CUSTON,&Receive_All_data.data[0],Receive_All_data.size);
// while (gpio_get_level(FEED_KEY.pin_num) == 0) { }
// vTaskDelay(10 / portTICK_PERIOD_MS); }
// }
// }else{
// // Motor_GO(0xff);
// }
vTaskDelay(1000 / portTICK_PERIOD_MS);
// ESP_LOGI(TAG, "LED State:%d",!s_led_state); // ESP_LOGI(TAG, "LED State:%d",!s_led_state);
} }
fflush(stdout); fflush(stdout);

View File

@ -127,8 +127,9 @@ void Fill_char2block_Buff(char ch,uint8_t SetPosX,uint16_t SetPosY,FontType_t fo
free(Rebuff); 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); Fill_char2block_Buff(ch,Heat58MM.Heat_block.PosX,Heat58MM.Heat_block.PosY,FontASCII_24x24);
Heat58MM.Heat_block.PosX += FontASCII_24x24.width/8; Heat58MM.Heat_block.PosX += FontASCII_24x24.width/8;
if(Heat58MM.Heat_block.PosX >= TH_ACTUAL_WIDTH ||ch == '\n') 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.PosX = 0;
Heat58MM.Heat_block.PosY += FontASCII_24x24.hight; 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) void Fill_Page_Buff(char ch)
@ -225,7 +229,26 @@ void Timer_State_machine_cb(uint16_t time_ms)
counts = 0; 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<size;i++)
{
block_hight = Addchar2PrintArea(data[i]);
}
if(cmd == CMD_CUSTON)
{
}
if(data[i-1] == '\n' || block_hight )
{
Fill_block2Page_Buff(10,Heat58MM.Heat_block.PosY+24*3,(uint8_t*)&Heat58MM.Heat_block_buff[0]);
memset(&Heat58MM.Heat_block_buff[0],0x00,TH_ACTUAL_WIDTH*100*8);
xEventGroupSetBits(Printer_group,EVENT_Motor_Test);
}
}
void Printer_Run_task(void *pvParameter) void Printer_Run_task(void *pvParameter)
{ {

78
main/module/USB/usb_cdc.c Normal file
View File

@ -0,0 +1,78 @@
#include <stdint.h>
#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");
}

View File

@ -56,6 +56,23 @@ enum{
#define KEY_EVENT BIT7 #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 typedef struct
{ {
@ -94,4 +111,6 @@ extern void button_init(void);
extern void Feed_Key_motion_cb(void *arg, void *data); extern void Feed_Key_motion_cb(void *arg, void *data);
extern void Sys_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 void Timer_State_machine_cb(uint16_t time_ms);
extern uint16_t input_Print_data(Cmd_Type cmd,char* data,size_t size);
#endif #endif