Skip to content

Commit 1c193cf

Browse files
committed
Add Preferences library
1 parent 031f36e commit 1c193cf

File tree

6 files changed

+625
-0
lines changed

6 files changed

+625
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
This example shows how to use Preferences (nvs) to store a
3+
structure. Note that the maximum size of a putBytes is 496K
4+
or 97% of the nvs partition size. nvs has signifcant overhead,
5+
so should not be used for data that will change often.
6+
*/
7+
#include <Preferences.h>
8+
Preferences prefs;
9+
10+
typedef struct {
11+
uint8_t hour;
12+
uint8_t minute;
13+
uint8_t setting1;
14+
uint8_t setting2;
15+
} schedule_t;
16+
17+
void setup() {
18+
Serial.begin(115200);
19+
20+
prefs.begin("schedule"); // use "schedule" namespace
21+
uint8_t content[] = {9, 30, 235, 255, 20, 15, 0, 1}; // two entries
22+
prefs.putBytes("schedule", content, sizeof(content));
23+
size_t schLen = prefs.getBytesLength("schedule");
24+
char buffer[schLen]; // prepare a buffer for the data
25+
prefs.getBytes("schedule", buffer, schLen);
26+
if (schLen % sizeof(schedule_t)) { // simple check that data fits
27+
Serial.println("Data is not correct size!");
28+
return;
29+
}
30+
schedule_t *schedule = (schedule_t *) buffer; // cast the bytes into a struct ptr
31+
Serial.print(schedule[1].hour);
32+
Serial.print(":");
33+
Serial.print(schedule[1].minute);
34+
Serial.print(" ");
35+
Serial.print(schedule[1].setting1);
36+
Serial.print("/");
37+
Serial.print(schedule[1].setting2);
38+
39+
schedule[2] = {8, 30, 20, 21}; // add a third entry (unsafely)
40+
41+
// force the struct array into a byte array
42+
prefs.putBytes("schedule", schedule, 3*sizeof(schedule_t));
43+
schLen = prefs.getBytesLength("schedule");
44+
char buffer2[schLen];
45+
prefs.getBytes("schedule", buffer2, schLen);
46+
for (int x=0; x<schLen; x++) Serial.print((uint8_t)buffer[x]);
47+
Serial.println();
48+
prefs.end();
49+
}
50+
51+
void loop() {}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
Microcontroller startup counter example with ESP32 Preferences library.
3+
4+
This simple example demonstrates using the Preferences library to store how many times
5+
the microcontroller has booted. The Preferences library is a wrapper around the Non-volatile
6+
storage on ESP32 processor.
7+
8+
created for arduino-esp32 09 Feb 2017
9+
by Martin Sloup (Arcao)
10+
*/
11+
12+
#include <Preferences.h>
13+
14+
Preferences preferences;
15+
16+
void setup() {
17+
Serial.begin(115200);
18+
Serial.println();
19+
20+
// Open Preferences with my-app namespace. Each application module, library, etc
21+
// has to use a namespace name to prevent key name collisions. We will open storage in
22+
// RW-mode (second parameter has to be false).
23+
// Note: Namespace name is limited to 15 chars.
24+
int ret = preferences.begin("my-app", false);
25+
Serial.println(ret);
26+
27+
// Remove all preferences under the opened namespace
28+
//preferences.clear();
29+
30+
// Or remove the counter key only
31+
//preferences.remove("counter");
32+
33+
// Get the counter value, if the key does not exist, return a default value of 0
34+
// Note: Key name is limited to 15 chars.
35+
unsigned int counter = preferences.getUInt("counter", 0);
36+
37+
// Increase counter by 1
38+
counter++;
39+
40+
// Print the counter to Serial Monitor
41+
Serial.print("Current counter value: ");
42+
Serial.println(counter);
43+
44+
// Store the counter to the Preferences
45+
preferences.putUInt("counter", counter);
46+
47+
// Close the Preferences
48+
preferences.end();
49+
50+
// Wait 10 seconds
51+
Serial.println("Restarting in 10 seconds...");
52+
delay(10000);
53+
54+
// Reset
55+
NVIC_SystemReset();
56+
}
57+
58+
void loop() {}

libraries/Preferences/keywords.txt

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#######################################
2+
# Syntax Coloring Map NVS
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
Preferences KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
begin KEYWORD2
15+
end KEYWORD2
16+
17+
clear KEYWORD2
18+
remove KEYWORD2
19+
20+
putChar KEYWORD2
21+
putUChar KEYWORD2
22+
putShort KEYWORD2
23+
putUShort KEYWORD2
24+
putInt KEYWORD2
25+
putUInt KEYWORD2
26+
putLong KEYWORD2
27+
putULong KEYWORD2
28+
putLong64 KEYWORD2
29+
putULong64 KEYWORD2
30+
putFloat KEYWORD2
31+
putDouble KEYWORD2
32+
putBool KEYWORD2
33+
putString KEYWORD2
34+
putBytes KEYWORD2
35+
36+
getChar KEYWORD2
37+
getUChar KEYWORD2
38+
getShort KEYWORD2
39+
getUShort KEYWORD2
40+
getInt KEYWORD2
41+
getUInt KEYWORD2
42+
getLong KEYWORD2
43+
getULong KEYWORD2
44+
getLong64 KEYWORD2
45+
getULong64 KEYWORD2
46+
getFloat KEYWORD2
47+
getDouble KEYWORD2
48+
getBool KEYWORD2
49+
getString KEYWORD2
50+
getBytes KEYWORD2
51+
52+
#######################################
53+
# Constants (LITERAL1)
54+
#######################################
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=Preferences
2+
version=2.0.0
3+
author=Hristo Gochkov
4+
maintainer=Hristo Gochkov <hristo@espressif.com>
5+
sentence=Provides friendly access to ESP32's Non-Volatile Storage
6+
paragraph=
7+
category=Data Storage
8+
url=
9+
architectures=renesas,renesas_uno

0 commit comments

Comments
 (0)