Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on May 12, 2022. It is now read-only.

Commit 8d0e68d

Browse filesBrowse files
authored
Added parameter to Preferences::begin to select non-default NVS partition (espressif#4718)
1 parent d253085 commit 8d0e68d
Copy full SHA for 8d0e68d

File tree

2 files changed

+14
-3
lines changed
Filter options

2 files changed

+14
-3
lines changed

‎libraries/Preferences/src/Preferences.cpp

Copy file name to clipboardExpand all lines: libraries/Preferences/src/Preferences.cpp
+13-2Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "Preferences.h"
1515

1616
#include "nvs.h"
17+
#include "nvs_flash.h"
1718

1819
const char * nvs_errors[] = { "OTHER", "NOT_INITIALIZED", "NOT_FOUND", "TYPE_MISMATCH", "READ_ONLY", "NOT_ENOUGH_SPACE", "INVALID_NAME", "INVALID_HANDLE", "REMOVE_FAILED", "KEY_TOO_LONG", "PAGE_FULL", "INVALID_STATE", "INVALID_LENGTH"};
1920
#define nvs_error(e) (((e)>ESP_ERR_NVS_BASE)?nvs_errors[(e)&~(ESP_ERR_NVS_BASE)]:nvs_errors[0])
@@ -28,12 +29,22 @@ Preferences::~Preferences(){
2829
end();
2930
}
3031

31-
bool Preferences::begin(const char * name, bool readOnly){
32+
bool Preferences::begin(const char * name, bool readOnly, const char* partition_label){
3233
if(_started){
3334
return false;
3435
}
3536
_readOnly = readOnly;
36-
esp_err_t err = nvs_open(name, readOnly?NVS_READONLY:NVS_READWRITE, &_handle);
37+
esp_err_t err = ESP_OK;
38+
if (partition_label != NULL) {
39+
err = nvs_flash_init_partition(partition_label);
40+
if (err) {
41+
log_e("nvs_flash_init_partition failed: %s", nvs_error(err));
42+
return false;
43+
}
44+
err = nvs_open_from_partition(partition_label, name, readOnly ? NVS_READONLY : NVS_READWRITE, &_handle);
45+
} else {
46+
err = nvs_open(name, readOnly?NVS_READONLY:NVS_READWRITE, &_handle);
47+
}
3748
if(err){
3849
log_e("nvs_open failed: %s", nvs_error(err));
3950
return false;

‎libraries/Preferences/src/Preferences.h

Copy file name to clipboardExpand all lines: libraries/Preferences/src/Preferences.h
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Preferences {
2929
Preferences();
3030
~Preferences();
3131

32-
bool begin(const char * name, bool readOnly=false);
32+
bool begin(const char * name, bool readOnly=false, const char* partition_label=NULL);
3333
void end();
3434

3535
bool clear();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.