Last active
March 9, 2022 18:55
-
-
Save luigibrancati/931aac73f6712437620226a37a1554d3 to your computer and use it in GitHub Desktop.
Full code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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