|
1 |
| -#include <Arduino.h> |
2 | 1 | #include <Preferences.h>
|
3 | 2 |
|
4 |
| - |
5 |
| -struct TestData { |
6 |
| - uint8_t id; |
7 |
| - uint16_t value; |
8 |
| -}; |
9 |
| - |
10 | 3 | Preferences preferences;
|
11 | 4 |
|
12 |
| -void validate_types() { |
13 |
| - assert(preferences.getType("char") == PT_I8); |
14 |
| - assert(preferences.getType("uchar") == PT_U8); |
15 |
| - assert(preferences.getType("short") == PT_I16); |
16 |
| - assert(preferences.getType("ushort") == PT_U16); |
17 |
| - assert(preferences.getType("int") == PT_I32); |
18 |
| - assert(preferences.getType("uint") == PT_U32); |
19 |
| - assert(preferences.getType("long") == PT_I32); |
20 |
| - assert(preferences.getType("ulong") == PT_U32); |
21 |
| - assert(preferences.getType("long64") == PT_I64); |
22 |
| - assert(preferences.getType("ulong64") == PT_U64); |
23 |
| - assert(preferences.getType("float") == PT_BLOB); |
24 |
| - assert(preferences.getType("double") == PT_BLOB); |
25 |
| - assert(preferences.getType("bool") == PT_U8); |
26 |
| - assert(preferences.getType("str") == PT_STR); |
27 |
| - assert(preferences.getType("strLen") == PT_STR); |
28 |
| - assert(preferences.getType("struct") == PT_BLOB); |
29 |
| -} |
30 |
| - |
31 |
| -// Function to increment string values |
32 |
| -void incrementStringValues(String &val_string, char *val_string_buf, size_t buf_size) { |
33 |
| - // Extract the number from string and increment it |
34 |
| - val_string = "str" + String(val_string.substring(3).toInt() + 1); |
35 |
| - |
36 |
| - // Extract the number from strLen and increment it |
37 |
| - String strLen_str = String(val_string_buf); |
38 |
| - int strLen_num = strLen_str.substring(6).toInt(); |
39 |
| - snprintf(val_string_buf, buf_size, "strLen%d", strLen_num + 1); |
40 |
| -} |
41 |
| - |
42 | 5 | void setup() {
|
43 | 6 | Serial.begin(115200);
|
| 7 | + |
44 | 8 | while (!Serial) {
|
45 | 9 | ;
|
46 | 10 | }
|
47 | 11 |
|
48 | 12 | preferences.begin("my-app", false);
|
49 | 13 |
|
50 |
| - // Get the preferences value and if not exists, use default parameter |
51 |
| - char val_char = preferences.getChar("char", 'A'); |
52 |
| - unsigned char val_uchar = preferences.getUChar("uchar", 0); |
53 |
| - int16_t val_short = preferences.getShort("short", 0); |
54 |
| - uint16_t val_ushort = preferences.getUShort("ushort", 0); |
55 |
| - int32_t val_int = preferences.getInt("int", 0); |
56 |
| - uint32_t val_uint = preferences.getUInt("uint", 0); |
57 |
| - int64_t val_long = preferences.getLong("long", 0); |
58 |
| - uint32_t val_ulong = preferences.getULong("ulong", 0); |
59 |
| - int64_t val_long64 = preferences.getLong64("long64", 0); |
60 |
| - uint64_t val_ulong64 = preferences.getULong64("ulong64", 0); |
61 |
| - float val_float = preferences.getFloat("float", 0.0f); |
62 |
| - double val_double = preferences.getDouble("double", 0.0); |
63 |
| - bool val_bool = preferences.getBool("bool", false); |
| 14 | + // Get the counter value, if the key does not exist, return a default value of 0 |
| 15 | + unsigned int counter = preferences.getUInt("counter", 0); |
64 | 16 |
|
65 |
| - // Strings |
66 |
| - String val_string = preferences.getString("str", "str0"); |
67 |
| - char val_string_buf[20] = "strLen0"; |
68 |
| - preferences.getString("strLen", val_string_buf, sizeof(val_string_buf)); |
| 17 | + // Print the counter to Serial Monitor |
| 18 | + Serial.printf("Current counter value: %u\n", counter); |
69 | 19 |
|
70 |
| - // Structure data |
71 |
| - TestData test_data = {0, 0}; |
72 |
| - |
73 |
| - size_t struct_size = preferences.getBytes("struct", &test_data, sizeof(test_data)); |
74 |
| - if (struct_size == 0) { |
75 |
| - // First time - set initial values using parameter names |
76 |
| - test_data.id = 1; |
77 |
| - test_data.value = 100; |
78 |
| - } |
| 20 | + // Increase counter by 1 |
| 21 | + counter++; |
79 | 22 |
|
80 |
| - Serial.printf("Values from Preferences: "); |
81 |
| - Serial.printf("char: %c | uchar: %u | short: %d | ushort: %u | int: %ld | uint: %lu | ", |
82 |
| - val_char, val_uchar, val_short, val_ushort, val_int, val_uint); |
83 |
| - Serial.printf("long: %lld | ulong: %lu | long64: %lld | ulong64: %llu | ", |
84 |
| - val_long, val_ulong, val_long64, val_ulong64); |
85 |
| - Serial.printf("float: %.2f | double: %.2f | bool: %s | str: %s | strLen: %s | struct: {id:%u,val:%u}\n", |
86 |
| - val_float, val_double, val_bool ? "true" : "false", val_string.c_str(), val_string_buf, test_data.id, test_data.value); |
| 23 | + // Store the counter to the Preferences |
| 24 | + preferences.putUInt("counter", counter); |
87 | 25 |
|
88 |
| - |
89 |
| - // Increment the values |
90 |
| - val_char += 1; // Increment char A -> B |
91 |
| - val_uchar += 1; |
92 |
| - val_short += 1; |
93 |
| - val_ushort += 1; |
94 |
| - val_int += 1; |
95 |
| - val_uint += 1; |
96 |
| - val_long += 1; |
97 |
| - val_ulong += 1; |
98 |
| - val_long64 += 1; |
99 |
| - val_ulong64 += 1; |
100 |
| - val_float += 1.1f; |
101 |
| - val_double += 1.1; |
102 |
| - val_bool = !val_bool; // Toggle boolean value |
103 |
| - |
104 |
| - // Increment string values using function |
105 |
| - incrementStringValues(val_string, val_string_buf, sizeof(val_string_buf)); |
106 |
| - |
107 |
| - test_data.id += 1; |
108 |
| - test_data.value += 10; |
109 |
| - |
110 |
| - // Store the updated values back to Preferences |
111 |
| - preferences.putChar("char", val_char); |
112 |
| - preferences.putUChar("uchar", val_uchar); |
113 |
| - preferences.putShort("short", val_short); |
114 |
| - preferences.putUShort("ushort", val_ushort); |
115 |
| - preferences.putInt("int", val_int); |
116 |
| - preferences.putUInt("uint", val_uint); |
117 |
| - preferences.putLong("long", val_long); |
118 |
| - preferences.putULong("ulong", val_ulong); |
119 |
| - preferences.putLong64("long64", val_long64); |
120 |
| - preferences.putULong64("ulong64", val_ulong64); |
121 |
| - preferences.putFloat("float", val_float); |
122 |
| - preferences.putDouble("double", val_double); |
123 |
| - preferences.putBool("bool", val_bool); |
124 |
| - preferences.putString("str", val_string); |
125 |
| - preferences.putString("strLen", val_string_buf); |
126 |
| - preferences.putBytes("struct", &test_data, sizeof(test_data)); |
127 |
| - |
128 |
| - // Check if the keys exist |
129 |
| - assert(preferences.isKey("char")); |
130 |
| - assert(preferences.isKey("struct")); |
131 |
| - |
132 |
| - // Validate the types of the keys |
133 |
| - validate_types(); |
134 |
| - |
135 |
| - // Close the Preferences, wait and restart |
| 26 | + // Close the Preferences |
136 | 27 | preferences.end();
|
137 |
| - Serial.flush(); |
| 28 | + |
| 29 | + // Wait 1 second |
138 | 30 | delay(1000);
|
| 31 | + |
| 32 | + // Restart ESP |
139 | 33 | ESP.restart();
|
140 | 34 | }
|
141 | 35 |
|
|
0 commit comments