From e64880f66c751d6f1c5bb18cfad0f1c2eb167b12 Mon Sep 17 00:00:00 2001 From: Leon Krieg Date: Tue, 27 Aug 2024 15:09:06 +0200 Subject: [PATCH] Move parser to separate module and add USART_IsDataAvailable --- src/main.c | 54 +++++----------------------------------------------- src/parser.c | 44 ++++++++++++++++++++++++++++++++++++++++++ src/parser.h | 6 ++++++ src/serial.c | 9 +++++++-- src/serial.h | 3 +++ 5 files changed, 65 insertions(+), 51 deletions(-) create mode 100644 src/parser.c create mode 100644 src/parser.h diff --git a/src/main.c b/src/main.c index adc6b5e..8ba3060 100644 --- a/src/main.c +++ b/src/main.c @@ -1,52 +1,6 @@ #include "common.h" #include "serial.h" - -#include - - -// # 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; - } -} +#include "parser.h" int main(void) { @@ -58,8 +12,10 @@ int main(void) for (i = 1;; i++) { if (i >= 99999) i = 1; - while ((ch = USART_GetChar())) { - parse(ch); + if (USART_IsDataAvailable()) { + while ((ch = USART_GetChar())) { + CMD_Parse(ch); + } } USART_Printf("[CORE] Fetching sensors #%05lu...\r\n", i); diff --git a/src/parser.c b/src/parser.c new file mode 100644 index 0000000..736956e --- /dev/null +++ b/src/parser.c @@ -0,0 +1,44 @@ +#include "common.h" +#include "serial.h" +#include "parser.h" + +// # Supported Commands +// (Optional decimal point for numbers) +// - START +// - RESET +// - STOP +// - SET TEMP 20 +// - SET DEWP 60.0 + +static int state; + +void CMD_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; + } +} diff --git a/src/parser.h b/src/parser.h new file mode 100644 index 0000000..ee28894 --- /dev/null +++ b/src/parser.h @@ -0,0 +1,6 @@ +#ifndef MAD_PARSER_H +#define MAD_PARSER_H + +void CMD_Parse(char ch); + +#endif // MAD_PARSER_H diff --git a/src/serial.c b/src/serial.c index 0a9ec7e..00214c5 100644 --- a/src/serial.c +++ b/src/serial.c @@ -1,8 +1,8 @@ -#include "serial.h" #include "common.h" +#include "serial.h" -#include #include +#include #include #include @@ -66,6 +66,11 @@ void USART_Printf(const char *fmt, ...) } } +bool USART_IsDataAvailable(void) +{ + return rxhead != rxtail; +} + // Interrupt Handler ISR(USART_RXC_vect) { diff --git a/src/serial.h b/src/serial.h index abd7145..44138ed 100644 --- a/src/serial.h +++ b/src/serial.h @@ -1,9 +1,12 @@ #ifndef MAD_SERIAL_H #define MAD_SERIAL_H +#include + void USART_Init(void); char USART_GetChar(void); void USART_PutChar(const char ch); void USART_Printf(const char *fmt, ...); +bool USART_IsDataAvailable(void); #endif // MAD_SERIAL_H