diff --git a/src/common.c b/src/common.c index 6c8de42..d32313f 100644 --- a/src/common.c +++ b/src/common.c @@ -1 +1,29 @@ -void *TODO; +#include "common.h" +#include "serial.h" + +#define PREFIX_ERROR(fmt) "Error:" fmt + +static bool state; + +void Info(const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + USART_Print(state, "", fmt, args); + va_end(args); +} + +void Error(const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + USART_Print(state, "Error: ", fmt, args); + va_end(args); +} + +void SetStateFlag(bool is_running) +{ + state = is_running; +} diff --git a/src/common.h b/src/common.h index be3c756..9a26b73 100644 --- a/src/common.h +++ b/src/common.h @@ -5,10 +5,16 @@ #define UNUSED(s) (void)(s) #include +#include #include + #include #define Sleep(ms) _delay_ms(ms) +void Info(const char *fmt, ...); +void Error(const char *fmt, ...); +void SetStateFlag(bool is_running); + void Enable(void); void Disable(void); void SetTemp(long val); diff --git a/src/main.c b/src/main.c index 8efc39d..f03070a 100644 --- a/src/main.c +++ b/src/main.c @@ -9,8 +9,8 @@ int main(void) char ch; unsigned long i = 1; - running = true; USART_Init(); + Enable(); for (;;) { // Process rx ring buffer @@ -19,13 +19,11 @@ int main(void) } Sleep(3000); - - if (!running) - continue; - - // TODO: Main program - if (i >= 99999) i = 1; - USART_Printf("[CORE] Fetching sensors #%05lu...\r\n", i++); + if (running) { + // TODO: Main program + if (i >= 99999) i = 1; + Info("Fetching sensors #%05lu...", i++); + } } return 0; @@ -33,39 +31,48 @@ int main(void) void Enable(void) { - USART_Printf("[CORE] Parsed 'CMD_RUN' token.\r\n"); + Info("Parsed 'CMD_RUN' token."); if (running == true) { - USART_Printf("[CORE] Error: Already running.\r\n"); + Error("Already running."); + return; } - running = true; + SetStateFlag(running); + Info("Program started."); } void Disable(void) { - USART_Printf("[CORE] Parsed 'CMD_STOP' token.\r\n"); + Info("Parsed 'CMD_STOP' token."); if (running == false) { - USART_Printf("[CORE] Error: Already stopped.\r\n"); + Error("Already stopped."); + return; } running = false; + SetStateFlag(running); + Info("Program stopped."); } void SetTemp(long val) { - USART_Printf("[CORE] Parsed 'CMD_SET_TEMP', VAL='%ld'.\r\n", val); + Info("Parsed 'CMD_SET_TEMP', VAL='%ld'.", val); if (val < 10) { - USART_Printf("[CORE] Error: Given temperature is too low.\r\n"); + Error("Given temperature is too low."); + return; } else if (val > 40) { - USART_Printf("[CORE] Error: Given temperature is too high.\r\n"); + Error("Given temperature is too high."); + return; } } void SetDewp(long val) { - USART_Printf("[CORE] Parsed 'CMD_SET_DEWP', VAL='%ld'.\r\n", val); + Info("Parsed 'CMD_SET_DEWP', VAL='%ld'.", val); if (val < 10) { - USART_Printf("[CORE] Error: Given dew point is too low.\r\n"); + Error("Given dew point is too low."); + return; } else if (val > 80) { - USART_Printf("[CORE] Error: Given dew point is too high.\r\n"); + Error("Given dew point is too high."); + return; } } diff --git a/src/parser.c b/src/parser.c index bbd2ee8..24bcc35 100644 --- a/src/parser.c +++ b/src/parser.c @@ -110,7 +110,7 @@ void CMD_Parse(char ch) // Unexpected character else { - USART_Printf("[CORE] Error: Unexpected character '%c'.\r\n", ch); + Error("Unexpected character '%c'.", ch); state = 0; } break; @@ -163,7 +163,7 @@ void CMD_Parse(char ch) // Unexpected character else { - USART_Printf("[CORE] Error: Unexpected character '%c'.\r\n", ch); + Error("Unexpected character '%c'.", ch); state = 0; } break; diff --git a/src/serial.c b/src/serial.c index bc78f0a..507a7bc 100644 --- a/src/serial.c +++ b/src/serial.c @@ -1,7 +1,6 @@ #include "common.h" #include "serial.h" -#include #include #include #include @@ -12,6 +11,7 @@ #define USART_BAUD_PRESCALE ((((F_CPU / 16) + \ (USART_BAUDRATE / 2)) / (USART_BAUDRATE)) - 1) +// TODO: TX ring buffer static unsigned char rxbuf[USART_BUFSIZE]; static unsigned char rxhead, rxtail; @@ -30,6 +30,11 @@ void USART_Init(void) UBRRL = USART_BAUD_PRESCALE; // Baud rate lower byte } +bool USART_IsDataAvailable(void) +{ + return rxhead != rxtail; +} + char USART_GetChar(void) { unsigned char newtail; @@ -51,24 +56,21 @@ void USART_PutChar(const char ch) UDR = ch; } -void USART_Printf(const char *fmt, ...) +void USART_Print(bool state, const char *pre, const char *fmt, va_list ap) { + int len; char buf[256], *ptr; - va_list args; - va_start(args, fmt); - vsprintf(buf, fmt, args); - va_end(args); + len = sprintf(buf, "[CORE][%c] %s", state ? 'E' : 'D', pre); + vsnprintf(buf + len, sizeof(buf) - len, fmt, ap); ptr = buf; while (*ptr != '\0') { USART_PutChar(*ptr++); } -} -bool USART_IsDataAvailable(void) -{ - return rxhead != rxtail; + USART_PutChar('\r'); + USART_PutChar('\n'); } // Interrupt Handler diff --git a/src/serial.h b/src/serial.h index 44138ed..3a4c2b2 100644 --- a/src/serial.h +++ b/src/serial.h @@ -1,12 +1,13 @@ #ifndef MAD_SERIAL_H #define MAD_SERIAL_H +#include #include void USART_Init(void); char USART_GetChar(void); void USART_PutChar(const char ch); -void USART_Printf(const char *fmt, ...); bool USART_IsDataAvailable(void); +void USART_Print(bool state, const char *pre, const char *fmt, va_list ap); #endif // MAD_SERIAL_H