Merge pull request #5 from madcow/parser
Implement serial command parser with floating point support
This commit is contained in:
72
src/common/parser.c
Normal file
72
src/common/parser.c
Normal file
@@ -0,0 +1,72 @@
|
||||
#include "common.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define CMD_MAX_LEN 128
|
||||
|
||||
// TODO: Write documentation.
|
||||
// TODO: Implement start and stop commands.
|
||||
// TODO: Reset command buffer on timeout?
|
||||
|
||||
static char cmdbuf[CMD_MAX_LEN + 1];
|
||||
static char *tail = cmdbuf + CMD_MAX_LEN - 1;
|
||||
static char *head = cmdbuf;
|
||||
|
||||
static bool ParseLine(cmd_t *out);
|
||||
|
||||
bool CMD_Parse(char ch, cmd_t *out)
|
||||
{
|
||||
bool is_valid;
|
||||
|
||||
if (ch == '\n' || ch == '\r') {
|
||||
is_valid = ParseLine(out);
|
||||
tail = cmdbuf + CMD_MAX_LEN - 1;
|
||||
head = cmdbuf;
|
||||
return is_valid;
|
||||
}
|
||||
|
||||
if (head < tail) {
|
||||
*head++ = ch;
|
||||
*head = '\0';
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool ParseLine(cmd_t *out)
|
||||
{
|
||||
head = cmdbuf;
|
||||
|
||||
// Command SET <float> <float>
|
||||
if (tolower(head[0] == 's')) {
|
||||
if ((tolower(head[1]) != 'e') ||
|
||||
(tolower(head[2]) != 't')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
out->type = T_SET;
|
||||
head += 3;
|
||||
|
||||
// Parse <float> arguments
|
||||
for (int i = 0; i < 2; i++) {
|
||||
errno = 0;
|
||||
|
||||
// Try converting string to floating point
|
||||
// Skips whitespace and moves tail pointer
|
||||
out->args[i] = (float) strtod(head, &tail);
|
||||
|
||||
if ((errno == ERANGE) ||
|
||||
(out->args[i] == 0 && head == tail)) {
|
||||
return false; // Conversion failed
|
||||
}
|
||||
|
||||
head = tail;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
18
src/common/parser.h
Normal file
18
src/common/parser.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef MAD_CORE_COMMON_PARSER
|
||||
#define MAD_CORE_COMMON_PARSER
|
||||
|
||||
#include "common/types.h"
|
||||
|
||||
// Command types
|
||||
#define T_SET 0
|
||||
|
||||
struct cmd_s {
|
||||
int type;
|
||||
float args[2];
|
||||
};
|
||||
|
||||
typedef struct cmd_s cmd_t;
|
||||
|
||||
bool CMD_Parse(char ch, cmd_t *out);
|
||||
|
||||
#endif // MAD_CORE_COMMON_PARSER
|
||||
@@ -3,13 +3,15 @@
|
||||
|
||||
#include "common/types.h"
|
||||
|
||||
// Timeout Flags
|
||||
#define WDT2000 0x7 // 2000 ms
|
||||
#define WDT1000 0x6 // 1000 ms
|
||||
#define WDT500 0x5 // 500 ms
|
||||
#define WDT250 0x4 // 250 ms
|
||||
#define WDT100 0x3 // 100 ms
|
||||
// XXX: WDT...
|
||||
// Timeout flags
|
||||
#define WDT2000 0x7 // 2000 ms
|
||||
#define WDT1000 0x6 // 1000 ms
|
||||
#define WDT500 0x5 // 500 ms
|
||||
#define WDT250 0x4 // 250 ms
|
||||
#define WDT100 0x3 // 100 ms
|
||||
#define WDT64 0x2 // 64 ms
|
||||
#define WDT32 0x1 // 32 ms
|
||||
#define WDT16 0x0 // 16 ms
|
||||
|
||||
void WDT_Enable(void);
|
||||
void WDT_SetTimeoutFlag(byte flag);
|
||||
|
||||
Reference in New Issue
Block a user