Skip to content

Commit 8d0e68d

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

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

Diff for: libraries/Preferences/src/Preferences.cpp

+13-2
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;

Diff for: libraries/Preferences/src/Preferences.h

+1-1
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)