Improve print state machine, add print API

This commit is contained in:
fruchti 2018-08-04 08:54:25 +02:00
parent 17962b29ce
commit 064cdc5ba0
4 changed files with 85 additions and 57 deletions

View file

@ -1 +1 @@
138 155

View file

@ -1,8 +1,7 @@
#include "ltp1245.h" #include "ltp1245.h"
#include "pinning.h" #include "pinning.h"
#define LINEWIDTH (64 * 6) uint8_t LTP1245_Buffer[LTP1245_LINEWIDTH / 8 * LTP1245_BUFFER_LINES];
uint8_t LTP1245_Buffer[LINEWIDTH / 8 * LTP1245_BUFFER_LINES];
typedef struct State_t typedef struct State_t
{ {
@ -12,9 +11,15 @@ typedef struct State_t
static volatile int Stepper_Delta = 0; static volatile int Stepper_Delta = 0;
static int PrintLines; static int PrintLines;
static int CurrentBufferLine; static int CurrentBufferLine;
bool Printing = false;
static volatile int PulseWidth = 2000; static volatile int PulseWidth = 2000;
// Main State machine states
static State_t State_Idle(void);
static State_t State_PaperLoad(void);
static State_t State_Printing(void);
static State_t State_PaperFeed(void);
static volatile State_t State = {State_Idle};
static void InitStepper(void) static void InitStepper(void)
{ {
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN; RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
@ -169,18 +174,10 @@ static void SendLine(uint8_t *line)
while(DMA1_Channel5->CNDTR); while(DMA1_Channel5->CNDTR);
DMA1_Channel5->CCR &= ~DMA_CCR_EN; DMA1_Channel5->CCR &= ~DMA_CCR_EN;
DMA1_Channel5->CMAR = (uint32_t)line; DMA1_Channel5->CMAR = (uint32_t)line;
DMA1_Channel5->CNDTR = LINEWIDTH / 8; DMA1_Channel5->CNDTR = LTP1245_LINEWIDTH / 8;
DMA1_Channel5->CCR |= DMA_CCR_EN; DMA1_Channel5->CCR |= DMA_CCR_EN;
} }
// Main state machine states
static State_t State_Idle(void);
static State_t State_PaperLoad(void);
static State_t State_InitialPaperFeed(void);
static State_t State_Printing(void);
static State_t State_FinalPaperFeed(void);
// Automatic paper loading // Automatic paper loading
static State_t State_PaperLoad(void) static State_t State_PaperLoad(void)
{ {
@ -205,22 +202,11 @@ static State_t State_PaperLoad(void)
return (State_t){State_PaperLoad}; return (State_t){State_PaperLoad};
} }
// Paper feed before printing
static State_t State_InitialPaperFeed(void)
{
if(Stepper_Delta == 0)
{
return (State_t){State_Printing};
}
return (State_t){State_InitialPaperFeed};
}
// Actual printing // Actual printing
static State_t State_Printing(void) static State_t State_Printing(void)
{ {
if(!HeadDown() || !HasPaper()) if(!HeadDown() || !HasPaper())
{ {
Printing = false;
return (State_t){State_Idle}; return (State_t){State_Idle};
} }
@ -230,13 +216,9 @@ static State_t State_Printing(void)
} }
else if(Stepper_Delta == 0) else if(Stepper_Delta == 0)
{ {
SendLine(LTP1245_Buffer + CurrentBufferLine * LINEWIDTH / 8); SendLine(LTP1245_Buffer + CurrentBufferLine * LTP1245_LINEWIDTH / 8);
ActivateHead(3); ActivateHead(3);
CurrentBufferLine++; CurrentBufferLine++;
if(CurrentBufferLine >= LTP1245_BUFFER_LINES)
{
CurrentBufferLine = 0;
}
PrintLines--; PrintLines--;
@ -246,27 +228,26 @@ static State_t State_Printing(void)
} }
else else
{ {
Stepper_Delta = 500; // Print done
return (State_t){State_FinalPaperFeed}; Stepper_Delta = 1;
return (State_t){State_Idle};
} }
} }
return (State_t){State_Printing}; return (State_t){State_Printing};
} }
// Paper feed after printing static State_t State_PaperFeed(void)
static State_t State_FinalPaperFeed(void)
{ {
if(!HasPaper() || !HeadDown()) if(!HasPaper())
{ {
Printing = false;
return (State_t){State_Idle}; return (State_t){State_Idle};
} }
if(Stepper_Delta == 0) if(Stepper_Delta == 0)
{ {
Printing = false;
return (State_t){State_Idle}; return (State_t){State_Idle};
} }
return (State_t){State_FinalPaperFeed}; return (State_t){State_PaperFeed};
} }
static State_t State_Idle(void) static State_t State_Idle(void)
@ -275,19 +256,54 @@ static State_t State_Idle(void)
{ {
return (State_t){State_PaperLoad}; return (State_t){State_PaperLoad};
} }
if(Printing)
{
Stepper_Delta = 200;
return (State_t){State_InitialPaperFeed};
}
return (State_t){State_Idle}; return (State_t){State_Idle};
} }
void LTP1245_Print(void) LTP1245_Result_t LTP1245_Print(uint8_t *data, int lines)
{ {
PrintLines = LTP1245_BUFFER_LINES * 5; do
{
if(!HasPaper())
{
return LTP1245_NO_PAPER;
}
if(!HeadDown())
{
return LTP1245_HEAD_UP;
}
} while(State.fn != State_Idle);
if(lines > LTP1245_BUFFER_LINES)
{
lines = LTP1245_BUFFER_LINES;
}
memcpy(LTP1245_Buffer, data, lines * LTP1245_LINEWIDTH / 8);
PrintLines = lines;
CurrentBufferLine = 0; CurrentBufferLine = 0;
Printing = true; State = (State_t){State_Printing};
return LTP1245_OK;
}
LTP1245_Result_t LTP1245_FeedPaper(int lines)
{
do
{
if(!HasPaper())
{
return LTP1245_NO_PAPER;
}
if(!HeadDown())
{
return LTP1245_HEAD_UP;
}
} while(State.fn != State_Idle);
Stepper_Delta = lines;
State = (State_t){State_PaperFeed};
return LTP1245_OK;
} }
void LTP1245_Init(void) void LTP1245_Init(void)
@ -296,21 +312,11 @@ void LTP1245_Init(void)
InitSensors(); InitSensors();
InitStepper(); InitStepper();
InitThermistor(); InitThermistor();
for(int i = 0; i < sizeof(LTP1245_Buffer); i++)
{
int shift = (i * 8 / (64 * 6)) % 8;
LTP1245_Buffer[i] =(0x11 << shift) | (0x11 >> (8 - shift));
}
LTP1245_Print();
} }
void AdvanceStateMachine(void) void AdvanceStateMachine(void)
{ {
static State_t state = {State_Idle}; State = State.fn();
state = state.fn();
} }
void TIM3_IRQHandler(void) void TIM3_IRQHandler(void)

