Move parser to separate module and add USART_IsDataAvailable
This commit is contained in:
52
src/main.c
52
src/main.c
@@ -1,52 +1,6 @@
|
||||
#include "common.h"
|
||||
#include "serial.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;
|
||||
}
|
||||
}
|
||||
#include "parser.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
@@ -58,8 +12,10 @@ int main(void)
|
||||
for (i = 1;; i++) {
|
||||
if (i >= 99999) i = 1;
|
||||
|
||||
if (USART_IsDataAvailable()) {
|
||||
while ((ch = USART_GetChar())) {
|
||||
parse(ch);
|
||||
CMD_Parse(ch);
|
||||
}
|
||||
}
|
||||
|
||||
USART_Printf("[CORE] Fetching sensors #%05lu...\r\n", i);
|
||||
|
||||
44
src/parser.c
Normal file
44
src/parser.c
Normal 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
6
src/parser.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef MAD_PARSER_H
|
||||
#define MAD_PARSER_H
|
||||
|
||||
void CMD_Parse(char ch);
|
||||
|
||||
#endif // MAD_PARSER_H
|
||||
@@ -1,8 +1,8 @@
|
||||
#include "serial.h"
|
||||
#include "common.h"
|
||||
#include "serial.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
|
||||
@@ -66,6 +66,11 @@ void USART_Printf(const char *fmt, ...)
|
||||
}
|
||||
}
|
||||
|
||||
bool USART_IsDataAvailable(void)
|
||||
{
|
||||
return rxhead != rxtail;
|
||||
}
|
||||
|
||||
// Interrupt Handler
|
||||
ISR(USART_RXC_vect)
|
||||
{
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
#ifndef MAD_SERIAL_H
|
||||
#define MAD_SERIAL_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user