Merge branch 'main' of tesserakt.pro:alex/arduino-filament-dryer
This commit is contained in:
50
include/settings_store.h
Normal file
50
include/settings_store.h
Normal file
@@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <EEPROM.h>
|
||||
|
||||
#include "config.h"
|
||||
|
||||
// After TuningData (15 bytes) + checksum (1 byte) at address 0
|
||||
static const uint16_t SETTINGS_MAGIC = 0xDA7E;
|
||||
static const int SETTINGS_EEPROM_ADDR = 16;
|
||||
|
||||
struct SettingsData {
|
||||
uint16_t magic = 0;
|
||||
float targetC = TARGET_TEMP_C;
|
||||
};
|
||||
|
||||
inline uint8_t settingsChecksum(const SettingsData &data) {
|
||||
const uint8_t *bytes = reinterpret_cast<const uint8_t *>(&data);
|
||||
uint8_t sum = 0;
|
||||
for (uint8_t i = 0; i < sizeof(SettingsData) - 1; ++i) {
|
||||
sum ^= bytes[i];
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
inline bool settingsLoad(SettingsData &out) {
|
||||
EEPROM.get(SETTINGS_EEPROM_ADDR, out);
|
||||
const uint8_t stored = EEPROM.read(SETTINGS_EEPROM_ADDR + sizeof(SettingsData));
|
||||
if (out.magic != SETTINGS_MAGIC) {
|
||||
return false;
|
||||
}
|
||||
return settingsChecksum(out) == stored;
|
||||
}
|
||||
|
||||
inline void settingsSave(const SettingsData &data) {
|
||||
EEPROM.put(SETTINGS_EEPROM_ADDR, data);
|
||||
EEPROM.write(SETTINGS_EEPROM_ADDR + sizeof(SettingsData), settingsChecksum(data));
|
||||
}
|
||||
|
||||
inline void settingsSaveTarget(float targetC) {
|
||||
SettingsData data;
|
||||
data.magic = SETTINGS_MAGIC;
|
||||
data.targetC = targetC;
|
||||
settingsSave(data);
|
||||
}
|
||||
|
||||
inline void settingsClear() {
|
||||
SettingsData cleared;
|
||||
settingsSave(cleared);
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "config.h"
|
||||
#include "pid_autotuner.h"
|
||||
#include "pid_controller.h"
|
||||
#include "settings_store.h"
|
||||
#include "tuning_store.h"
|
||||
|
||||
class ThermalController {
|
||||
@@ -54,6 +55,12 @@ public:
|
||||
Serial.println(F("Loaded learned PID from EEPROM"));
|
||||
printTuning();
|
||||
}
|
||||
|
||||
SettingsData settings;
|
||||
if (settingsLoad(settings) && settings.targetC >= TARGET_MIN_C &&
|
||||
settings.targetC <= TARGET_MAX_C) {
|
||||
setTarget(settings.targetC, false);
|
||||
}
|
||||
}
|
||||
|
||||
void applyTuning(const TuningData &data) {
|
||||
@@ -132,7 +139,7 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
void setTarget(float targetC) {
|
||||
void setTarget(float targetC, bool persist = true) {
|
||||
targetTempC_ = targetC;
|
||||
pid_.setSetpoint(targetC);
|
||||
pid_.reset();
|
||||
@@ -144,6 +151,9 @@ public:
|
||||
fanIdleOverride_ = false;
|
||||
}
|
||||
applyFan();
|
||||
if (persist && targetC >= TARGET_MIN_C && targetC <= TARGET_MAX_C) {
|
||||
settingsSaveTarget(targetC);
|
||||
}
|
||||
}
|
||||
|
||||
void noteSensorMax(float maxTempC) {
|
||||
|
||||
Reference in New Issue
Block a user