From 0f3333b0c6a28f0aab369fec569b77dc5f60265d Mon Sep 17 00:00:00 2001 From: Andrea Gilardoni Date: Thu, 14 Nov 2024 11:18:06 +0100 Subject: [PATCH 1/3] [preferences] adding getType command --- libraries/Preferences/src/Preferences.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Preferences/src/Preferences.cpp b/libraries/Preferences/src/Preferences.cpp index 9bdac488..f1274554 100644 --- a/libraries/Preferences/src/Preferences.cpp +++ b/libraries/Preferences/src/Preferences.cpp @@ -187,7 +187,7 @@ size_t Preferences::putBytes(const char* key, const void* value, size_t len) { PreferenceType Preferences::getType(const char* key) { string res = ""; if (key != nullptr && strlen(key) > 0) { - if (modem.write(string(PROMPT(_PREF_PUT)), res, "%s%s\r\n", CMD_WRITE(_PREF_PUT), key)) { + if (modem.write(string(PROMPT(_PREF_TYPE)), res, "%s%s\r\n", CMD_WRITE(_PREF_TYPE), key)) { return static_cast(atoi(res.c_str())); } } From 30ba157841dd05c1e57825d94bbe6ea3af219019 Mon Sep 17 00:00:00 2001 From: Andrea Gilardoni Date: Thu, 14 Nov 2024 11:23:42 +0100 Subject: [PATCH 2/3] [preferences] improving string get and put --- libraries/Preferences/src/Preferences.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/libraries/Preferences/src/Preferences.cpp b/libraries/Preferences/src/Preferences.cpp index f1274554..3e689c95 100644 --- a/libraries/Preferences/src/Preferences.cpp +++ b/libraries/Preferences/src/Preferences.cpp @@ -162,8 +162,9 @@ size_t Preferences::putBool(const char* key, const bool value) { size_t Preferences::putString(const char* key, const char* value) { string res = ""; if (key != nullptr && strlen(key) > 0 && value != nullptr && strlen(value) > 0) { - if (modem.write(string(PROMPT(_PREF_PUT)), res, "%s%s,%d,%s\r\n", CMD_WRITE(_PREF_PUT), key, PT_STR, value)) { - return atoi(res.c_str()); + modem.write_nowait(string(PROMPT(_PREF_PUT)), res, "%s%s,%d,%d\r\n", CMD_WRITE(_PREF_PUT), key, PT_STR, strlen(value)); + if(modem.passthrough((uint8_t *)value, strlen(value))) { + return strlen(value); } } return 0; @@ -307,9 +308,11 @@ bool Preferences::getBool(const char* key, const bool defaultValue) { size_t Preferences::getString(const char* key, char* value, const size_t maxLen) { string res = ""; if (key != nullptr && strlen(key) > 0 && value != nullptr) { + modem.read_using_size(); if (modem.write(string(PROMPT(_PREF_GET)), res, "%s%s,%d\r\n", CMD_WRITE(_PREF_GET), key, PT_STR)) { - if (res.length() < maxLen) { + if (res.length()+1 < maxLen) { // take into account \0 at the end strncpy(value, res.c_str(), res.length()); + value[res.length()] = '\0'; return res.length(); } } @@ -320,6 +323,7 @@ size_t Preferences::getString(const char* key, char* value, const size_t maxLen) String Preferences::getString(const char* key, const String defaultValue) { string res = ""; if (key != nullptr && strlen(key) > 0) { + modem.read_using_size(); if (modem.write(string(PROMPT(_PREF_GET)), res, "%s%s,%d,%s\r\n", CMD_WRITE(_PREF_GET), key, PT_STR, defaultValue.c_str())) { return String(res.c_str()); } From 0901777ea0d870e072205cb41ece8f1f26e1fbb8 Mon Sep 17 00:00:00 2001 From: pennam Date: Wed, 27 Nov 2024 15:05:05 +0100 Subject: [PATCH 3/3] [preferences] add example for type support checking --- .../examples/TypesCheck/TypesCheck.ino | 400 ++++++++++++++++++ 1 file changed, 400 insertions(+) create mode 100644 libraries/Preferences/examples/TypesCheck/TypesCheck.ino diff --git a/libraries/Preferences/examples/TypesCheck/TypesCheck.ino b/libraries/Preferences/examples/TypesCheck/TypesCheck.ino new file mode 100644 index 00000000..022ccf6d --- /dev/null +++ b/libraries/Preferences/examples/TypesCheck/TypesCheck.ino @@ -0,0 +1,400 @@ +/* + This example checks all possible types that can be stored with Preferences library +*/ +#include +#include +Preferences prefs; + +void setup() { + Serial.begin(115200); + + if (!prefs.begin("types")) { // use "types" namespace + Serial.println("Cannot initialize preferences"); + Serial.println("Make sure your WiFi firmware version is greater than 0.3.0"); + while(1) {}; + } + + if (!testChar()) { + return; + } + + if (!testUChar()) { + return; + } + + if (!testShort()) { + return; + } + + if (!testUShort()) { + return; + } + + if (!testInt()) { + return; + } + + if (!testUInt()) { + return; + } + + if (!testLong64()) { + return; + } + + if (!testULong64()) { + return; + } + + if (!testFloat()) { + return; + } + + if (!testDouble()) { + return; + } + + if (!testBytes()) { + return; + } + + if (!testString()) { + return; + } +} + +void loop() { + Serial.println(prefs.freeEntries()); + prefs.clear(); + Serial.println(prefs.freeEntries()); + prefs.end(); + + while(1) {}; +} + +int testChar() { + int8_t data; + int8_t value; + + data = INT8_MAX; + prefs.putChar("char", data); + value = prefs.getChar("char", 0); + if (value != 127) { + Serial.println("getChar MAX Error"); + return 0; + } else { + Serial.print("getChar MAX: "); + Serial.println(value); + } + + data = INT8_MIN; + prefs.putChar("char", data); + value = prefs.getChar("char", 0); + if (value != -128) { + Serial.println("getChar MIN Error"); + return 0; + } else { + Serial.print("getChar MIN: "); + Serial.println(value); + } + + return 1; +} + +int testUChar() { + uint8_t data; + uint8_t value; + + data = UINT8_MAX; + prefs.putUChar("uchar", data); + value = prefs.getUChar("uchar", 0); + if (value != 255) { + Serial.println("getUChar MAX Error"); + return 0; + } else { + Serial.print("getUChar MAX: "); + Serial.println(value); + } + + prefs.remove("uchar"); + + data = 0; + prefs.putUChar("uchar", data); + value = prefs.getUChar("uchar", 1); + if (value != 0) { + Serial.println("getUChar MIN Error"); + return 0; + } else { + Serial.print("getUChar MIN: "); + Serial.println(value); + } + + return 1; +} + +int testShort() { + int16_t data; + int16_t value; + + data = INT16_MAX; + prefs.putShort("short", data); + value = prefs.getShort("short", 0); + if (value != 32767) { + Serial.println("getShort MAX Error"); + return 0; + } else { + Serial.print("getShort MAX: "); + Serial.println(value); + } + + data = INT16_MIN; + prefs.putShort("short", data); + value = prefs.getShort("short", 0); + if (value != -32768) { + Serial.println("getShort MIN Error"); + return 0; + } else { + Serial.print("getShort MIN: "); + Serial.println(value); + } + + return 1; +} + +int testUShort() { + uint16_t data; + uint16_t value; + + data = UINT16_MAX; + prefs.putUShort("ushort", data); + value = prefs.getUShort("ushort", 0); + if (value != 65535U) { + Serial.println("getUShort MAX Error"); + return 0; + } else { + Serial.print("getUShort MAX: "); + Serial.println(value); + } + + data = 0; + prefs.putUShort("ushort", data); + value = prefs.getUShort("ushort", 1); + if (value != 0) { + Serial.println("getUShort MIN Error"); + return 0; + } else { + Serial.print("getUShort MIN: "); + Serial.println(value); + } + + return 1; +} + +int testInt() { + int32_t data; + int32_t value; + + data = INT32_MAX; + prefs.putInt("int", data); + value = prefs.getInt("int", 0); + if (value != 2147483647) { + Serial.println("getInt MAX Error"); + return 0; + } else { + Serial.print("getInt MAX: "); + Serial.println(value); + } + + data = INT32_MIN; + prefs.putInt("int", data); + value = prefs.getInt("int", 0); + if (value != (-2147483647 - 1)) { + Serial.println("getInt MIN Error"); + return 0; + } else { + Serial.print("getInt MIN: "); + Serial.println(value); + } + + return 1; +} + +int testUInt() { + uint32_t data; + uint32_t value; + + data = UINT32_MAX; + prefs.putUInt("uint", data); + value = prefs.getUInt("uint", 0); + if (value != 4294967295U) { + Serial.println("getUInt MAX Error"); + return 0; + } else { + Serial.print("getUInt MAX: "); + Serial.println(value); + } + + data = 0; + prefs.putUInt("uint", data); + value = prefs.getUInt("uint", 1); + if (value != 0) { + Serial.println("getUInt MIN Error"); + return 0; + } else { + Serial.print("getUInt MIN: "); + Serial.println(value); + } + + return 1; +} + +int testLong64() { + int64_t data; + int64_t value; + + data = INT64_MAX; + prefs.putLong64("long", data); + value = prefs.getLong64("long", 0); + if (value != 9223372036854775807LL) { + Serial.println("getLong MAX Error"); + return 0; + } else { + Serial.print("getLong MAX: "); + Serial.println(value); + } + + data = INT64_MIN; + prefs.putLong64("long", data); + value = prefs.getLong64("long", 0); + if (value != (-9223372036854775807LL - 1)) { + Serial.println("getLong MIN Error"); + return 0; + } else { + Serial.print("getLong MIN: "); + Serial.println(value); + } + + return 1; +} + +int testULong64() { + uint64_t data; + uint64_t value; + + data = UINT64_MAX; + prefs.putULong64("ulong", data); + value = prefs.getULong64("ulong", 0); + if (value != 18446744073709551615ULL) { + Serial.println("getULong64 MAX Error"); + return 0; + } else { + Serial.print("getULong64 MAX: "); + Serial.println(value); + } + + data = 0; + prefs.putULong64("ulong", data); + value = prefs.getULong64("ulong", 1); + if (value != 0) { + Serial.println("getULong64 MIN Error"); + return 0; + } else { + Serial.print("getULong64 MIN: "); + Serial.println(value); + } + + return 1; +} + +int testFloat() { + float data; + float value; + + data = FLT_MAX; + prefs.putFloat("float", data); + value = prefs.getFloat("float", 0); + if (value != FLT_MAX) { + Serial.println("getFloat MAX Error"); + return 0; + } else { + Serial.print("getFloat MAX: "); + Serial.println(value); + } + + data = FLT_MIN; + prefs.putFloat("float", data); + value = prefs.getFloat("float", 0); + if (value != FLT_MIN) { + Serial.println("getFloat MIN Error"); + return 0; + } else { + Serial.print("getFloat MIN: "); + Serial.println(value); + } + + return 1; +} + +int testDouble() { + double data; + double value; + + data = DBL_MAX; + prefs.putDouble("double", data); + value = prefs.getDouble("double", 0); + if (value != DBL_MAX) { + Serial.println("getDouble MAX Error"); + return 0; + } else { + Serial.print("getDouble MAX: "); + Serial.println(value); + } + + data = DBL_MIN; + prefs.putDouble("double", data); + value = prefs.getDouble("double", 0); + if (value != DBL_MIN) { + Serial.println("getDouble MIN Error"); + return 0; + } else { + Serial.print("getDouble MIN: "); + Serial.println(value); + } + + return 1; +} + +int testBytes() { + uint8_t data[] = {9, 30, 235, 255, 20, 15, 0, 1, 9, 30, 235, 255, 20, 15, 0, 1, 9, 30, 235, 255, 20, 15, 0, 1, 9, 30, 235, 255, 20, 15, 0, 1}; + size_t len; + + prefs.putBytes("bytes", data, sizeof(data)); + len = prefs.getBytesLength("bytes"); + uint8_t value[len]; + prefs.getBytes("bytes", value, len); + if (memcmp(data, value, len) != 0) { + Serial.println("getBytes Error"); + return 0; + } else { + Serial.println("getBytes OK"); + } + + return 1; +} + +int testString() { + const char* data = "Lorem ipsum dolor sit amet consectetur adipiscing elit."; + String value = "default"; + + prefs.putString("string", data); + value = prefs.getString("string", value); + if (strcmp(data, value.c_str()) != 0) { + Serial.println("getString Error"); + Serial.println("Make sure your WiFi firmware version is greater than 0.5.0"); + return 0; + } else { + Serial.println("getString OK"); + } + + return 1; +}