Add pen state control

This commit is contained in:
fruchti 2020-11-28 23:27:06 +01:00
parent 0411a9af38
commit a8392b92a9
4 changed files with 44 additions and 3 deletions

View file

@ -1 +1 @@
246
250

View file

@ -3,6 +3,7 @@
// Port A
#define PIN_OUTPUT_X 8 // PA8 - TIM1_CH1
#define PIN_OUTPUT_Y 9 // PA9 - TIM1_CH2
#define PIN_PEN_STATE 10 // PA10
#define PIN_USB_DM 11 // PA11 - USB_DM
#define PIN_USB_DP 12 // PA12 - USB_DP
// #define PIN_USB_PULLUP 15 // PA15 - 1.5 kΩ to D+

View file

@ -7,10 +7,13 @@
MODULE_OWNS_PIN(GPIOA, PIN_OUTPUT_X);
MODULE_OWNS_PIN(GPIOA, PIN_OUTPUT_Y);
MODULE_OWNS_PIN(GPIOA, PIN_PEN_STATE);
static HPGL_Movement_t Output_Buffer[CONFIG_BUFFER_MOVEMENTS];
static const HPGL_Movement_t *Output_BufferRead = Output_Buffer;
static HPGL_Movement_t *Output_BufferWrite = Output_Buffer;
static bool Output_PenIsDown;
static unsigned int Output_DelayCounter = 0;
typedef struct
{
@ -52,6 +55,18 @@ bool Output_EnqueueMovement(HPGL_Movement_t movement)
return true;
}
static void Output_PenDown(void)
{
GPIOA->BRR = (1 << PIN_PEN_STATE);
Output_PenIsDown = true;
}
static void Output_PenUp(void)
{
GPIOA->BSRR = (1 << PIN_PEN_STATE);
Output_PenIsDown = false;
}
static bool Output_FetchNextPoint(void)
{
if(Output_BufferRead == Output_BufferWrite)
@ -60,6 +75,20 @@ static bool Output_FetchNextPoint(void)
}
const HPGL_Movement_t *movement = Output_BufferRead;
if(movement->pen_down != Output_PenIsDown)
{
if(movement->pen_down)
{
Output_PenDown();
}
else
{
Output_PenUp();
}
Output_DelayCounter = OUTPUT_PEN_DELAY;
return false;
}
Output_TargetPoint.x = movement->x;
Output_TargetPoint.y = movement->y;
// TODO: Handle pen state
@ -94,7 +123,7 @@ static int Output_ApproximateLength(int dx, int dy)
}
}
static void Output_Position()
static void Output_Position(void)
{
uint16_t compare_x = OUTPUT_PWM_OFFSET + Output_CurrentPoint.x;
uint16_t compare_y = OUTPUT_PWM_OFFSET + Output_CurrentPoint.y;
@ -105,6 +134,12 @@ static void Output_Position()
void Output_Tick(void)
{
if(Output_DelayCounter > 0)
{
Output_DelayCounter--;
return;
}
int step_length = Output_CurrentVelocity;
while(true)
{
@ -138,11 +173,15 @@ void Output_Init(void)
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
Output_PenUp();
GPIOA->CRH = (GPIOA->CRH
& ~(0xf << (4 * PIN_OUTPUT_X - 32))
& ~(0xf << (4 * PIN_OUTPUT_Y - 32)))
& ~(0xf << (4 * PIN_OUTPUT_Y - 32))
& ~(0xf << (4 * PIN_PEN_STATE - 32)))
| (0xa << (4 * PIN_OUTPUT_X - 32)) // AF output, 2 MHz
| (0xa << (4 * PIN_OUTPUT_Y - 32)) // AF output, 2 MHz
| (0x6 << (4 * PIN_PEN_STATE - 32)) // OD output, 2 MHz
;
TIM1->CCMR1 = TIM_CCMR1_OC1PE | TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1

View file

@ -4,6 +4,7 @@
#define OUTPUT_PWM_RESOLUTION 15 // In bits
#define OUTPUT_PWM_OFFSET 300 // Minimum value
#define OUTPUT_PEN_DELAY 200 // In timer ticks
#define OUTPUT_RESOLUTION 14 // In bits
#define OUTPUT_COORDINATE_LIMIT ((1 << OUTPUT_RESOLUTION) - 1)