Prevent redundant writes and start implementing parser timeout

This commit is contained in:
2024-09-22 23:26:48 +02:00
parent a4ed6be3de
commit 9458db03f7
3 changed files with 88 additions and 29 deletions

View File

@@ -6,8 +6,8 @@
#include <avr/interrupt.h>
// TODO: Implement command parser timeout for large input.
// TODO: Config header for chip specifics like EEPROM size.
// TODO: Only update EEPROM if value differs from previous.
// TODO: Check thermistor conversion results /w thermometer.
// TODO: Implement primary state machine for update loop.
// TODO: Migrate to ATMega 1284P-PU for 2nd 16-bit timer.
@@ -31,7 +31,8 @@ static float relh;
static int Init(void);
static void Update(void);
static void UpdateSensors(void);
static void SetTarget(float t, float td);
static void GetSensorState(void);
static int Init(void)
{
@@ -85,7 +86,7 @@ static int Init(void)
if (MEM_Read(&mem) == 0) {
Info("Found persistent configuration in EEPROM!");
Info("Setting targets TEMP='%.2fC', DEWP='%.2fC'.",
Info("Using targets TEMP='%.2fC', DEWP='%.2fC'.",
mem.temp, mem.dewp);
temp_target = mem.temp;
@@ -139,7 +140,6 @@ static void Update(void)
{
char ch;
cmd_t cmd;
mem_block_t mem;
// Parse serial commands
while ((ch = USART_Getc()) >= 0) {
@@ -148,18 +148,14 @@ static void Update(void)
continue;
}
switch(cmd.type) {
case T_SET: // Configure new persistent target
Info("Setting new target TEMP='%.2fC'...", cmd.args[0]);
Info("Setting new target DEWP='%.2fC'...", cmd.args[1]);
mem.temp = temp_target = cmd.args[0];
mem.dewp = dewp_target = cmd.args[1];
MEM_Write(&mem);
case T_SET: // Set new persistent target
SetTarget(cmd.args[0], cmd.args[1]);
return;
}
}
// Poll sensor values
UpdateSensors();
GetSensorState();
// Handle state
switch (state) {
@@ -174,7 +170,33 @@ static void Update(void)
}
}
static void UpdateSensors(void)
static void SetTarget(float t, float td)
{
mem_block_t mem;
Info("=======================================");
Info("Setting temperature target to '%.2fC'.", t);
Info("Setting dewpoint target to '%.2fC'.", td);
Info("=======================================");
if (MEM_Read(&mem) == 0) {
if (t == mem.temp && td == mem.dewp) {
return; // Nothing to do
}
}
mem.temp = t;
mem.dewp = td;
// Keep in EEPROM
MEM_Write(&mem);
// Update state
temp_target = t;
dewp_target = td;
}
static void GetSensorState(void)
{
word raw;
float t[6], rh[3], dp[3];