forked from arduino/ArduinoCore-renesas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrefs2Struct.ino
56 lines (50 loc) · 1.75 KB
/
Prefs2Struct.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
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);
if (!prefs.begin("schedule")) { // use "schedule" namespace
Serial.println("Cannot initialize preferences");
Serial.println("Make sure your WiFi firmware version is greater than 0.3.0");
while(1) {};
}
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
Serial.println("Data is not correct size!");
return;
}
schedule_t *schedule = (schedule_t *) buffer; // cast the bytes into a struct ptr
Serial.print(schedule[1].hour);
Serial.print(":");
Serial.print(schedule[1].minute);
Serial.print(" ");
Serial.print(schedule[1].setting1);
Serial.print("/");
Serial.print(schedule[1].setting2);
Serial.println();
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.print((uint8_t)buffer[x]);
Serial.println();
prefs.end();
}
void loop() {}