334 lines
9.4 KiB
C
334 lines
9.4 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";
|
|
|
|
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;
|
|
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,
|
|
};
|
|
|
|
|
|
/********************************* 定义系统全局变量 *****************************/
|
|
|
|
|
|
|
|
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;
|
|
|
|
extern const char Font24x24ASCII[];
|
|
|
|
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;
|
|
memset(&Heat58MM.Heat_Page_buff[0],0xff,TH_ACTUAL_WIDTH*3);
|
|
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;
|
|
}
|
|
|
|
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_block2Page_Buff(50,24*50,(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:
|
|
/* code */
|
|
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;
|
|
}
|
|
}
|
|
|
|
|
|
void Printer_Run_task(void *pvParameter)
|
|
{
|
|
uint16_t counts = 0;
|
|
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);
|
|
xSemaphoreGive(Motor_is_Running);//解锁
|
|
}
|
|
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:
|
|
//开始加热
|
|
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:
|
|
ESP_LOGI(TAG,"EVENT_Motor_Sleep");
|
|
Motor_GO(0xff);
|
|
xEventGroupClearBits(Printer_group,EVENT_Motor_Sleep);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
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
|
|
} |