-
Notifications
You must be signed in to change notification settings - Fork 0
/
nvs_storage.h
115 lines (109 loc) · 3.72 KB
/
nvs_storage.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* \file nvs_storage.h
*
* \brief function to store and read nvs flash variables
*
* This is the files that contains the functions to save and read data from the nvs flash.
*
* \date 19.08.2019
*
*/
#include <Preferences.h>
/// Initialize a Preferences Object to store the data into ESP32 flash
Preferences preferences;
/**************************************************************************/
/*!
@brief store given value of a key in a namespace
@param used_namespace the intended namespace to store the values
@param key_nvs the key to be stored at
@param value the String key to be stored
@returns void
*/
/**************************************************************************/
void set_value(const char used_namespace[14], const char key_nvs[14], String value) {
// Open Preferences with my-app namespace.
// RW-mode if second parameter is false
Serial.print("[NVS] Saving ");
Serial.print(key_nvs);
Serial.print(" to ");
Serial.print(used_namespace);
Serial.print(": ");
Serial.print(value);
preferences.begin(used_namespace, false);
preferences.putString(key_nvs, value);
// Close the Preferences
preferences.end();
Serial.println(" ...done.");
}
/**************************************************************************/
/*!
@brief retrieve given value of a key in a namespace
@param used_namespace the intended namespace to get values
@param key_nvs the String key to be retrieved
@returns String retrieved string value
*/
/**************************************************************************/
String get_value(const char used_namespace[14], const char key_nvs[14]) {
Serial.print("[NVS] Loading ");
Serial.print(key_nvs);
Serial.print(" from ");
Serial.print(used_namespace);
Serial.print(": ");
// Open Preferences with my-app namespace.
// RW-mode if second parameter is false
preferences.begin(used_namespace, true);
// get value of key, assume value is String
// if key does not exist, return a default value
// const char *default_value = "none";
String tmp = preferences.getString(key_nvs);
// Close the Preferences
preferences.end();
Serial.print(tmp);
Serial.println(" ...done.");
return tmp;
}
/**************************************************************************/
/*!
@brief store given int value of a key in a namespace
@param used_namespace the intended namespace to be used
@param key_nvs the key to be stored
@param value the integer key to be stored
@returns void
*/
/**************************************************************************/
void set_integer(const char used_namespace[14], const char key_nvs[14], unsigned int value)
{
Serial.print("[NVS] Saving ");
Serial.print(key_nvs);
Serial.print(" to ");
Serial.print(used_namespace);
Serial.print(": ");
Serial.print(value);
preferences.begin(used_namespace, false);
preferences.putUInt(key_nvs, value);
// Close the Preferences
preferences.end();
Serial.println(" ...done.");
}
/**************************************************************************/
/*!
@brief retrieve given int value of a key in a namespace
@param used_namespace the intended namespace
@param key_nvs the key to be retrieved
@returns int retrieved integer value
*/
/**************************************************************************/
unsigned int get_integer(const char used_namespace[14], const char key_nvs[14])
{
Serial.print("[NVS] Loading ");
Serial.print(key_nvs);
Serial.print(" from ");
Serial.print(used_namespace);
Serial.print(": ");
preferences.begin(used_namespace, true);
unsigned int tmp = preferences.getUInt(key_nvs,0);
preferences.end();
Serial.print(tmp);
Serial.println(" ...done.");
return tmp;
}