437 lines
12 KiB
C
437 lines
12 KiB
C
#include "Printer_Config.h"
|
||
#include "include/Sys_Printer.h"
|
||
#include <stdint.h>
|
||
#include <stdlib.h>
|
||
#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";
|
||
uint8_t Sleep_mode = 0;//0:运行 1:休眠
|
||
|
||
|
||
// Receive_data_t Rev_USB_data = {
|
||
// .type = USB,
|
||
// .read = 0,
|
||
// .write = 0,
|
||
// .buffer = (char*)&uheap_1[0],
|
||
// .max_size = 1024*512,
|
||
// };
|
||
|
||
typedef struct _Heat_block
|
||
{
|
||
uint16_t hight;
|
||
uint8_t PosX;
|
||
uint16_t PosY;
|
||
char* Heat_block_buff;
|
||
}Heat_block_t;
|
||
|
||
|
||
typedef struct _Heat_page_t{
|
||
|
||
char* Heat_Page_buff;
|
||
char* Heat_block_buff;
|
||
Heat_block_t Heat_block;
|
||
uint8_t Heat_line_buf[TH_WIDTH];
|
||
uint32_t Heat_Read;
|
||
uint32_t Heat_Write;
|
||
uint32_t Max_hight;
|
||
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,
|
||
.Heat_block.PosX=0,
|
||
.Heat_block.PosY=0,
|
||
};
|
||
|
||
|
||
typedef struct _fonttype
|
||
{
|
||
uint8_t font_id;
|
||
uint8_t width;
|
||
uint8_t hight;
|
||
uint16_t char_size;
|
||
uint32_t file_size;
|
||
char* file_buff;
|
||
}FontType_t;
|
||
|
||
|
||
FontType_t FontASCII_24x24 = {
|
||
.font_id = 3,
|
||
.width = 24,
|
||
.hight = 24,
|
||
.char_size = 72,
|
||
.file_size = 9144,
|
||
.file_buff = Font24x24ASCII,
|
||
};
|
||
|
||
/********************************* 定义系统全局变量 *****************************/
|
||
|
||
|
||
|
||
EXT_RAM_BSS_ATTR uint8_t Heat_Page_buff[800][TH_WIDTH];
|
||
|
||
uint32_t StepCount = 0; //步进计数
|
||
uint32_t FeedPhase = 0; //马达相位
|
||
|
||
uint16_t PrnDotWr,PrnDotRd;
|
||
bool Printer_direction = true; //打印方向 true:正方向
|
||
|
||
EventGroupHandle_t Printer_group;
|
||
SemaphoreHandle_t Spi_send_end;
|
||
SemaphoreHandle_t Motor_is_Running;
|
||
|
||
|
||
|
||
void Sys_heap_Init(void)
|
||
{
|
||
|
||
}
|
||
void Fill_block2Page_Buff(uint16_t SetPosY,uint16_t hight,uint8_t* block_buff)
|
||
{
|
||
uint32_t Total_height = SetPosY + hight;
|
||
for(int i=SetPosY;i<(SetPosY+hight);i++)
|
||
{
|
||
memcpy(&Heat58MM.Heat_Page_buff[i*TH_ACTUAL_WIDTH],&block_buff[(i - SetPosY)*TH_ACTUAL_WIDTH],TH_ACTUAL_WIDTH);
|
||
}
|
||
if(Total_height > Heat58MM.Max_hight)
|
||
{
|
||
Heat58MM.Max_hight = Total_height;
|
||
}
|
||
Heat58MM.Heat_Write = Heat58MM.Max_hight;
|
||
Heat58MM.Heat_block.PosX = 0;
|
||
Heat58MM.Heat_block.PosY = 0;
|
||
}
|
||
|
||
void Fill_char2block_Buff(char ch,uint8_t SetPosX,uint16_t SetPosY,FontType_t font)
|
||
{
|
||
int i=0;
|
||
uint8_t* Rebuff;
|
||
Rebuff = malloc(font.char_size+12);
|
||
memcpy(&Rebuff[0],&font.file_buff[ch*font.char_size],font.char_size);
|
||
for(i=0;i<font.hight;i++)
|
||
{
|
||
memcpy(&Heat58MM.Heat_block_buff[(SetPosY+i)*TH_ACTUAL_WIDTH+SetPosX],&Rebuff[i*(font.width/8)],(font.width/8));
|
||
}
|
||
|
||
free(Rebuff);
|
||
}
|
||
|
||
uint16_t Addchar2PrintArea(char 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')
|
||
{
|
||
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)
|
||
{
|
||
// int i=0,j=0;
|
||
// uint8_t Rebuff[72];
|
||
// memcpy(&Rebuff[0],&Font24x24ASCII[ch*72],72);
|
||
// memset(&Heat58MM.Heat_block_buff[0],0x00,TH_ACTUAL_WIDTH*24);
|
||
// // 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*TH_ACTUAL_WIDTH+j*3],&Rebuff[i*3],3);
|
||
// }
|
||
|
||
// }
|
||
// Fill_char2block_Buff(ch,3,FontASCII_24x24);
|
||
for(int i=0;i<30;i++)
|
||
{
|
||
Addchar2PrintArea('A'+i);
|
||
// Addchar2PrintArea('\n');
|
||
}
|
||
Fill_block2Page_Buff(10,Heat58MM.Heat_block.PosY+24*3,(uint8_t*)&Heat58MM.Heat_block_buff[0]);
|
||
}
|
||
|
||
|
||
|
||
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:
|
||
memset(&Heat58MM.Heat_Page_buff[0],0x00,TH_ACTUAL_WIDTH*500*8);
|
||
Heat58MM.Heat_Write = 200*8;
|
||
xEventGroupSetBits(Printer_group,EVENT_Motor_Test);
|
||
break;
|
||
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
|
||
void Timer_State_machine_cb(uint16_t time_ms)
|
||
{
|
||
static uint16_t counts = 0;
|
||
counts+=time_ms;
|
||
// if(counts >=10)
|
||
{
|
||
static portBASE_TYPE xHigherPriorityTaskWoken;
|
||
xSemaphoreGiveFromISR(SysTick_queue,xHigherPriorityTaskWoken);
|
||
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);
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
void Printer_Run_task(void *pvParameter)
|
||
{
|
||
uint16_t counts = 1;
|
||
char ch = 0;
|
||
while (1)
|
||
{
|
||
|
||
if(xSemaphoreTake(SysTick_queue, portMAX_DELAY ) == pdTRUE )
|
||
{
|
||
if(counts%1000 == 0)
|
||
{
|
||
if(counts%5000 == 0)
|
||
{
|
||
if (xSemaphoreTake(Motor_is_Running, 10 / portTICK_PERIOD_MS) == pdTRUE)
|
||
{
|
||
xEventGroupSetBits(Printer_group,EVENT_Motor_Sleep);
|
||
Sleep_mode = 1;
|
||
xSemaphoreGive(Motor_is_Running);//解锁
|
||
}
|
||
|
||
}else if(counts%9000 == 0)
|
||
{
|
||
if(Sleep_mode == 1)
|
||
{
|
||
ESP_LOGI(TAG,"Entering light sleep\n");
|
||
vTaskDelay(500/ portTICK_PERIOD_MS);
|
||
/* Enter sleep mode */
|
||
esp_light_sleep_start();
|
||
if (esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_GPIO) {
|
||
/* Waiting for the gpio inactive, or the chip will continuously trigger wakeup*/
|
||
// wait_gpio_inactive();
|
||
}
|
||
Sleep_mode = 0;
|
||
}
|
||
counts = 0;
|
||
}
|
||
}
|
||
if(counts%100 == 0)
|
||
{
|
||
|
||
}
|
||
if(counts%10 == 0)
|
||
{
|
||
|
||
}
|
||
if(1)
|
||
{
|
||
|
||
}
|
||
counts+=1;
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
static uint16_t 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 0;
|
||
}
|
||
|
||
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);
|
||
}
|
||
return (Heat58MM.Heat_Write - Heat58MM.Heat_Read);
|
||
}
|
||
|
||
|
||
void Printer_Heat_task(void *pvParameter)
|
||
{
|
||
static uint8_t Ongoing_state = 0;
|
||
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
|
||
| EVENT_Motor_Sleep | Heat_END_EVENT | EVENT_SPI_SEND_Done,pdFALSE,pdFALSE,portMAX_DELAY);
|
||
|
||
switch (bits)
|
||
{
|
||
case EVENT_Motor_Test:
|
||
if (xSemaphoreTake(Motor_is_Running, 0) == pdTRUE)//上锁,电机运行中
|
||
{
|
||
Ongoing_state = 1;
|
||
}else if(Ongoing_state == 0){
|
||
ESP_LOGI(TAG,"Event Motor_is_Running");
|
||
xEventGroupClearBits(Printer_group,EVENT_Motor_Test);
|
||
break;
|
||
}
|
||
if(Printer_start(Heat_Step) == 0)
|
||
{
|
||
Ongoing_state = 0;
|
||
xSemaphoreGive(Motor_is_Running);//解锁
|
||
}
|
||
|
||
// Set_heat_timer_value(1000*1000*5);
|
||
xEventGroupClearBits(Printer_group,EVENT_Motor_Test);
|
||
break;
|
||
case EVENT_Heat_Begin:
|
||
//开始加热
|
||
if(Heat_Step < 3)
|
||
{
|
||
Set_gpio_state(&STB,1);
|
||
}
|
||
Set_heat_timer_value(312);//50mm/s
|
||
|
||
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");
|
||
Set_gpio_state(&STB,0);
|
||
Motor_GO(1);
|
||
Heat_Step+=1;
|
||
if(Heat_Step>3)
|
||
{
|
||
Heat_Step = 0;
|
||
}
|
||
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:
|
||
ESP_LOGI(TAG,"EVENT_Motor_Sleep");
|
||
Motor_GO(0xff);
|
||
Set_gpio_state(&STB,0);
|
||
// Sleep_mode = 1;
|
||
xEventGroupClearBits(Printer_group,EVENT_Motor_Sleep);
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
if(bits != EVENT_Motor_Sleep)
|
||
{
|
||
Sleep_mode = 0;
|
||
}
|
||
}
|
||
}
|
||
|
||
void Printer_run_init()
|
||
{
|
||
Motor_is_Running = xSemaphoreCreateMutex();
|
||
Sys_heap_Init();
|
||
Heat58MM.Heat_Page_buff = malloc(TH_ACTUAL_WIDTH*500*8);
|
||
Heat58MM.Heat_block_buff = malloc(TH_ACTUAL_WIDTH*100*8);
|
||
memset(&Heat58MM.Heat_Page_buff[0],0x00,TH_ACTUAL_WIDTH*500*8);
|
||
memset(&Heat58MM.Heat_block_buff[0],0x00,TH_ACTUAL_WIDTH*100*8);
|
||
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
|
||
} |