overload Preferences.getBytes similar to nvs so you can get size of t… (#2498)

* overload Preferences.getBytes similar to nvs so you can get size of the array.

* Cleaner implentation, with a separate function to get length.  Added an example
This commit is contained in:
lbernstone 2019-04-09 18:45:38 -06:00 committed by Me No Dev
parent 91508030d8
commit da8b7c1b80
3 changed files with 55 additions and 3 deletions

View File

@ -0,0 +1,43 @@
/*
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);
prefs.begin("schedule"); // use "schedule" namespace
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
log_e("Data is not correct size!");
return;
}
schedule_t *schedule = (schedule_t *) buffer; // cast the bytes into a struct ptr
Serial.printf("%02d:%02d %d/%d\n",
schedule[1].hour, schedule[1].minute,
schedule[1].setting1, schedule[1].setting2);
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.printf("%02X ", buffer[x]);
Serial.println();
}
void loop() {}

View File

@ -447,9 +447,9 @@ String Preferences::getString(const char* key, const String defaultValue){
return String(buf);
}
size_t Preferences::getBytes(const char* key, void * buf, size_t maxLen){
size_t Preferences::getBytesLength(const char* key){
size_t len = 0;
if(!_started || !key || !buf || !maxLen){
if(!_started || !key){
return 0;
}
esp_err_t err = nvs_get_blob(_handle, key, NULL, &len);
@ -457,11 +457,19 @@ size_t Preferences::getBytes(const char* key, void * buf, size_t maxLen){
log_e("nvs_get_blob len fail: %s %s", key, nvs_error(err));
return 0;
}
return len;
}
size_t Preferences::getBytes(const char* key, void * buf, size_t maxLen){
size_t len = getBytesLength(key);
if(!len || !buf || !maxLen){
return len;
}
if(len > maxLen){
log_e("not enough space in buffer: %u < %u", maxLen, len);
return 0;
}
err = nvs_get_blob(_handle, key, buf, &len);
esp_err_t err = nvs_get_blob(_handle, key, buf, &len);
if(err){
log_e("nvs_get_blob fail: %s %s", key, nvs_error(err));
return 0;

View File

@ -63,6 +63,7 @@ class Preferences {
bool getBool(const char* key, bool defaultValue = false);
size_t getString(const char* key, char* value, size_t maxLen);
String getString(const char* key, String defaultValue = String());
size_t getBytesLength(const char* key);
size_t getBytes(const char* key, void * buf, size_t maxLen);
size_t freeEntries();
};