Move parser to separate module and add USART_IsDataAvailable

This commit is contained in:
2024-08-27 15:09:06 +02:00
parent c716137e1a
commit e64880f66c
5 changed files with 65 additions and 51 deletions

View File

@@ -1,52 +1,6 @@
#include "common.h" #include "common.h"
#include "serial.h" #include "serial.h"
#include "parser.h"
#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) int main(void)
{ {
@@ -58,8 +12,10 @@ int main(void)
for (i = 1;; i++) { for (i = 1;; i++) {
if (i >= 99999) i = 1; if (i >= 99999) i = 1;
while ((ch = USART_GetChar())) { if (USART_IsDataAvailable()) {
parse(ch); while ((ch = USART_GetChar())) {
CMD_Parse(ch);
}
} }
USART_Printf("[CORE] Fetching sensors #%05lu...\r\n", i); USART_Printf("[CORE] Fetching sensors #%05lu...\r\n", i);

44
src/parser.c Normal file
View File

@@ -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;
}
}

6
src/parser.h Normal file
View File

@@ -0,0 +1,6 @@
#ifndef MAD_PARSER_H
#define MAD_PARSER_H
void CMD_Parse(char ch);
#endif // MAD_PARSER_H

View File

@@ -1,8 +1,8 @@
#include "serial.h"
#include "common.h" #include "common.h"
#include "serial.h"
#include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h>
#include <avr/io.h> #include <avr/io.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
@@ -66,6 +66,11 @@ void USART_Printf(const char *fmt, ...)
} }
} }
bool USART_IsDataAvailable(void)
{
return rxhead != rxtail;
}
// Interrupt Handler // Interrupt Handler
ISR(USART_RXC_vect) ISR(USART_RXC_vect)
{ {

View File

@@ -1,9 +1,12 @@
#ifndef MAD_SERIAL_H #ifndef MAD_SERIAL_H
#define MAD_SERIAL_H #define MAD_SERIAL_H
#include <stdbool.h>
void USART_Init(void); void USART_Init(void);
char USART_GetChar(void); char USART_GetChar(void);
void USART_PutChar(const char ch); void USART_PutChar(const char ch);
void USART_Printf(const char *fmt, ...); void USART_Printf(const char *fmt, ...);
bool USART_IsDataAvailable(void);
#endif // MAD_SERIAL_H #endif // MAD_SERIAL_H