Skip to content

Instantly share code, notes, and snippets.

@luigibrancati
Last active March 9, 2022 18:55
Show Gist options
  • Select an option

  • Save luigibrancati/931aac73f6712437620226a37a1554d3 to your computer and use it in GitHub Desktop.

Select an option

Save luigibrancati/931aac73f6712437620226a37a1554d3 to your computer and use it in GitHub Desktop.
Full code
#ifndef _MEMORY_UTILS_H
#define _MEMORY_UTILS_H
#include <Preferences.h>
Preferences preferences;
const char* prefNamespace = "message";
const char* varName = "messagevar";
String readMessage(const String def){
preferences.begin(prefNamespace);
if(preferences.isKey(varName)){
Serial.println(String(varName)+" key found");
return preferences.getString(varName);
}
else{
Serial.println(String(varName)+" key not found, returning default value "+def);
return def;
}
preferences.end();
}
bool updateMessage(const String newValue){
preferences.begin(prefNamespace);
String oldValue = readMessage("");
bool outcome = false;
if(newValue != oldValue){
Serial.println("Updating last read message!");
preferences.putString(varName, newValue);
outcome = true;
}
else{
Serial.println("Last read message is the same, not updating it in memory");
}
preferences.end();
return outcome;
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment