|  | 
|  | 1 | +#include <Arduino.h> | 
| 1 | 2 | #include <Preferences.h> | 
| 2 | 3 | 
 | 
|  | 4 | + | 
|  | 5 | +struct TestData { | 
|  | 6 | +  uint8_t id; | 
|  | 7 | +  uint16_t value; | 
|  | 8 | +}; | 
|  | 9 | + | 
| 3 | 10 | Preferences preferences; | 
| 4 | 11 | 
 | 
|  | 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 | + | 
| 5 | 42 | void setup() { | 
| 6 | 43 |   Serial.begin(115200); | 
| 7 |  | - | 
| 8 | 44 |   while (!Serial) { | 
| 9 | 45 |     ; | 
| 10 | 46 |   } | 
| 11 | 47 | 
 | 
| 12 | 48 |   preferences.begin("my-app", false); | 
| 13 | 49 | 
 | 
| 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); | 
|  | 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); | 
| 16 | 64 | 
 | 
| 17 |  | -  // Print the counter to Serial Monitor | 
| 18 |  | -  Serial.printf("Current counter value: %u\n", counter); | 
|  | 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)); | 
| 19 | 69 | 
 | 
| 20 |  | -  // Increase counter by 1 | 
| 21 |  | -  counter++; | 
|  | 70 | +  // Structure data | 
|  | 71 | +  TestData test_data = {0, 0}; | 
| 22 | 72 | 
 | 
| 23 |  | -  // Store the counter to the Preferences | 
| 24 |  | -  preferences.putUInt("counter", counter); | 
|  | 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 | +  } | 
| 25 | 79 | 
 | 
| 26 |  | -  // Close the Preferences | 
| 27 |  | -  preferences.end(); | 
|  | 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); | 
| 28 | 87 | 
 | 
| 29 |  | -  // Wait 1 second | 
| 30 |  | -  delay(1000); | 
| 31 | 88 | 
 | 
| 32 |  | -  // Restart ESP | 
|  | 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 | 
|  | 136 | +  preferences.end(); | 
|  | 137 | +  Serial.flush(); | 
|  | 138 | +  delay(1000); | 
| 33 | 139 |   ESP.restart(); | 
| 34 | 140 | } | 
| 35 | 141 | 
 | 
|  | 
0 commit comments