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