-
Notifications
You must be signed in to change notification settings - Fork 7.6k
overload Preferences.getBytes similar to nvs so you can get size of t… #2498
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
libraries/Preferences/examples/Prefs2Struct/Prefs2Struct.ino
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
This example shows how to use Preferences (nvs) to store a | ||
structure. Note that the maximum size of a putBytes is 496K | ||
or 97% of the nvs partition size. nvs has signifcant overhead, | ||
so should not be used for data that will change often. | ||
*/ | ||
#include <Preferences.h> | ||
Preferences prefs; | ||
|
||
typedef struct { | ||
uint8_t hour; | ||
uint8_t minute; | ||
uint8_t setting1; | ||
uint8_t setting2; | ||
} schedule_t; | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
prefs.begin("schedule"); // use "schedule" namespace | ||
uint8_t content[] = {9, 30, 235, 255, 20, 15, 0, 1}; // two entries | ||
prefs.putBytes("schedule", content, sizeof(content)); | ||
size_t schLen = prefs.getBytesLength("schedule"); | ||
char buffer[schLen]; // prepare a buffer for the data | ||
prefs.getBytes("schedule", buffer, schLen); | ||
if (schLen % sizeof(schedule_t)) { // simple check that data fits | ||
log_e("Data is not correct size!"); | ||
return; | ||
} | ||
schedule_t *schedule = (schedule_t *) buffer; // cast the bytes into a struct ptr | ||
Serial.printf("%02d:%02d %d/%d\n", | ||
schedule[1].hour, schedule[1].minute, | ||
schedule[1].setting1, schedule[1].setting2); | ||
schedule[2] = {8, 30, 20, 21}; // add a third entry (unsafely) | ||
// force the struct array into a byte array | ||
prefs.putBytes("schedule", schedule, 3*sizeof(schedule_t)); | ||
schLen = prefs.getBytesLength("schedule"); | ||
char buffer2[schLen]; | ||
prefs.getBytes("schedule", buffer2, schLen); | ||
for (int x=0; x<schLen; x++) Serial.printf("%02X ", buffer[x]); | ||
Serial.println(); | ||
} | ||
|
||
void loop() {} |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.