121 lines
3.4 KiB
Arduino
121 lines
3.4 KiB
Arduino
|
|
||
|
void saveGlobalEnergyToEEPROM() {
|
||
|
|
||
|
#ifndef DEBUG_NO_SERIAL_MSG
|
||
|
Serial.println(cs_savingGlobalEnergyToEEPROM);
|
||
|
#endif
|
||
|
|
||
|
EnergyData data;
|
||
|
data.energy = globalEnergy;
|
||
|
// Berechnung der Prüfsumme nur über data.energy
|
||
|
data.checksum = calculateChecksum(data.energy);
|
||
|
|
||
|
EEPROM.put(EEPROM_ADDR_ENERGY, data);
|
||
|
EEPROM.commit(); // Dauerhaftes Speichern der Änderungen
|
||
|
delay(100);
|
||
|
#ifndef DEBUG_NO_SERIAL_MSG
|
||
|
Serial.print(cs_eepromWriteComplete);
|
||
|
Serial.println(data.energy);
|
||
|
#endif
|
||
|
|
||
|
//readGlobalEnergyFromEEPROM();
|
||
|
}
|
||
|
|
||
|
// Funktion zum Lesen der Energiemenge aus dem EEPROM
|
||
|
float readGlobalEnergyFromEEPROM() {
|
||
|
EnergyData data;
|
||
|
EEPROM.get(EEPROM_ADDR_ENERGY, data);
|
||
|
|
||
|
/*
|
||
|
Serial.print(cs_readChecksum);
|
||
|
Serial.println(data.checksum);
|
||
|
Serial.print(cs_calculatedChecksum);
|
||
|
Serial.println(calculateChecksum(data.energy));
|
||
|
*/
|
||
|
|
||
|
// Überprüfen der Prüfsumme nur über data.energy
|
||
|
if (data.checksum == calculateChecksum(data.energy)) {
|
||
|
// Prüfsumme ist korrekt, geben Sie die Energiemenge zurück
|
||
|
Serial.print(cs_eepromReadComplete);
|
||
|
Serial.print(data.energy);
|
||
|
Serial.println();
|
||
|
|
||
|
return data.energy;
|
||
|
|
||
|
} else {
|
||
|
// Prüfsumme stimmt nicht überein, möglicherweise ungültige Daten
|
||
|
// Hier können Sie eine geeignete Behandlung implementieren, z. B. Standardwert zurückgeben
|
||
|
Serial.println(cs_eepromValueDamaged);
|
||
|
return 0.0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Funktion zum Berechnen der Prüfsumme über data.energy
|
||
|
uint16_t calculateChecksum(float energy) {
|
||
|
uint16_t checksum = 0;
|
||
|
uint8_t *dataPtr = (uint8_t *)&energy;
|
||
|
|
||
|
for (int i = 0; i < sizeof(float); i++) {
|
||
|
checksum ^= dataPtr[i];
|
||
|
}
|
||
|
|
||
|
return checksum;
|
||
|
}
|
||
|
|
||
|
void writeConfigToEEPROM() {
|
||
|
globalConfigData.checksum = calculateChecksumConfig();
|
||
|
|
||
|
printConfigData(globalConfigData);
|
||
|
|
||
|
EEPROM.put(EEPROM_ADDR_CFG_START, globalConfigData);
|
||
|
EEPROM.commit();
|
||
|
delay(100);
|
||
|
}
|
||
|
|
||
|
void readConfigFromEEPROM() {
|
||
|
EEPROM.get(EEPROM_ADDR_CFG_START, globalConfigData);
|
||
|
|
||
|
Serial.print(cs_eepromConfigAfterRead);
|
||
|
printConfigData(globalConfigData);
|
||
|
uint16_t checksumEEPROM = globalConfigData.checksum;
|
||
|
|
||
|
// Überprüfe die Checksumme
|
||
|
uint16_t checksum = calculateChecksumConfig();
|
||
|
|
||
|
if (checksum != checksumEEPROM) {
|
||
|
Serial.println(cs_eepromSetConfigDefault);
|
||
|
// Setze auf Standardwerte
|
||
|
globalConfigData.temp_min = DEFAULT_TEMP_MIN;
|
||
|
globalConfigData.temp_max = DEFAULT_TEMP_MAX;
|
||
|
globalConfigData.humi_min = DEFAULT_HUMI_MIN;
|
||
|
globalConfigData.humi_max = DEFAULT_HUMI_MAX;
|
||
|
globalConfigData.current_min = DEFAULT_CURRENT_MIN;
|
||
|
globalConfigData.current_max = DEFAULT_CURRENT_MAX;
|
||
|
globalConfigData.shunt_voltage_drop = DEFAULT_SHUNT_VOLTAGE_DROP;
|
||
|
globalConfigData.shunt_max_current = DEFAULT_SHUNT_CURRENT_MAX;
|
||
|
globalConfigData.max_capacity = DEFAULT_MAX_CAPACITY;
|
||
|
globalConfigData.time_ina226_refresh = DEFAULT_INA226READ_DELAY_S;
|
||
|
globalConfigData.current_fact = 100.0; // offset 100
|
||
|
globalConfigData.checksum = 0;
|
||
|
|
||
|
writeConfigToEEPROM();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
uint16_t calculateChecksumConfig()
|
||
|
{
|
||
|
// Berechne die Checksumme
|
||
|
uint16_t checksum = 0;
|
||
|
byte* dataPointer = (byte*)&globalConfigData;
|
||
|
globalConfigData.checksum = 0;
|
||
|
|
||
|
for (size_t i = 0; i < sizeof(globalConfigData); ++i) {
|
||
|
checksum += dataPointer[i];
|
||
|
}
|
||
|
|
||
|
Serial.print(cs_newChecksum);
|
||
|
Serial.println(checksum);
|
||
|
|
||
|
return checksum;
|
||
|
}
|