Rewrite logging system and sync state with clients
This commit is contained in:
30
src/common.c
30
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;
|
||||
}
|
||||
|
||||
@@ -5,10 +5,16 @@
|
||||
#define UNUSED(s) (void)(s)
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <util/delay.h>
|
||||
#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);
|
||||
|
||||
45
src/main.c
45
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
22
src/serial.c
22
src/serial.c
@@ -1,7 +1,6 @@
|
||||
#include "common.h"
|
||||
#include "serial.h"
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
@@ -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
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
#ifndef MAD_SERIAL_H
|
||||
#define MAD_SERIAL_H
|
||||
|
||||
#include <stdarg.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);
|
||||
void USART_Print(bool state, const char *pre, const char *fmt, va_list ap);
|
||||
|
||||
#endif // MAD_SERIAL_H
|
||||
|
||||
Reference in New Issue
Block a user