Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

write eeprom or name if they are available even config is in flash #333

Merged
merged 7 commits into from
Sep 30, 2024
31 changes: 21 additions & 10 deletions src/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,13 @@ uint16_t configLengthEEPROM = 0;
boolean configActivated = false;
uint16_t pNameBuffer = 0; // pointer for nameBuffer during reading of config
const uint16_t configLengthFlash = sizeof(CustomDeviceConfig);
// if config is in EEPROM, ensure upload a new config even if a config in flash is available

void resetConfig();
void readConfig();
void _activateConfig();
void readConfigFromMemory(bool configFromFlash);

bool configStoredInFlash()
{
return configLengthFlash > 0;
Expand Down Expand Up @@ -138,16 +140,24 @@ void OnSetConfig()
char *cfg = cmdMessenger.readStringArg();
uint8_t cfgLen = strlen(cfg);

bool maxConfigLengthNotExceeded = configLengthEEPROM + cfgLen + 1 < MEM_LEN_CONFIG;
if (maxConfigLengthNotExceeded) {
MFeeprom.write_block(MEM_OFFSET_CONFIG + configLengthEEPROM, cfg, cfgLen + 1); // save the received config string including the terminatung NULL (+1) to EEPROM
configLengthEEPROM += cfgLen;
cmdMessenger.sendCmd(kStatus, configLengthEEPROM);
} else
cmdMessenger.sendCmd(kStatus, -1); // last successfull saving block is already NULL terminated, nothing more todo
if (configStoredInEEPROM() || !configStoredInFlash()) {
bool maxConfigLengthNotExceeded = configLengthEEPROM + cfgLen + 1 < MEM_LEN_CONFIG;
if (maxConfigLengthNotExceeded) {
// save the received config string including the terminatung NULL (+1) to EEPROM
MFeeprom.write_block(MEM_OFFSET_CONFIG + configLengthEEPROM, cfg, cfgLen + 1);
configLengthEEPROM += cfgLen;
cmdMessenger.sendCmd(kStatus, configLengthEEPROM);
} else
// staus message to connector, failure on setting config
elral marked this conversation as resolved.
Show resolved Hide resolved
// connector does not check for status = -1
cmdMessenger.sendCmd(kStatus, -1);
DocMoebiuz marked this conversation as resolved.
Show resolved Hide resolved
#ifdef DEBUG2CMDMESSENGER
cmdMessenger.sendCmd(kDebug, F("Setting config end"));
cmdMessenger.sendCmd(kDebug, F("Setting config end"));
#endif
} else {
// connector does not check for status = -1
cmdMessenger.sendCmd(kStatus, -1);
}
}

void resetConfig()
Expand Down Expand Up @@ -718,9 +728,10 @@ void OnGenNewSerial()
// ************************************************************
void storeName()
{
if (!configStoredInFlash()) {
if (configStoredInEEPROM() || !configStoredInFlash()) {
elral marked this conversation as resolved.
Show resolved Hide resolved
MFeeprom.write_byte(MEM_OFFSET_NAME, '#');
MFeeprom.write_block(MEM_OFFSET_NAME + 1, name, MEM_LEN_NAME - 1);
// MFeeprom.commit() is not required, name is always set before config
DocMoebiuz marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -735,7 +746,7 @@ void restoreName()
void OnSetName()
{
char *cfg = cmdMessenger.readStringArg();
if (!configStoredInFlash()) {
if (configStoredInEEPROM() || !configStoredInFlash()) {
elral marked this conversation as resolved.
Show resolved Hide resolved
memcpy(name, cfg, MEM_LEN_NAME);
storeName();
}
Expand Down
Loading