Test command parser state machine
This commit is contained in:
56
src/main.c
56
src/main.c
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user