Rewrite logging system and sync state with clients

This commit is contained in:
2024-08-28 02:32:34 +02:00
parent 481170792a
commit 9a3ae27a5f
6 changed files with 77 additions and 33 deletions

View File

@@ -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;
}

View File

@@ -5,10 +5,16 @@
#define UNUSED(s) (void)(s) #define UNUSED(s) (void)(s)
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h> #include <stdbool.h>
#include <util/delay.h> #include <util/delay.h>
#define Sleep(ms) _delay_ms(ms) #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 Enable(void);
void Disable(void); void Disable(void);
void SetTemp(long val); void SetTemp(long val);

View File

@@ -9,8 +9,8 @@ int main(void)
char ch; char ch;
unsigned long i = 1; unsigned long i = 1;
running = true;
USART_Init(); USART_Init();
Enable();
for (;;) { for (;;) {
// Process rx ring buffer // Process rx ring buffer
@@ -19,13 +19,11 @@ int main(void)
} }
Sleep(3000); Sleep(3000);
if (running) {
if (!running)
continue;
// TODO: Main program // TODO: Main program
if (i >= 99999) i = 1; if (i >= 99999) i = 1;
USART_Printf("[CORE] Fetching sensors #%05lu...\r\n", i++); Info("Fetching sensors #%05lu...", i++);
}
} }
return 0; return 0;
@@ -33,39 +31,48 @@ int main(void)
void Enable(void) void Enable(void)
{ {
USART_Printf("[CORE] Parsed 'CMD_RUN' token.\r\n"); Info("Parsed 'CMD_RUN' token.");
if (running == true) { if (running == true) {
USART_Printf("[CORE] Error: Already running.\r\n"); Error("Already running.");
return;
} }
running = true; running = true;
SetStateFlag(running);
Info("Program started.");
} }
void Disable(void) void Disable(void)
{ {
USART_Printf("[CORE] Parsed 'CMD_STOP' token.\r\n"); Info("Parsed 'CMD_STOP' token.");
if (running == false) { if (running == false) {
USART_Printf("[CORE] Error: Already stopped.\r\n"); Error("Already stopped.");
return;
} }
running = false; running = false;
SetStateFlag(running);
Info("Program stopped.");
} }
void SetTemp(long val) 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) { 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) { } 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) 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) { 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) { } else if (val > 80) {
USART_Printf("[CORE] Error: Given dew point is too high.\r\n"); Error("Given dew point is too high.");
return;
} }
} }

View File

@@ -110,7 +110,7 @@ void CMD_Parse(char ch)
// Unexpected character // Unexpected character
else { else {
USART_Printf("[CORE] Error: Unexpected character '%c'.\r\n", ch); Error("Unexpected character '%c'.", ch);
state = 0; state = 0;
} }
break; break;
@@ -163,7 +163,7 @@ void CMD_Parse(char ch)
// Unexpected character // Unexpected character
else { else {
USART_Printf("[CORE] Error: Unexpected character '%c'.\r\n", ch); Error("Unexpected character '%c'.", ch);
state = 0; state = 0;
} }
break; break;

View File

@@ -1,7 +1,6 @@
#include "common.h" #include "common.h"
#include "serial.h" #include "serial.h"
#include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <avr/io.h> #include <avr/io.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
@@ -12,6 +11,7 @@
#define USART_BAUD_PRESCALE ((((F_CPU / 16) + \ #define USART_BAUD_PRESCALE ((((F_CPU / 16) + \
(USART_BAUDRATE / 2)) / (USART_BAUDRATE)) - 1) (USART_BAUDRATE / 2)) / (USART_BAUDRATE)) - 1)
// TODO: TX ring buffer
static unsigned char rxbuf[USART_BUFSIZE]; static unsigned char rxbuf[USART_BUFSIZE];
static unsigned char rxhead, rxtail; static unsigned char rxhead, rxtail;
@@ -30,6 +30,11 @@ void USART_Init(void)
UBRRL = USART_BAUD_PRESCALE; // Baud rate lower byte UBRRL = USART_BAUD_PRESCALE; // Baud rate lower byte
} }
bool USART_IsDataAvailable(void)
{
return rxhead != rxtail;
}
char USART_GetChar(void) char USART_GetChar(void)
{ {
unsigned char newtail; unsigned char newtail;
@@ -51,24 +56,21 @@ void USART_PutChar(const char ch)
UDR = 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; char buf[256], *ptr;
va_list args;
va_start(args, fmt); len = sprintf(buf, "[CORE][%c] %s", state ? 'E' : 'D', pre);
vsprintf(buf, fmt, args); vsnprintf(buf + len, sizeof(buf) - len, fmt, ap);
va_end(args);
ptr = buf; ptr = buf;
while (*ptr != '\0') { while (*ptr != '\0') {
USART_PutChar(*ptr++); USART_PutChar(*ptr++);
} }
}
bool USART_IsDataAvailable(void) USART_PutChar('\r');
{ USART_PutChar('\n');
return rxhead != rxtail;
} }
// Interrupt Handler // Interrupt Handler

View File

@@ -1,12 +1,13 @@
#ifndef MAD_SERIAL_H #ifndef MAD_SERIAL_H
#define MAD_SERIAL_H #define MAD_SERIAL_H
#include <stdarg.h>
#include <stdbool.h> #include <stdbool.h>
void USART_Init(void); void USART_Init(void);
char USART_GetChar(void); char USART_GetChar(void);
void USART_PutChar(const char ch); void USART_PutChar(const char ch);
void USART_Printf(const char *fmt, ...);
bool USART_IsDataAvailable(void); bool USART_IsDataAvailable(void);
void USART_Print(bool state, const char *pre, const char *fmt, va_list ap);
#endif // MAD_SERIAL_H #endif // MAD_SERIAL_H