From ce2af6682f399131acb14f6bf1c025b83631b12b Mon Sep 17 00:00:00 2001 From: Leon Krieg Date: Tue, 27 Aug 2024 17:22:34 +0200 Subject: [PATCH] Start implementing proper parser state machine --- src/parser.c | 59 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 11 deletions(-) diff --git a/src/parser.c b/src/parser.c index 736956e..7912826 100644 --- a/src/parser.c +++ b/src/parser.c @@ -2,18 +2,51 @@ #include "serial.h" #include "parser.h" -// # Supported Commands -// (Optional decimal point for numbers) -// - START -// - RESET -// - STOP -// - SET TEMP 20 -// - SET DEWP 60.0 +/* + * # Supported Commands + * (Optional decimal point for numbers) + * + * - RUN + * - STOP + * - TEMP 20 + * - DEWP 60.0 + */ static int state; +#define PARSE_IDLE 0 +#define PARSE_CMD_RUN 1 +#define PARSE_CMD_STOP 2 +#define PARSE_CMD_SET_TEMP 3 +#define PARSE_CMD_SET_DEWP 4 + void CMD_Parse(char ch) { + #if 0 + switch (state) { + case PARSE_IDLE: + if (ch == 'R') + state = PARSE_CMD_RUN; + else if (ch == 'S') + state = PARSE_CMD_STOP; + else if (ch == 'T') + state = PARSE_CMD_SET_TEMP; + else if (ch == 'D') + state = PARSE_CMD_SET_DEWP; + } + break; + case PARSE_CMD_RUN: + break; + case PARSE_CMD_STOP: + break; + case PARSE_CMD_SET_TEMP: + break; + case PARSE_CMD_SET_DEWP: + break; + } + #endif + + // Placeholder switch (state) { case 0: if (ch == 't') @@ -24,21 +57,25 @@ void CMD_Parse(char ch) state = 2; else state = 0; + break; case 2: if (ch == 's') state = 3; else state = 0; + break; case 3: if (ch == 't') state = 4; else state = 0; + break; case 4: if (ch == '\n') { - USART_Printf("[CORE] Parsed 'test'!\r\n"); - state = 0; - } else - state = 0; + // XXX: Just so the web server can show something... + USART_Printf("[CORE] Parsed 'CMD_SET_TEMP' token.\r\n"); + USART_Printf("[CORE] Parsed 'CMD_SET_DEWP' token.\r\n"); + } + state = 0; } }