Handle numeric arguments for TEMP/DEWP commands
This commit is contained in:
9
README
9
README
@@ -15,11 +15,12 @@
|
|||||||
|
|
||||||
- RUN
|
- RUN
|
||||||
- STOP
|
- STOP
|
||||||
- TEMP <float>
|
- TEMP <num>
|
||||||
- DEWP <float>
|
- DEWP <num>
|
||||||
|
|
||||||
Decimal point for numbers is optional. Make sure you
|
Decimal point for numbers is currently not supported.
|
||||||
have disabled hardware flow control on the client side.
|
Make sure you have disabled hardware flow control on
|
||||||
|
the client side.
|
||||||
|
|
||||||
TODO: Do we need to handle backspaces for interactive
|
TODO: Do we need to handle backspaces for interactive
|
||||||
serial consoles?! Overkill?!
|
serial consoles?! Overkill?!
|
||||||
|
|||||||
@@ -9,4 +9,9 @@
|
|||||||
#include <util/delay.h>
|
#include <util/delay.h>
|
||||||
#define Sleep(ms) _delay_ms(ms)
|
#define Sleep(ms) _delay_ms(ms)
|
||||||
|
|
||||||
|
void Enable(void);
|
||||||
|
void Disable(void);
|
||||||
|
void SetTemp(long val);
|
||||||
|
void SetDewp(long val);
|
||||||
|
|
||||||
#endif // MAD_COMMON_H
|
#endif // MAD_COMMON_H
|
||||||
|
|||||||
49
src/main.c
49
src/main.c
@@ -2,22 +2,63 @@
|
|||||||
#include "serial.h"
|
#include "serial.h"
|
||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
|
|
||||||
|
bool running;
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
unsigned long i;
|
|
||||||
char ch;
|
char ch;
|
||||||
|
unsigned long i = 1;
|
||||||
|
|
||||||
|
running = true;
|
||||||
USART_Init();
|
USART_Init();
|
||||||
|
|
||||||
for (i = 1;; i++) {
|
for (;;) {
|
||||||
if (i >= 99999) i = 1;
|
// Process rx ring buffer
|
||||||
USART_Printf("[CORE] Fetching sensors #%05lu...\r\n", i);
|
|
||||||
while ((ch = USART_GetChar())) {
|
while ((ch = USART_GetChar())) {
|
||||||
CMD_Parse(ch);
|
CMD_Parse(ch);
|
||||||
}
|
}
|
||||||
|
|
||||||
Sleep(3000);
|
Sleep(3000);
|
||||||
|
|
||||||
|
if (!running)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// TODO: Main program
|
||||||
|
if (i >= 99999) i = 1;
|
||||||
|
USART_Printf("[CORE] Fetching sensors #%05lu...\r\n", i++);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Enable(void)
|
||||||
|
{
|
||||||
|
USART_Printf("[CORE] Parsed 'CMD_RUN' token.\r\n");
|
||||||
|
running = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Disable(void)
|
||||||
|
{
|
||||||
|
USART_Printf("[CORE] Parsed 'CMD_STOP' token.\r\n");
|
||||||
|
running = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetTemp(long val)
|
||||||
|
{
|
||||||
|
USART_Printf("[CORE] Parsed 'CMD_SET_TEMP', VAL='%ld'.\r\n", val);
|
||||||
|
if (val < 10) {
|
||||||
|
USART_Printf("[CORE] Error: Given temperature is too low.\r\n");
|
||||||
|
} else if (val > 40) {
|
||||||
|
USART_Printf("[CORE] Error: Given temperature is too high.\r\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetDewp(long val)
|
||||||
|
{
|
||||||
|
USART_Printf("[CORE] Parsed 'CMD_SET_DEWP', VAL='%ld'.\r\n", val);
|
||||||
|
if (val < 10) {
|
||||||
|
USART_Printf("[CORE] Error: Given dew point is too low.\r\n");
|
||||||
|
} else if (val > 80) {
|
||||||
|
USART_Printf("[CORE] Error: Given dew point is too high.\r\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
149
src/parser.c
149
src/parser.c
@@ -4,15 +4,16 @@
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* # Supported Commands
|
* # Supported Commands
|
||||||
* (Optional decimal point for numbers)
|
|
||||||
*
|
*
|
||||||
* - RUN
|
* - RUN
|
||||||
* - STOP
|
* - STOP
|
||||||
* - TEMP 20
|
* - TEMP <num>
|
||||||
* - DEWP 60.0
|
* - DEWP <num>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static int state;
|
static int state;
|
||||||
|
static char numbuf[8];
|
||||||
|
static size_t numlen;
|
||||||
|
|
||||||
#define PARSE_IDLE 0
|
#define PARSE_IDLE 0
|
||||||
#define PARSE_CMD_RUN 1
|
#define PARSE_CMD_RUN 1
|
||||||
@@ -22,6 +23,8 @@ static int state;
|
|||||||
|
|
||||||
void CMD_Parse(char ch)
|
void CMD_Parse(char ch)
|
||||||
{
|
{
|
||||||
|
long num;
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case PARSE_IDLE:
|
case PARSE_IDLE:
|
||||||
@@ -52,11 +55,11 @@ void CMD_Parse(char ch)
|
|||||||
if (ch == 'T') // TEMP
|
if (ch == 'T') // TEMP
|
||||||
state = 1;
|
state = 1;
|
||||||
if (ch == 'D') // DEWP
|
if (ch == 'D') // DEWP
|
||||||
state = 5;
|
state = 6;
|
||||||
if (ch == 'R') // RUN
|
if (ch == 'R') // RUN
|
||||||
state = 9;
|
state = 11;
|
||||||
if (ch == 'S') // STOP
|
if (ch == 'S') // STOP
|
||||||
state = 12;
|
state = 14;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Parse 'TEMP'
|
// Parse 'TEMP'
|
||||||
@@ -80,85 +83,133 @@ void CMD_Parse(char ch)
|
|||||||
break;
|
break;
|
||||||
case 4:
|
case 4:
|
||||||
if (ch == ' ') {
|
if (ch == ' ') {
|
||||||
// XXX: Just so the web server can show something...
|
numlen = 0;
|
||||||
USART_Printf("[CORE] Parsed 'CMD_SET_TEMP' token.\r\n");
|
state = 5;
|
||||||
|
} else {
|
||||||
|
state = 0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
// Error: Number too long
|
||||||
|
if (numlen >= sizeof(numbuf) - 1) {
|
||||||
|
state = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse numeric string
|
||||||
|
else if ((ch >= '0' && ch <= '9') || (!numlen && ch == '-')) {
|
||||||
|
numbuf[numlen++] = ch;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse terminator and strip decimal
|
||||||
|
else if (ch == '\n' || ch == '.' || ch == ',') {
|
||||||
|
numbuf[numlen] = '\0';
|
||||||
|
num = strtol(numbuf, NULL, 10);
|
||||||
|
SetTemp(num);
|
||||||
|
state = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unexpected character
|
||||||
|
else {
|
||||||
|
USART_Printf("[CORE] Error: Unexpected character '%c'.\r\n", ch);
|
||||||
|
state = 0;
|
||||||
}
|
}
|
||||||
// TODO: Parse float value
|
|
||||||
state = 0;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Parse 'DEWP'
|
// Parse 'DEWP'
|
||||||
case 5:
|
|
||||||
if (ch == 'E')
|
|
||||||
state = 6;
|
|
||||||
else
|
|
||||||
state = 0;
|
|
||||||
break;
|
|
||||||
case 6:
|
case 6:
|
||||||
if (ch == 'W')
|
if (ch == 'E')
|
||||||
state = 7;
|
state = 7;
|
||||||
else
|
else
|
||||||
state = 0;
|
state = 0;
|
||||||
break;
|
break;
|
||||||
case 7:
|
case 7:
|
||||||
if (ch == 'P')
|
if (ch == 'W')
|
||||||
state = 8;
|
state = 8;
|
||||||
else
|
else
|
||||||
state = 0;
|
state = 0;
|
||||||
break;
|
break;
|
||||||
case 8:
|
case 8:
|
||||||
|
if (ch == 'P')
|
||||||
|
state = 9;
|
||||||
|
else
|
||||||
|
state = 0;
|
||||||
|
break;
|
||||||
|
case 9:
|
||||||
if (ch == ' ') {
|
if (ch == ' ') {
|
||||||
// XXX: Just so the web server can show something...
|
numlen = 0;
|
||||||
USART_Printf("[CORE] Parsed 'CMD_SET_DEWP' token.\r\n");
|
state = 10;
|
||||||
|
} else {
|
||||||
|
state = 0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 10:
|
||||||
|
// Error: Number too long
|
||||||
|
if (numlen >= sizeof(numbuf) - 1) {
|
||||||
|
state = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse numeric string
|
||||||
|
else if ((ch >= '0' && ch <= '9') || (!numlen && ch == '-')) {
|
||||||
|
numbuf[numlen++] = ch;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse terminator and strip decimal
|
||||||
|
else if (ch == '\n' || ch == '.' || ch == ',') {
|
||||||
|
numbuf[numlen] = '\0';
|
||||||
|
num = strtol(numbuf, NULL, 10);
|
||||||
|
SetDewp(num);
|
||||||
|
state = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unexpected character
|
||||||
|
else {
|
||||||
|
USART_Printf("[CORE] Error: Unexpected character '%c'.\r\n", ch);
|
||||||
|
state = 0;
|
||||||
}
|
}
|
||||||
// TODO: Parse float value
|
|
||||||
state = 0;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Parse 'RUN'
|
// Parse 'RUN'
|
||||||
case 9:
|
|
||||||
if (ch == 'U')
|
|
||||||
state = 10;
|
|
||||||
else
|
|
||||||
state = 0;
|
|
||||||
break;
|
|
||||||
case 10:
|
|
||||||
if (ch == 'N')
|
|
||||||
state = 11;
|
|
||||||
else
|
|
||||||
state = 0;
|
|
||||||
break;
|
|
||||||
case 11:
|
case 11:
|
||||||
if (ch == '\n') {
|
if (ch == 'U')
|
||||||
// XXX: Just so the web server can show something...
|
state = 12;
|
||||||
USART_Printf("[CORE] Parsed 'CMD_RUN' token.\r\n");
|
else
|
||||||
}
|
state = 0;
|
||||||
state = 0;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Parse 'STOP'
|
|
||||||
case 12:
|
case 12:
|
||||||
if (ch == 'T')
|
if (ch == 'N')
|
||||||
state = 13;
|
state = 13;
|
||||||
else
|
else
|
||||||
state = 0;
|
state = 0;
|
||||||
break;
|
break;
|
||||||
case 13:
|
case 13:
|
||||||
if (ch == 'O')
|
if (ch == '\n') {
|
||||||
state = 14;
|
Enable();
|
||||||
else
|
}
|
||||||
state = 0;
|
state = 0;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
// Parse 'STOP'
|
||||||
case 14:
|
case 14:
|
||||||
if (ch == 'P')
|
if (ch == 'T')
|
||||||
state = 15;
|
state = 15;
|
||||||
else
|
else
|
||||||
state = 0;
|
state = 0;
|
||||||
break;
|
break;
|
||||||
case 15:
|
case 15:
|
||||||
|
if (ch == 'O')
|
||||||
|
state = 16;
|
||||||
|
else
|
||||||
|
state = 0;
|
||||||
|
break;
|
||||||
|
case 16:
|
||||||
|
if (ch == 'P')
|
||||||
|
state = 17;
|
||||||
|
else
|
||||||
|
state = 0;
|
||||||
|
break;
|
||||||
|
case 17:
|
||||||
if (ch == '\n') {
|
if (ch == '\n') {
|
||||||
// XXX: Just so the web server can show something...
|
Disable();
|
||||||
USART_Printf("[CORE] Parsed 'CMD_STOP' token.\r\n");
|
|
||||||
}
|
}
|
||||||
state = 0;
|
state = 0;
|
||||||
break;
|
break;
|
||||||
|
|||||||
Reference in New Issue
Block a user