Skip to content

Conversation

bot1131357
Copy link
Contributor

@bot1131357 bot1131357 commented Jan 14, 2021

Preferences library currently works only with the default NVS partition named "nvs". I added an function overload for Preferences::begin to enable use of a separate NVS partition.

For example, if my NVS partition is named "app_data" and namespace "app", I can do the following:

Preferences prefs;
prefs.begin("app_data", "app", false); 

// example use
uint8_t tmp = prefs.getUChar("val1", 0xff);
prefs.putUChar("val1", 0x00);

First time contributing - let me know if I'm doing it wrong. Thanks.

@me-no-dev
Copy link
Member

This could be done without the overload :)
How about you modify the existing function like this:

Preferences.h

bool begin(const char* name, bool readOnly = false, const char* partition_label=NULL);

Preferences.cpp

bool Preferences::begin(const char * name, bool readOnly, const char* partition_label){
    if(_started){
        return false;
    }
    _readOnly = readOnly;
    esp_err_t err = ESP_OK;
    if (partition_label != NULL) {
        err = nvs_flash_init_partition(partition_label);
        if (err) {
            log_e("nvs_flash_init_partition failed: %s", nvs_error(err));
            return false;
        }
        err = nvs_open_from_partition(partition_label, name, readOnly ? NVS_READONLY : NVS_READWRITE, &_handle);
    } else {
        err = nvs_open(name, readOnly?NVS_READONLY:NVS_READWRITE, &_handle);
    }
    if(err){
        log_e("nvs_open failed: %s", nvs_error(err));
        return false;
    }
    _started = true;
    return true;
}

@bot1131357
Copy link
Contributor Author

Thanks for getting back to me!

I did consider doing this, but I wanted the arguments consistent with esp-idf:

esp_err_tnvs_open_from_partition(const char *part_name, const char *name, nvs_open_mode_topen_mode, nvs_handle_t *out_handle)

The parameter bool readOnly placed in between namespace and partition labels feels clunky to me.

Thoughts?

@me-no-dev
Copy link
Member

This is Arduino and not IDF :) Partition name will be rarely used anyway

@bot1131357
Copy link
Contributor Author

bot1131357 commented Jan 15, 2021

I have pushed the changes as per your suggestion. :)

Referring to my original example, now the partition selection becomes an optional argument to the existing Preference::begin function:

Preferences prefs;
prefs.begin("app", false, "app_data");  // additional parameter to specify non-default partition

@me-no-dev me-no-dev merged commit 8d0e68d into espressif:master Jan 15, 2021
@me-no-dev
Copy link
Member

thanks :)

@bot1131357
Copy link
Contributor Author

Thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants