Skip to content
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

Add WiFi Preferences support #239

Merged
merged 8 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/compile-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ jobs:
- libraries/WiFiS3
- libraries/OTAUpdate
- libraries/OPAMP
- libraries/Preferences
- board:
fqbn: "arduino-git:renesas:minima"
additional-sketch-paths: |
Expand Down
56 changes: 56 additions & 0 deletions libraries/Preferences/examples/Prefs2Struct/Prefs2Struct.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,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() {}
61 changes: 61 additions & 0 deletions libraries/Preferences/examples/StartCounter/StartCounter.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Microcontroller startup counter example with ESP32 Preferences library.

This simple example demonstrates using the Preferences library to store how many times
the microcontroller has booted. The Preferences library is a wrapper around the Non-volatile
storage on ESP32 processor.

created for arduino-esp32 09 Feb 2017
by Martin Sloup (Arcao)
*/

#include <Preferences.h>

Preferences preferences;

void setup() {
Serial.begin(115200);
Serial.println();

// Open Preferences with my-app namespace. Each application module, library, etc
// has to use a namespace name to prevent key name collisions. We will open storage in
// RW-mode (second parameter has to be false).
// Note: Namespace name is limited to 15 chars.
if (!preferences.begin("my-app", false)) {
Serial.println("Cannot initialize preferences");
Serial.println("Make sure your WiFi firmware version is greater than 0.3.0");
while(1) {};
}

// Remove all preferences under the opened namespace
//preferences.clear();

// Or remove the counter key only
//preferences.remove("counter");

// Get the counter value, if the key does not exist, return a default value of 0
// Note: Key name is limited to 15 chars.
unsigned int counter = preferences.getUInt("counter", 0);

// Increase counter by 1
counter++;

// Print the counter to Serial Monitor
Serial.print("Current counter value: ");
Serial.println(counter);

// Store the counter to the Preferences
preferences.putUInt("counter", counter);

// Close the Preferences
preferences.end();

// Wait 10 seconds
Serial.println("Restarting in 10 seconds...");
delay(10000);

// Reset
NVIC_SystemReset();
}

void loop() {}
54 changes: 54 additions & 0 deletions libraries/Preferences/keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#######################################
# Syntax Coloring Map NVS
#######################################

#######################################
# Datatypes (KEYWORD1)
#######################################

Preferences KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
#######################################
begin KEYWORD2
end KEYWORD2

clear KEYWORD2
remove KEYWORD2

putChar KEYWORD2
putUChar KEYWORD2
putShort KEYWORD2
putUShort KEYWORD2
putInt KEYWORD2
putUInt KEYWORD2
putLong KEYWORD2
putULong KEYWORD2
putLong64 KEYWORD2
putULong64 KEYWORD2
putFloat KEYWORD2
putDouble KEYWORD2
putBool KEYWORD2
putString KEYWORD2
putBytes KEYWORD2

getChar KEYWORD2
getUChar KEYWORD2
getShort KEYWORD2
getUShort KEYWORD2
getInt KEYWORD2
getUInt KEYWORD2
getLong KEYWORD2
getULong KEYWORD2
getLong64 KEYWORD2
getULong64 KEYWORD2
getFloat KEYWORD2
getDouble KEYWORD2
getBool KEYWORD2
getString KEYWORD2
getBytes KEYWORD2

#######################################
# Constants (LITERAL1)
#######################################
9 changes: 9 additions & 0 deletions libraries/Preferences/library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=Preferences
version=2.0.0
author=Hristo Gochkov
maintainer=Hristo Gochkov <[email protected]>
sentence=Provides friendly access to ESP32's Non-Volatile Storage
paragraph=
category=Data Storage
url=
architectures=renesas,renesas_uno
Loading
Loading