From 49af25ab1502cef8d2a1f68d17f0534cfd20ecc7 Mon Sep 17 00:00:00 2001 From: Jakub Andrysek Date: Wed, 18 Jun 2025 15:13:00 +0200 Subject: [PATCH 1/5] feat(test): Enhance NVS test --- tests/validation/nvs/nvs.ino | 128 +++++++++++++++++++++++++++---- tests/validation/nvs/test_nvs.py | 12 +-- 2 files changed, 118 insertions(+), 22 deletions(-) diff --git a/tests/validation/nvs/nvs.ino b/tests/validation/nvs/nvs.ino index 20b5b460098..4f0c292a43f 100644 --- a/tests/validation/nvs/nvs.ino +++ b/tests/validation/nvs/nvs.ino @@ -1,35 +1,131 @@ +#include #include +#include + + +struct TestData { + uint8_t id; + uint16_t value; +}; Preferences preferences; +void validate_types() { + assert(preferences.getType("char") == PT_I8); + assert(preferences.getType("uchar") == PT_U8); + assert(preferences.getType("short") == PT_I16); + assert(preferences.getType("ushort") == PT_U16); + assert(preferences.getType("int") == PT_I32); + assert(preferences.getType("uint") == PT_U32); + assert(preferences.getType("long") == PT_I32); + assert(preferences.getType("ulong") == PT_U32); + assert(preferences.getType("long64") == PT_I64); + assert(preferences.getType("ulong64") == PT_U64); + assert(preferences.getType("float") == PT_BLOB); + assert(preferences.getType("double") == PT_BLOB); + assert(preferences.getType("bool") == PT_U8); + assert(preferences.getType("str") == PT_STR); + assert(preferences.getType("strLen") == PT_STR); + assert(preferences.getType("struct") == PT_BLOB); +} + void setup() { Serial.begin(115200); + while (!Serial) {} + + preferences.begin("my-app", false); + + // Get the preferences value and if not exists, use default parameter + char val_char = preferences.getChar("char", 'A'); + unsigned char val_uchar = preferences.getUChar("uchar", 0); + int16_t val_short = preferences.getShort("short", 0); + uint16_t val_ushort = preferences.getUShort("ushort", 0); + int32_t val_int = preferences.getInt("int", 0); + uint32_t val_uint = preferences.getUInt("uint", 0); + int64_t val_long = preferences.getLong("long", 0); + uint32_t val_ulong = preferences.getULong("ulong", 0); + int64_t val_long64 = preferences.getLong64("long64", 0); + uint64_t val_ulong64 = preferences.getULong64("ulong64", 0); + float val_float = preferences.getFloat("float", 0.0f); + double val_double = preferences.getDouble("double", 0.0); + bool val_bool = preferences.getBool("bool", false); + + // Strings + String val_string = preferences.getString("str", "str0"); + char val_string_buf[20] = "strLen0"; + preferences.getString("strLen", val_string_buf, sizeof(val_string_buf)); - while (!Serial) { - ; + // Structure data + TestData test_data = {0, 0}; + + size_t struct_size = preferences.getBytes("struct", &test_data, sizeof(test_data)); + if (struct_size == 0) { + // First time - set initial values using parameter names + test_data.id = 1; + test_data.value = 100; } - preferences.begin("my-app", false); + Serial.printf("Values from Preferences: "); + 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); + Serial.printf("long: %lld | ulong: %lu | long64: %lld | ulong64: %llu | ", + val_long, val_ulong, val_long64, val_ulong64); + Serial.printf("float: %.2f | double: %.2f | bool: %s | str: %s | strLen: %s | struct: {id:%u,val:%u}\n", + val_float, val_double, val_bool ? "true" : "false", val_string.c_str(), val_string_buf, test_data.id, test_data.value); - // Get the counter value, if the key does not exist, return a default value of 0 - unsigned int counter = preferences.getUInt("counter", 0); - // Print the counter to Serial Monitor - Serial.printf("Current counter value: %u\n", counter); + // Increment the values + val_char += 1; // Increment char A -> B + val_uchar += 1; + val_short += 1; + val_ushort += 1; + val_int += 1; + val_uint += 1; + val_long += 1; + val_ulong += 1; + val_long64 += 1; + val_ulong64 += 1; + val_float += 1.1f; + val_double += 1.1; + val_bool = !val_bool; // Toggle boolean value - // Increase counter by 1 - counter++; + // Extract the number from strLen and increment it + val_string = "str" + String(val_string.substring(3).toInt() + 1); + String strLen_str = String(val_string_buf); + int strLen_num = strLen_str.substring(6).toInt(); + snprintf(val_string_buf, sizeof(val_string_buf), "strLen%d", strLen_num + 1); - // Store the counter to the Preferences - preferences.putUInt("counter", counter); + test_data.id += 1; + test_data.value += 10; - // Close the Preferences - preferences.end(); + // Store the updated values back to Preferences + preferences.putChar("char", val_char); + preferences.putUChar("uchar", val_uchar); + preferences.putShort("short", val_short); + preferences.putUShort("ushort", val_ushort); + preferences.putInt("int", val_int); + preferences.putUInt("uint", val_uint); + preferences.putLong("long", val_long); + preferences.putULong("ulong", val_ulong); + preferences.putLong64("long64", val_long64); + preferences.putULong64("ulong64", val_ulong64); + preferences.putFloat("float", val_float); + preferences.putDouble("double", val_double); + preferences.putBool("bool", val_bool); + preferences.putString("str", val_string); + preferences.putString("strLen", val_string_buf); + preferences.putBytes("struct", &test_data, sizeof(test_data)); - // Wait 1 second - delay(1000); + // Check if the keys exist + assert(preferences.isKey("char")); + assert(preferences.isKey("struct")); - // Restart ESP + // Validate the types of the keys + validate_types(); + + // Close the Preferences, wait and restart + preferences.end(); + delay(1000); ESP.restart(); } diff --git a/tests/validation/nvs/test_nvs.py b/tests/validation/nvs/test_nvs.py index 424095a49ba..e336bf7d0eb 100644 --- a/tests/validation/nvs/test_nvs.py +++ b/tests/validation/nvs/test_nvs.py @@ -4,11 +4,11 @@ def test_nvs(dut): LOGGER = logging.getLogger(__name__) - LOGGER.info("Expecting counter value 0") - dut.expect_exact("Current counter value: 0") + LOGGER.info("Expecting default values from Preferences") + dut.expect_exact("Values from Preferences: char: A | uchar: 0 | short: 0 | ushort: 0 | int: 0 | uint: 0 | long: 0 | ulong: 0 | long64: 0 | ulong64: 0 | float: 0.00 | double: 0.00 | bool: false | str: str0 | strLen: strLen0 | struct: {id:1,val:100}") - LOGGER.info("Expecting counter value 1") - dut.expect_exact("Current counter value: 1") + LOGGER.info("Expecting updated preferences for the first time") + dut.expect_exact("Values from Preferences: char: B | uchar: 1 | short: 1 | ushort: 1 | int: 1 | uint: 1 | long: 1 | ulong: 1 | long64: 1 | ulong64: 1 | float: 1.10 | double: 1.10 | bool: true | str: str1 | strLen: strLen1 | struct: {id:2,val:110}") - LOGGER.info("Expecting counter value 2") - dut.expect_exact("Current counter value: 2") + LOGGER.info("Expecting updated preferences for the second time") + dut.expect_exact("Values from Preferences: char: C | uchar: 2 | short: 2 | ushort: 2 | int: 2 | uint: 2 | long: 2 | ulong: 2 | long64: 2 | ulong64: 2 | float: 2.20 | double: 2.20 | bool: false | str: str2 | strLen: strLen2 | struct: {id:3,val:120}") From e15e7f7ea980cc50b4944a5cf1e996f588fff767 Mon Sep 17 00:00:00 2001 From: Jakub Andrysek Date: Wed, 18 Jun 2025 15:27:55 +0200 Subject: [PATCH 2/5] fix(nvs): Remove unused Unity header and improve Serial wait loop --- tests/validation/nvs/nvs.ino | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/validation/nvs/nvs.ino b/tests/validation/nvs/nvs.ino index 4f0c292a43f..8ad786346a3 100644 --- a/tests/validation/nvs/nvs.ino +++ b/tests/validation/nvs/nvs.ino @@ -1,6 +1,5 @@ #include #include -#include struct TestData { @@ -31,7 +30,9 @@ void validate_types() { void setup() { Serial.begin(115200); - while (!Serial) {} + while (!Serial) { + ; + } preferences.begin("my-app", false); @@ -125,6 +126,7 @@ void setup() { // Close the Preferences, wait and restart preferences.end(); + Serial.flush(); delay(1000); ESP.restart(); } From e14204c5b4380ba6923fecd2851c8b446511fa7c Mon Sep 17 00:00:00 2001 From: Jakub Andrysek Date: Wed, 18 Jun 2025 15:41:39 +0200 Subject: [PATCH 3/5] refactor(nvs): Extract string increment logic into a separate function --- tests/validation/nvs/nvs.ino | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/tests/validation/nvs/nvs.ino b/tests/validation/nvs/nvs.ino index 8ad786346a3..5196b0459e9 100644 --- a/tests/validation/nvs/nvs.ino +++ b/tests/validation/nvs/nvs.ino @@ -28,6 +28,17 @@ void validate_types() { assert(preferences.getType("struct") == PT_BLOB); } +// Function to increment string values +void incrementStringValues(String &val_string, char *val_string_buf, size_t buf_size) { + // Extract the number from string and increment it + val_string = "str" + String(val_string.substring(3).toInt() + 1); + + // Extract the number from strLen and increment it + String strLen_str = String(val_string_buf); + int strLen_num = strLen_str.substring(6).toInt(); + snprintf(val_string_buf, buf_size, "strLen%d", strLen_num + 1); +} + void setup() { Serial.begin(115200); while (!Serial) { @@ -90,11 +101,8 @@ void setup() { val_double += 1.1; val_bool = !val_bool; // Toggle boolean value - // Extract the number from strLen and increment it - val_string = "str" + String(val_string.substring(3).toInt() + 1); - String strLen_str = String(val_string_buf); - int strLen_num = strLen_str.substring(6).toInt(); - snprintf(val_string_buf, sizeof(val_string_buf), "strLen%d", strLen_num + 1); + // Increment string values using function + incrementStringValues(val_string, val_string_buf, sizeof(val_string_buf)); test_data.id += 1; test_data.value += 10; From 13aac7627f1de072256c3c3a9ff83ccb006894f7 Mon Sep 17 00:00:00 2001 From: Jakub Andrysek Date: Thu, 19 Jun 2025 13:25:36 +0200 Subject: [PATCH 4/5] refactor(test): Format long strings in expect_exact calls for better readability --- tests/validation/nvs/nvs.ino | 18 ++++++++---------- tests/validation/nvs/test_nvs.py | 18 +++++++++++++++--- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/tests/validation/nvs/nvs.ino b/tests/validation/nvs/nvs.ino index 5196b0459e9..12c640c65b0 100644 --- a/tests/validation/nvs/nvs.ino +++ b/tests/validation/nvs/nvs.ino @@ -1,7 +1,6 @@ #include #include - struct TestData { uint8_t id; uint16_t value; @@ -78,16 +77,15 @@ void setup() { } Serial.printf("Values from Preferences: "); - 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); - Serial.printf("long: %lld | ulong: %lu | long64: %lld | ulong64: %llu | ", - val_long, val_ulong, val_long64, val_ulong64); - Serial.printf("float: %.2f | double: %.2f | bool: %s | str: %s | strLen: %s | struct: {id:%u,val:%u}\n", - val_float, val_double, val_bool ? "true" : "false", val_string.c_str(), val_string_buf, test_data.id, test_data.value); - + 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); + Serial.printf("long: %lld | ulong: %lu | long64: %lld | ulong64: %llu | ", val_long, val_ulong, val_long64, val_ulong64); + Serial.printf( + "float: %.2f | double: %.2f | bool: %s | str: %s | strLen: %s | struct: {id:%u,val:%u}\n", val_float, val_double, val_bool ? "true" : "false", + val_string.c_str(), val_string_buf, test_data.id, test_data.value + ); // Increment the values - val_char += 1; // Increment char A -> B + val_char += 1; // Increment char A -> B val_uchar += 1; val_short += 1; val_ushort += 1; @@ -99,7 +97,7 @@ void setup() { val_ulong64 += 1; val_float += 1.1f; val_double += 1.1; - val_bool = !val_bool; // Toggle boolean value + val_bool = !val_bool; // Toggle boolean value // Increment string values using function incrementStringValues(val_string, val_string_buf, sizeof(val_string_buf)); diff --git a/tests/validation/nvs/test_nvs.py b/tests/validation/nvs/test_nvs.py index e336bf7d0eb..7f595b8e93d 100644 --- a/tests/validation/nvs/test_nvs.py +++ b/tests/validation/nvs/test_nvs.py @@ -5,10 +5,22 @@ def test_nvs(dut): LOGGER = logging.getLogger(__name__) LOGGER.info("Expecting default values from Preferences") - dut.expect_exact("Values from Preferences: char: A | uchar: 0 | short: 0 | ushort: 0 | int: 0 | uint: 0 | long: 0 | ulong: 0 | long64: 0 | ulong64: 0 | float: 0.00 | double: 0.00 | bool: false | str: str0 | strLen: strLen0 | struct: {id:1,val:100}") + dut.expect_exact( + "Values from Preferences: char: A | uchar: 0 | short: 0 | ushort: 0 | int: 0 | uint: 0 | long: 0 | ulong: 0 | " + "long64: 0 | ulong64: 0 | float: 0.00 | double: 0.00 | bool: false | str: str0 | strLen: strLen0 | " + "struct: {id:1,val:100}" + ) LOGGER.info("Expecting updated preferences for the first time") - dut.expect_exact("Values from Preferences: char: B | uchar: 1 | short: 1 | ushort: 1 | int: 1 | uint: 1 | long: 1 | ulong: 1 | long64: 1 | ulong64: 1 | float: 1.10 | double: 1.10 | bool: true | str: str1 | strLen: strLen1 | struct: {id:2,val:110}") + dut.expect_exact( + "Values from Preferences: char: B | uchar: 1 | short: 1 | ushort: 1 | int: 1 | uint: 1 | long: 1 | ulong: 1 | " + "long64: 1 | ulong64: 1 | float: 1.10 | double: 1.10 | bool: true | str: str1 | strLen: strLen1 | " + "struct: {id:2,val:110}" + ) LOGGER.info("Expecting updated preferences for the second time") - dut.expect_exact("Values from Preferences: char: C | uchar: 2 | short: 2 | ushort: 2 | int: 2 | uint: 2 | long: 2 | ulong: 2 | long64: 2 | ulong64: 2 | float: 2.20 | double: 2.20 | bool: false | str: str2 | strLen: strLen2 | struct: {id:3,val:120}") + dut.expect_exact( + "Values from Preferences: char: C | uchar: 2 | short: 2 | ushort: 2 | int: 2 | uint: 2 | long: 2 | ulong: 2 | " + "long64: 2 | ulong64: 2 | float: 2.20 | double: 2.20 | bool: false | str: str2 | strLen: strLen2 | " + "struct: {id:3,val:120}" + ) From 7fabe3bb5acb04cc4b75edc744cf05839aebd2bc Mon Sep 17 00:00:00 2001 From: Jakub Andrysek Date: Thu, 26 Jun 2025 15:53:49 +0200 Subject: [PATCH 5/5] feat(SDFS): Add destructor to clean up resources --- libraries/SD/src/SD.cpp | 4 ++++ libraries/SD/src/SD.h | 1 + 2 files changed, 5 insertions(+) diff --git a/libraries/SD/src/SD.cpp b/libraries/SD/src/SD.cpp index 2d646276d87..077a7c1121f 100644 --- a/libraries/SD/src/SD.cpp +++ b/libraries/SD/src/SD.cpp @@ -22,6 +22,10 @@ using namespace fs; SDFS::SDFS(FSImplPtr impl) : FS(impl), _pdrv(0xFF) {} +SDFS::~SDFS() { + end(); +} + bool SDFS::begin(uint8_t ssPin, SPIClass &spi, uint32_t frequency, const char *mountpoint, uint8_t max_files, bool format_if_empty) { if (_pdrv != 0xFF) { return true; diff --git a/libraries/SD/src/SD.h b/libraries/SD/src/SD.h index aebc781a5e3..d8252ee44f7 100644 --- a/libraries/SD/src/SD.h +++ b/libraries/SD/src/SD.h @@ -26,6 +26,7 @@ class SDFS : public FS { public: SDFS(FSImplPtr impl); + ~SDFS(); bool begin( uint8_t ssPin = SS, SPIClass &spi = SPI, uint32_t frequency = 4000000, const char *mountpoint = "/sd", uint8_t max_files = 5, bool format_if_empty = false );