Test command parser state machine

This commit is contained in:
2024-08-27 14:35:31 +02:00
parent 9638d10830
commit a2b77e8db6
3 changed files with 61 additions and 7 deletions

8
README Normal file
View File

@@ -0,0 +1,8 @@
# USART Commands
Instead of polling for input we use an interrupt vector
to write data to a rx ring buffer when it's available.
We parse commands using a state machine so we can deal
with small chunks of characters at a time and spit out
the parsed command when the transmission has finished.

View File

@@ -3,24 +3,66 @@
#include <avr/interrupt.h>
// # Serial Input Data Flow
// USART INTERRUPT -> RING BUFFER -> COMMAND PARSER STATE MACHINE
//
// # Supported Commands
// (Optional decimal point for numbers)
// - START
// - RESET
// - STOP
// - SET TEMP 20
// - SET DEWP 60.0
static int state;
void parse(char ch)
{
switch (state) {
case 0:
if (ch == 't')
state = 1;
break;
case 1:
if (ch == 'e')
state = 2;
else
state = 0;
case 2:
if (ch == 's')
state = 3;
else
state = 0;
case 3:
if (ch == 't')
state = 4;
else
state = 0;
case 4:
if (ch == '\n') {
USART_Printf("[CORE] Parsed 'test'!\r\n");
state = 0;
} else
state = 0;
}
}
int main(void)
{
unsigned long i;
char ch;
// Handle USART_RXC interrupt
UCSRB |= (1 << RXCIE);
sei();
USART_Init();
for (i = 1;; i++) {
if (i >= 99999) i = 1;
ch = USART_GetChar();
while ((ch = USART_GetChar())) {
parse(ch);
}
USART_Printf("[CORE] Fetching sensors #%05lu...\r\n", i);
USART_Printf("[CORE] USART_GetChar() = '%c'\r\n",
(ch) ? ch : ' ');
Sleep(3000);
}

View File

@@ -20,6 +20,10 @@ void USART_Init(void)
rxhead = 0;
rxtail = 0;
// Handle RXC interrupt
UCSRB |= (1 << RXCIE);
sei();
UCSRB |= BIT(RXEN) | BIT(TXEN); // Enable rx and tx circuitry
UCSRC |= BIT(URSEL) | BIT(UCSZ0) | BIT(UCSZ1); // 8-bit chars
UBRRH = (USART_BAUD_PRESCALE >> 8); // Baud Rate upper byte