#include "Printer_Config.h" #include "include/Sys_Printer.h" #include #include #include "esp_log.h" #include "esp_system.h" #include "iot_button.h" #include "esp_sleep.h" // #include "esp_idf_version.h" static const char* TAG = "Printer_run.c"; EXT_RAM_BSS_ATTR uint8_t uheap_1[1024*512]; Receive_data_t Rev_USB_data = { .type = USB, .read = 0, .write = 0, .buffer = (char*)&uheap_1[0], .max_size = 1024*512, }; typedef struct _Heat_page_t{ char* Heat_Page_buff; char Heat_block_buff[80][TH_ACTUAL_WIDTH]; uint8_t Heat_line_buf[TH_WIDTH]; uint32_t Heat_Read; uint32_t Heat_Write; uint16_t Heat_Initial_time; uint16_t Heat_Line_time; }Heat_page_t; Heat_page_t Heat58MM = { .Heat_Initial_time = 300, .Heat_Read = 0, .Heat_Write = 0, }; /********************************* 定义系统全局变量 *****************************/ EXT_RAM_BSS_ATTR uint8_t Heat_Page_buff[800][TH_WIDTH]; EXT_RAM_BSS_ATTR uint8_t LineBuffer[480][TH_WIDTH]; //行打印buffer EXT_RAM_BSS_ATTR uint8_t PrnHeatProp[TH_WIDTH]; //打印头点行buffer EXT_RAM_BSS_ATTR uint8_t PrnDotBuf[DOTMAPLINE][TH_WIDTH]; uint32_t StepCount = 0; //步进计数 uint32_t FeedPhase = 0; //马达相位 uint32_t StepCount,AcceCount,AcceMax,AccMaxInit; uint16_t PrnDotWr,PrnDotRd; bool Printer_direction = true; //打印方向 true:正方向 EventGroupHandle_t Printer_group; SemaphoreHandle_t Spi_send_end; extern const char Font24x24ASCII[]; void Sys_heap_Init(void) { memset(&LineBuffer[0],0,480*(TH_WIDTH)); memset(&PrnHeatProp[0],0,TH_WIDTH); memset(&PrnDotBuf[0],0,DOTMAPLINE*(TH_WIDTH)); } void Fill_Page_Buff(char ch) { int i=0,j=0; uint8_t Rebuff[72]; memcpy(&Rebuff[0],&Font24x24ASCII[ch*72],72); // ESP_LOGI(TAG, "Font24x24ASCII[%d]: %X %X %X",0x41,) for(i=0;i<24;i++) { for(j=0;j<16;j++) { memcpy(&Rebuff[0],&Font24x24ASCII[(ch+j)*72],72); memcpy(&Heat58MM.Heat_block_buff[i][j*3],&Rebuff[i*3],3); } // memcpy(&Heat58MM.Heat_block_buff[i][0],&Rebuff[i*3],3); } for(i=0;i<80;i++) { memcpy(&Heat58MM.Heat_Page_buff[i*TH_ACTUAL_WIDTH],&Heat58MM.Heat_block_buff[i][0],TH_ACTUAL_WIDTH); } } const char *button_event_table[] = { "BUTTON_PRESS_DOWN", "BUTTON_PRESS_UP", "BUTTON_PRESS_REPEAT", "BUTTON_PRESS_REPEAT_DONE", "BUTTON_SINGLE_CLICK", "BUTTON_DOUBLE_CLICK", "BUTTON_MULTIPLE_CLICK", "BUTTON_LONG_PRESS_START", "BUTTON_LONG_PRESS_HOLD", "BUTTON_LONG_PRESS_UP", "BUTTON_PRESS_END", }; void Feed_Key_motion_cb(void *arg, void *data) { ESP_LOGI(TAG, "Feed_Key event[%d] %s", (button_event_t)data,button_event_table[(button_event_t)data]); switch ((button_event_t)data) { case BUTTON_SINGLE_CLICK: Fill_Page_Buff('1'); xEventGroupSetBits(Printer_group,EVENT_Motor_Test); break; case BUTTON_DOUBLE_CLICK: Fill_Page_Buff('A'); xEventGroupSetBits(Printer_group,EVENT_Motor_Test); break; default: break; } } void Sys_Key_motion_cb(void *arg, void *data) { ESP_LOGI(TAG, "Sys_Key event %s", button_event_table[(button_event_t)data]); esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause(); switch ((button_event_t)data) { case BUTTON_SINGLE_CLICK: /* code */ break; default: break; } } void Printer_Run_task(void *pvParameter) { uint8_t execute = 0; char ch = 0; Spi_send_end = xSemaphoreCreateBinary(); while (1) { if(execute != 0) { if(execute > 100) { vTaskDelay(10/ portTICK_PERIOD_MS); execute = 0; } continue; } vTaskDelay(20/ portTICK_PERIOD_MS); } } static void Printer_start(uint8_t Heat_Step) { if(Heat58MM.Heat_Page_buff == NULL || Heat58MM.Heat_Read == Heat58MM.Heat_Write) { Heat58MM.Heat_Write = 0; Heat58MM.Heat_Read = 0; return; } if(Heat_Step == 0) { memcpy(&Heat58MM.Heat_line_buf[10],&Heat58MM.Heat_Page_buff[Heat58MM.Heat_Read*TH_ACTUAL_WIDTH],TH_ACTUAL_WIDTH); spi_printer_data_send(Printer_spi,&Heat58MM.Heat_line_buf[0]); Heat58MM.Heat_Read+=1; }else{ xEventGroupSetBits(Printer_group,EVENT_Heat_Begin); } } void Printer_Heat_task(void *pvParameter) { static uint8_t Heat_Step = 0; EventBits_t bits; ESP_LOGI(TAG,"Printer_Heat_task init"); while (1) { bits = xEventGroupWaitBits(Printer_group,EVENT_Motor_Test | EVENT_Heat_Begin | EVENT_Heat_Done | EVENT_SPI_SEND_Begin| Heat_END_EVENT | EVENT_SPI_SEND_Done,pdFALSE,pdFALSE,portMAX_DELAY); switch (bits) { case EVENT_Motor_Test: // ESP_LOGI(TAG,"Event Motor_Test"); // Fill_Page_Buff('A'); Heat58MM.Heat_Write = 80; Printer_start(Heat_Step); // Set_heat_timer_value(1000*1000*5); xEventGroupClearBits(Printer_group,EVENT_Motor_Test); break; case EVENT_Heat_Begin: //开始加热 STB.state = 1; gpio_set_level(STB.pin_num,STB.state); Set_heat_timer_value(400*1); // Motor_GO(1); // //ESP_LOGI(TAG,"Heat_EVENT:%d",PrnDotRd); // xSemaphoreGive(Spi_send_end); xEventGroupClearBits(Printer_group,EVENT_Heat_Begin); break; case EVENT_SPI_SEND_Begin: // LAT.state = 0; // gpio_set_level(LAT.pin_num,LAT.state); // for(int i=0;i<0xff;i++); // LAT.state = 1; // gpio_set_level(LAT.pin_num,LAT.state); // STB.state = 1; // gpio_set_level(STB.pin_num,STB.state); // Set_heat_timer_value(600); // //ESP_LOGI(TAG,"SPI_SEND_EVENT:%d",PrnDotRd); // xEventGroupClearBits(Printer_group,SPI_SEND_EVENT); break; case EVENT_Heat_Done: // ESP_LOGI(TAG,"Event Heat_Done"); STB.state = 0; gpio_set_level(STB.pin_num,STB.state); Motor_GO(1); Heat_Step+=1; if(Heat_Step>3) { Heat_Step = 0; } // STB.state = 0; // gpio_set_level(STB.pin_num,STB.state); // ESP_LOGI(TAG,"Heat_END_EVENT"); // Motor_GO(0xff); xEventGroupSetBits(Printer_group,EVENT_Motor_Test); xEventGroupClearBits(Printer_group,EVENT_Heat_Done); break; case EVENT_SPI_SEND_Done: // ESP_LOGI(TAG,"EVENT_SPI_SEND_Done"); // SPI发送完成后锁存数据 LAT.state = 0; gpio_set_level(LAT.pin_num,LAT.state); for(int i=0;i<0xff;i++); LAT.state = 1; gpio_set_level(LAT.pin_num,LAT.state); xEventGroupSetBits(Printer_group,EVENT_Heat_Begin); xEventGroupClearBits(Printer_group,EVENT_SPI_SEND_Done); break; case EVENT_Motor_Sleep: Motor_GO(0xff); xEventGroupClearBits(Printer_group,EVENT_Motor_Sleep); break; default: break; } } } void Printer_run_init() { Sys_heap_Init(); Heat58MM.Heat_Page_buff = malloc(58*1600); xTaskCreatePinnedToCore(Printer_Heat_task, "Printer_Heat_task", 1024 * 10, NULL, 20, NULL, 1);//核心1 xTaskCreatePinnedToCore(Printer_Run_task, "Printer_Run_task", 1024 * 100, NULL, 12, NULL, 0);//核心1 }