Prevent redundant writes and start implementing parser timeout
This commit is contained in:
@@ -3,21 +3,8 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
static void Puts(const char *str)
|
||||
{
|
||||
while (*str != '\0') {
|
||||
USART_Putc(*str++);
|
||||
}
|
||||
}
|
||||
|
||||
static void PrintArgs(const char *fmt, va_list ap)
|
||||
{
|
||||
char msg[256];
|
||||
|
||||
vsnprintf(msg, sizeof(msg), fmt, ap);
|
||||
|
||||
Puts(msg);
|
||||
}
|
||||
static void Puts(const char *str);
|
||||
static void PrintArgs(const char *fmt, va_list ap);
|
||||
|
||||
void Print(const char *fmt, ...)
|
||||
{
|
||||
@@ -51,3 +38,19 @@ void Error(const char *fmt, ...)
|
||||
|
||||
// XXX: Force reset?
|
||||
}
|
||||
|
||||
static void Puts(const char *str)
|
||||
{
|
||||
while (*str != '\0') {
|
||||
USART_Putc(*str++);
|
||||
}
|
||||
}
|
||||
|
||||
static void PrintArgs(const char *fmt, va_list ap)
|
||||
{
|
||||
char msg[256];
|
||||
|
||||
vsnprintf(msg, sizeof(msg), fmt, ap);
|
||||
|
||||
Puts(msg);
|
||||
}
|
||||
|
||||
@@ -3,27 +3,44 @@
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <avr/io.h>
|
||||
|
||||
#define CMD_MAX_LEN 128
|
||||
|
||||
// TODO: Write documentation.
|
||||
// TODO: Implement start and stop commands.
|
||||
// TODO: Reset command buffer on timeout?
|
||||
// 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);
|
||||
static void StartTimer(void);
|
||||
static bool IsTimedOut(void);
|
||||
static void StopTimer(void);
|
||||
|
||||
bool CMD_Parse(char ch, cmd_t *out)
|
||||
{
|
||||
bool is_valid;
|
||||
|
||||
if (ch == '\n' || ch == '\r') {
|
||||
// FIXME: Reset command buffer on timeout:
|
||||
// No newline character is received if the serial
|
||||
// buffer is full and the ending is discarded. This
|
||||
// means that after a command that is too long, the
|
||||
// next command will not be parsed properly.
|
||||
|
||||
if (head == cmdbuf) {
|
||||
StartTimer();
|
||||
}
|
||||
|
||||
if (IsTimedOut() ||
|
||||
(ch == '\n' || ch == '\r')) {
|
||||
is_valid = ParseLine(out);
|
||||
tail = cmdbuf + CMD_MAX_LEN - 1;
|
||||
head = cmdbuf;
|
||||
StopTimer();
|
||||
|
||||
return is_valid;
|
||||
}
|
||||
|
||||
@@ -70,3 +87,20 @@ static bool ParseLine(cmd_t *out)
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static void StartTimer(void)
|
||||
{
|
||||
TCNT0 = 0x00;
|
||||
TCCR0 = BIT(CS00) | BIT(CS02);
|
||||
}
|
||||
|
||||
static bool IsTimedOut(void)
|
||||
{
|
||||
return false; // TIFR & BIT(TOV0)
|
||||
}
|
||||
|
||||
static void StopTimer(void)
|
||||
{
|
||||
TCNT0 = 0x00;
|
||||
TIFR = BIT(TOV0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user