Skip to content

Commit 1a63e16

Browse files
authored
Merge pull request #239 from pennam/preferences
Add WiFi Preferences support
2 parents fd3d4a4 + 1f47145 commit 1a63e16

File tree

7 files changed

+612
-0
lines changed

7 files changed

+612
-0
lines changed

Diff for: .github/workflows/compile-examples.yml

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ jobs:
9595
- libraries/WiFiS3
9696
- libraries/OTAUpdate
9797
- libraries/OPAMP
98+
- libraries/Preferences
9899
- board:
99100
fqbn: "arduino-git:renesas:minima"
100101
additional-sketch-paths: |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
if (!prefs.begin("schedule")) { // use "schedule" namespace
21+
Serial.println("Cannot initialize preferences");
22+
Serial.println("Make sure your WiFi firmware version is greater than 0.3.0");
23+
while(1) {};
24+
}
25+
uint8_t content[] = {9, 30, 235, 255, 20, 15, 0, 1}; // two entries
26+
prefs.putBytes("schedule", content, sizeof(content));
27+
size_t schLen = prefs.getBytesLength("schedule");
28+
char buffer[schLen]; // prepare a buffer for the data
29+
prefs.getBytes("schedule", buffer, schLen);
30+
if (schLen % sizeof(schedule_t)) { // simple check that data fits
31+
Serial.println("Data is not correct size!");
32+
return;
33+
}
34+
schedule_t *schedule = (schedule_t *) buffer; // cast the bytes into a struct ptr
35+
Serial.print(schedule[1].hour);
36+
Serial.print(":");
37+
Serial.print(schedule[1].minute);
38+
Serial.print(" ");
39+
Serial.print(schedule[1].setting1);
40+
Serial.print("/");
41+
Serial.print(schedule[1].setting2);
42+
Serial.println();
43+
44+
schedule[2] = {8, 30, 20, 21}; // add a third entry (unsafely)
45+
46+
// force the struct array into a byte array
47+
prefs.putBytes("schedule", schedule, 3*sizeof(schedule_t));
48+
schLen = prefs.getBytesLength("schedule");
49+
char buffer2[schLen];
50+
prefs.getBytes("schedule", buffer2, schLen);
51+
for (int x=0; x<schLen; x++) Serial.print((uint8_t)buffer[x]);
52+
Serial.println();
53+
prefs.end();
54+
}
55+
56+
void loop() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
if (!preferences.begin("my-app", false)) {
25+
Serial.println("Cannot initialize preferences");
26+
Serial.println("Make sure your WiFi firmware version is greater than 0.3.0");
27+
while(1) {};
28+
}
29+
30+
// Remove all preferences under the opened namespace
31+
//preferences.clear();
32+
33+
// Or remove the counter key only
34+
//preferences.remove("counter");
35+
36+
// Get the counter value, if the key does not exist, return a default value of 0
37+
// Note: Key name is limited to 15 chars.
38+
unsigned int counter = preferences.getUInt("counter", 0);
39+
40+
// Increase counter by 1
41+
counter++;
42+
43+
// Print the counter to Serial Monitor
44+
Serial.print("Current counter value: ");
45+
Serial.println(counter);
46+
47+
// Store the counter to the Preferences
48+
preferences.putUInt("counter", counter);
49+
50+
// Close the Preferences
51+
preferences.end();
52+
53+
// Wait 10 seconds
54+
Serial.println("Restarting in 10 seconds...");
55+
delay(10000);
56+
57+
// Reset
58+
NVIC_SystemReset();
59+
}
60+
61+
void loop() {}

Diff for: libraries/Preferences/keywords.txt

+54
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+
#######################################

Diff for: libraries/Preferences/library.properties

+9
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 <[email protected]>
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)