View file

@ -1,14 +1,23 @@
#pragma once #pragma once
#include <stdbool.h> #include <stdbool.h>
#include <string.h>
#include "stm32f1xx.h" #include "stm32f1xx.h"
typedef int (*LTP1245_DataProvider_t)(uint8_t *buffer); typedef enum
{
LTP1245_OK,
LTP1245_NO_PAPER,
LTP1245_HEAD_UP
} LTP1245_Result_t;
#define LTP1245_MAX_DRIVE_FREQ 473 // In Hz #define LTP1245_MAX_DRIVE_FREQ 473 // In Hz
#define LTP1245_BUFFER_LINES 64 #define LTP1245_BUFFER_LINES 64
#define LTP1245_LINEWIDTH 384 // In pixels
#define LTP1245_TH_REXT 10 // In kΩ #define LTP1245_TH_REXT 10 // In kΩ
void LTP1245_Init(void); void LTP1245_Init(void);
LTP1245_Result_t LTP1245_FeedPaper(int lines);
LTP1245_Result_t LTP1245_Print(uint8_t *data, int lines);

View file

@ -5,6 +5,19 @@ int main(void)
USB_Init(); USB_Init();
LTP1245_Init(); LTP1245_Init();
LTP1245_FeedPaper(10);
uint8_t buff[LTP1245_LINEWIDTH / 8 * 16];
for(int i = 0; i < sizeof(buff); i++)
{
int shift = (i * 8 / (64 * 6)) % 8;
buff[i] = (0x11 << shift) | (0x11 >> (8 - shift));
}
LTP1245_Print(buff, 16);
LTP1245_Print(buff, 5);
LTP1245_FeedPaper(10);
for(;;) for(;;)
{ {
} }