From 04fd7c28dc104b1c90c0789515ce55fa0185395b Mon Sep 17 00:00:00 2001 From: Mathieu Carbou Date: Thu, 14 Nov 2024 14:24:47 +0100 Subject: [PATCH 1/6] using std::vector instead --- platformio.ini | 2 + src/Chart.cpp | 32 +++---- src/Chart.h | 15 +-- src/ESPDash.cpp | 52 ++++------- src/ESPDash.h | 9 +- src/Statistic.h | 1 - src/vector.h | 243 ------------------------------------------------ 7 files changed, 49 insertions(+), 305 deletions(-) delete mode 100644 src/vector.h diff --git a/platformio.ini b/platformio.ini index d57f0eae..ba95bc4d 100644 --- a/platformio.ini +++ b/platformio.ini @@ -13,6 +13,7 @@ build_flags = -Wall -Wextra -D CONFIG_ARDUHAL_LOG_COLORS -D CORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG + ; -D DASH_USE_LEGACY_CHART_STORAGE=1 lib_deps = bblanchon/ArduinoJson@^7.1.0 mathieucarbou/ESPAsyncWebServer@^3.3.14 @@ -27,6 +28,7 @@ board = esp32-s3-devkitc-1 [env:arduino-3] platform = https://github.com/pioarduino/platform-espressif32/releases/download/53.03.10-rc1/platform-espressif32.zip board = esp32-s3-devkitc-1 +; board = esp32dev [env:esp8266] platform = espressif8266 diff --git a/src/Chart.cpp b/src/Chart.cpp index 7397a85c..fadb6610 100644 --- a/src/Chart.cpp +++ b/src/Chart.cpp @@ -13,19 +13,19 @@ Chart::Chart(ESPDash *dashboard, const int type, const char* name){ #if DASH_USE_LEGACY_CHART_STORAGE == 1 void Chart::emptyXAxisVectors() { - if(!_x_axis_i.Empty()) - _x_axis_i.Clear(); - if(!_x_axis_f.Empty()) - _x_axis_f.Clear(); - if(!_x_axis_s.Empty()) - _x_axis_s.Clear(); + if(!_x_axis_i.empty()) + _x_axis_i.clear(); + if(!_x_axis_f.empty()) + _x_axis_f.clear(); + if(!_x_axis_s.empty()) + _x_axis_s.clear(); } void Chart::emptyYAxisVectors() { - if(!_y_axis_i.Empty()) - _y_axis_i.Clear(); - if(!_y_axis_f.Empty()) - _y_axis_f.Clear(); + if(!_y_axis_i.empty()) + _y_axis_i.clear(); + if(!_y_axis_f.empty()) + _y_axis_f.clear(); } #else void Chart::clearXAxisPointers() { @@ -51,7 +51,7 @@ void Chart::updateX(int arr_x[], size_t x_size){ #if DASH_USE_LEGACY_CHART_STORAGE == 1 emptyXAxisVectors(); for(int i=0; i < x_size; i++){ - _x_axis_i.PushBack(arr_x[i]); + _x_axis_i.push_back(arr_x[i]); } #else clearXAxisPointers(); @@ -66,7 +66,7 @@ void Chart::updateX(float arr_x[], size_t x_size){ #if DASH_USE_LEGACY_CHART_STORAGE == 1 emptyXAxisVectors(); for(int i=0; i < x_size; i++){ - _x_axis_f.PushBack(arr_x[i]); + _x_axis_f.push_back(arr_x[i]); } #else clearXAxisPointers(); @@ -81,7 +81,7 @@ void Chart::updateX(String arr_x[], size_t x_size){ #if DASH_USE_LEGACY_CHART_STORAGE == 1 emptyXAxisVectors(); for(int i=0; i < x_size; i++){ - _x_axis_s.PushBack(arr_x[i].c_str()); + _x_axis_s.push_back(arr_x[i].c_str()); } #else clearXAxisPointers(); @@ -96,7 +96,7 @@ void Chart::updateX(const char* arr_x[], size_t x_size){ #if DASH_USE_LEGACY_CHART_STORAGE == 1 emptyXAxisVectors(); for(int i=0; i < x_size; i++){ - _x_axis_s.PushBack(String(arr_x[i])); + _x_axis_s.push_back(String(arr_x[i])); } #else clearXAxisPointers(); @@ -111,7 +111,7 @@ void Chart::updateY(int arr_y[], size_t y_size){ #if DASH_USE_LEGACY_CHART_STORAGE == 1 emptyYAxisVectors(); for(int i=0; i < y_size; i++){ - _y_axis_i.PushBack(arr_y[i]); + _y_axis_i.push_back(arr_y[i]); } #else clearYAxisPointers(); @@ -126,7 +126,7 @@ void Chart::updateY(float arr_y[], size_t y_size){ #if DASH_USE_LEGACY_CHART_STORAGE == 1 emptyYAxisVectors(); for(int i=0; i < y_size; i++){ - _y_axis_f.PushBack(arr_y[i]); + _y_axis_f.push_back(arr_y[i]); } #else clearYAxisPointers(); diff --git a/src/Chart.h b/src/Chart.h index e4bfb912..55a44022 100644 --- a/src/Chart.h +++ b/src/Chart.h @@ -3,7 +3,6 @@ #include #include "Arduino.h" -#include "vector.h" #include "ESPDash.h" #include "ArduinoJson.h" @@ -12,6 +11,10 @@ #define DASH_USE_LEGACY_CHART_STORAGE 0 #endif +#if DASH_USE_LEGACY_CHART_STORAGE == 1 + #include +#endif + // Default to Line Chart enum { BAR_CHART, @@ -42,12 +45,12 @@ class Chart { #if DASH_USE_LEGACY_CHART_STORAGE == 1 /* X-Axis */ - Vector _x_axis_i; - Vector _x_axis_f; - Vector _x_axis_s; + std::vector _x_axis_i; + std::vector _x_axis_f; + std::vector _x_axis_s; /* Y-Axis */ - Vector _y_axis_i; - Vector _y_axis_f; + std::vector _y_axis_i; + std::vector _y_axis_f; void emptyXAxisVectors(); void emptyYAxisVectors(); diff --git a/src/ESPDash.cpp b/src/ESPDash.cpp index a0d34586..7ea478a5 100644 --- a/src/ESPDash.cpp +++ b/src/ESPDash.cpp @@ -73,7 +73,7 @@ ESPDash::ESPDash(AsyncWebServer* server, const char* uri, bool enable_default_st } else if (json["command"] == "button:clicked") { // execute and reference card data struct to funtion uint32_t id = json["id"].as(); - for(int i=0; i < cards.Size(); i++){ + for(int i=0; i < cards.size(); i++){ Card *p = cards[i]; if(id == p->_id){ if(p->_callback != nullptr){ @@ -86,7 +86,7 @@ ESPDash::ESPDash(AsyncWebServer* server, const char* uri, bool enable_default_st } else if (json["command"] == "slider:changed") { // execute and reference card data struct to funtion uint32_t id = json["id"].as(); - for(int i=0; i < cards.Size(); i++){ + for(int i=0; i < cards.size(); i++){ Card *p = cards[i]; if(id == p->_id){ if(p->_callback_f != nullptr && p->_value_type == Card::FLOAT){ @@ -125,51 +125,33 @@ void ESPDash::setAuthentication(const String &user, const String &pass) { // Add Card void ESPDash::add(Card *card) { - cards.PushBack(card); + cards.push_back(card); } // Remove Card void ESPDash::remove(Card *card) { - for(int i=0; i < cards.Size(); i++){ - Card *p = cards[i]; - if(p->_id == card->_id){ - cards.Erase(i); - return; - } - } + cards.erase(std::remove(cards.begin(), cards.end(), card), cards.end()); } // Add Chart void ESPDash::add(Chart *chart) { - charts.PushBack(chart); + charts.push_back(chart); } // Remove Card void ESPDash::remove(Chart *chart) { - for(int i=0; i < charts.Size(); i++){ - Chart *p = charts[i]; - if(p->_id == chart->_id){ - charts.Erase(i); - return; - } - } + charts.erase(std::remove(charts.begin(), charts.end(), chart), charts.end()); } // Add Statistic void ESPDash::add(Statistic *statistic) { - statistics.PushBack(statistic); + statistics.push_back(statistic); } // Remove Statistic void ESPDash::remove(Statistic *statistic) { - for(int i=0; i < statistics.Size(); i++){ - Statistic *p = statistics[i]; - if(p->_id == statistic->_id){ - statistics.Erase(i); - return; - } - } + statistics.erase(std::remove(statistics.begin(), statistics.end(), statistic), statistics.end()); } // generates the layout JSON string to the frontend @@ -192,7 +174,7 @@ void ESPDash::generateLayoutJSON(AsyncWebSocketClient* client, bool changes_only // Generate JSON for all Cards doc["command"] = changes_only ? "update:components" : "update:layout:next"; - for (int i = 0; i < cards.Size(); i++) { + for (int i = 0; i < cards.size(); i++) { Card* c = cards[i]; if (changes_only) { if (!c->_changed && (onlyCard == nullptr || onlyCard->_id != c->_id)) { @@ -227,7 +209,7 @@ void ESPDash::generateLayoutJSON(AsyncWebSocketClient* client, bool changes_only // Generate JSON for all Charts doc["command"] = changes_only ? "update:components" : "update:layout:next"; - for (int i = 0; i < charts.Size(); i++) { + for (int i = 0; i < charts.size(); i++) { Chart* c = charts[i]; if (changes_only) { if (!c->_x_changed && !c->_y_changed && (onlyChart == nullptr || onlyChart->_id != c->_id)) { @@ -311,7 +293,7 @@ void ESPDash::generateLayoutJSON(AsyncWebSocketClient* client, bool changes_only } // Loop through user defined stats - for (int i = 0; i < statistics.Size(); i++) { + for (int i = 0; i < statistics.size(); i++) { Statistic* s = statistics[i]; if (changes_only) { if (!s->_changed) { @@ -426,7 +408,7 @@ void ESPDash::generateComponentJSON(JsonObject& doc, Chart* chart, bool change_o switch (chart->_x_axis_type) { case GraphAxisType::INTEGER: #if DASH_USE_LEGACY_CHART_STORAGE == 1 - for(int i=0; i < chart->_x_axis_i.Size(); i++) + for(int i=0; i < chart->_x_axis_i.size(); i++) xAxis.add(chart->_x_axis_i[i]); #else if (chart->_x_axis_i_ptr != nullptr) { @@ -437,7 +419,7 @@ void ESPDash::generateComponentJSON(JsonObject& doc, Chart* chart, bool change_o break; case GraphAxisType::FLOAT: #if DASH_USE_LEGACY_CHART_STORAGE == 1 - for(int i=0; i < chart->_x_axis_f.Size(); i++) + for(int i=0; i < chart->_x_axis_f.size(); i++) xAxis.add(chart->_x_axis_f[i]); #else if (chart->_x_axis_f_ptr != nullptr) { @@ -448,7 +430,7 @@ void ESPDash::generateComponentJSON(JsonObject& doc, Chart* chart, bool change_o break; case GraphAxisType::CHAR: #if DASH_USE_LEGACY_CHART_STORAGE == 1 - for(int i=0; i < chart->_x_axis_s.Size(); i++) + for(int i=0; i < chart->_x_axis_s.size(); i++) xAxis.add(chart->_x_axis_s[i].c_str()); #else if (chart->_x_axis_char_ptr != nullptr) { @@ -459,7 +441,7 @@ void ESPDash::generateComponentJSON(JsonObject& doc, Chart* chart, bool change_o break; case GraphAxisType::STRING: #if DASH_USE_LEGACY_CHART_STORAGE == 1 - for(int i=0; i < chart->_x_axis_s.Size(); i++) + for(int i=0; i < chart->_x_axis_s.size(); i++) xAxis.add(chart->_x_axis_s[i].c_str()); #else if (chart->_x_axis_s_ptr != nullptr) { @@ -479,7 +461,7 @@ void ESPDash::generateComponentJSON(JsonObject& doc, Chart* chart, bool change_o switch (chart->_y_axis_type) { case GraphAxisType::INTEGER: #if DASH_USE_LEGACY_CHART_STORAGE == 1 - for(int i=0; i < chart->_y_axis_i.Size(); i++) + for(int i=0; i < chart->_y_axis_i.size(); i++) yAxis.add(chart->_y_axis_i[i]); #else if (chart->_y_axis_i_ptr != nullptr) { @@ -490,7 +472,7 @@ void ESPDash::generateComponentJSON(JsonObject& doc, Chart* chart, bool change_o break; case GraphAxisType::FLOAT: #if DASH_USE_LEGACY_CHART_STORAGE == 1 - for(int i=0; i < chart->_y_axis_f.Size(); i++) + for(int i=0; i < chart->_y_axis_f.size(); i++) yAxis.add(chart->_y_axis_f[i]); #else if (chart->_y_axis_f_ptr != nullptr) { diff --git a/src/ESPDash.h b/src/ESPDash.h index 6f255c71..80e116d5 100644 --- a/src/ESPDash.h +++ b/src/ESPDash.h @@ -22,7 +22,6 @@ Github URL: https://github.com/ayushsharma82/ESP-DASH #include "Arduino.h" #include "stdlib_noniso.h" #include "dash_webpage.h" -#include "vector.h" #if defined(ESP8266) #define DASH_HARDWARE "ESP8266" @@ -45,6 +44,8 @@ Github URL: https://github.com/ayushsharma82/ESP-DASH #include "Chart.h" #include "Statistic.h" +#include + // If DASH_JSON_SIZE is set to a value, ESP-DASH will frequently measure the Json payload to make sure it remains within this size. // If the Json payload to send is larger, the payload will be split in several parts and sent in multiple messages. // @@ -111,9 +112,9 @@ class ESPDash{ AsyncWebServer* _server = nullptr; AsyncWebSocket* _ws = nullptr; - Vector cards; - Vector charts; - Vector statistics; + std::vector cards; + std::vector charts; + std::vector statistics; bool default_stats_enabled = false; bool basic_auth = false; String username; diff --git a/src/Statistic.h b/src/Statistic.h index 819343a5..ae3cadf3 100644 --- a/src/Statistic.h +++ b/src/Statistic.h @@ -3,7 +3,6 @@ #include #include "Arduino.h" -#include "vector.h" #include "ESPDash.h" #include "ArduinoJson.h" diff --git a/src/vector.h b/src/vector.h deleted file mode 100644 index 1c55e7f2..00000000 --- a/src/vector.h +++ /dev/null @@ -1,243 +0,0 @@ -#ifndef VectorClass -#define VectorClass - - -#ifndef MIN -#define MIN(a, b) (((a) < (b)) ? (a) : (b)) -#endif - -#ifndef MAX -#define MAX(a, b) (((a) > (b)) ? (a) : (b)) -#endif - -#define SWAP(type, a, b) type tmp ## a = a; a = b; b = tmp ## a; - -// This class implements missing vector from STL -template class Vector -{ - VectorType *begin; - VectorType *storage; - int head; - -public: - VectorType OB; - - // We can save a few re-sizings if we know how large the array is likely to grow to be - Vector(int initialSize = 0) - { - begin = new VectorType[initialSize]; //points to the beginning of the new array - head = initialSize - 1; - storage = begin + initialSize; //points to the element one outside of the array (such that end - begin = capacity) - } - - Vector(Vector &obj) - { - begin = new VectorType[0]; // Points to the beginning of the new array, it's zero but this line keeps malloc from seg faulting should we delete begin before resizing it - head = -1; - storage = begin; //points to the element one outside of the array (such that end - begin = capacity) - - *this = obj; - } - - // If there's anything in the vector then delete the array, if there's no array then doing will will cause seg faults - virtual ~Vector() { delete[] begin; } - - Vector &operator=(Vector &obj) - { - // Reallocate the underlying buffer to the same size as the - Resize(obj.Size()); - - for(int i = 0; i < obj.Size(); i++) - (*this)[i] = obj[i]; - - head = obj.head; - - return *this; - } - - // Swaps the underlying array and characteristics of this vector with another of the same type, very quickly - void Swap(Vector &obj) - { - SWAP(int, head, obj.head); - SWAP(VectorType*, begin, obj.begin); - SWAP(VectorType*, storage, obj.storage); - } - - // Checks the entire Vector to see whether a matching item exists. Bear in mind that the VectorType might need to implement - // equality operator (operator==) for this to work properly. - bool Contains(VectorType element) - { - for(int i = 0; i < Size(); i++) - if(operator [](i) == element) - return true; - - return false; - } - - int Find(VectorType element) - { - for(int i = 0; i < Size(); i++) - if(operator [](i) == element) - return i; - - return -1; - } - - void PushBack(VectorType element) { PushBack(&element, 1); } - - void PushBack(const VectorType *elements, int len) - { - // If the length plus this's size is greater than the capacity, reallocate to that size. - if(len + Size() > Capacity()) - ReAllocate(MAX(Size() + len, Size() * 2)); - - int append = MIN(storage - begin - head - 1, len), prepend = len - append; - - // memcpy the data starting at the head all the way up to the last element *(storage - 1) - memcpy((begin + head + 1), elements, sizeof(VectorType) * append); - - // If there's still data to copy memcpy whatever remains, starting at the first element *(begin) until the end of data. The first step will have ensured - // that we don't crash into the tail during this process. - memcpy(begin,(elements + append), sizeof(VectorType) * prepend); - - // Re-recalculate head and size. - head += len; - } - - void Erase(unsigned int position) { Erase(position, position + 1); } - - // Erase an arbitrary section of the vector from first up to last minus one. Like the stl counterpart, this is pretty labour intensive so go easy on it. - void Erase(int first, int last) - { - // For this we'll set the value of the array at first to the value of the array at last plus one. We'll do that all the way up to toIndex - for(int i = 0; i < (Size() - first); i++) - { - // If by trying to fill in the next element with the ones ahead of it we'll be running off the end of the vector, stop. - if((i + last) > (Size() - 1)) - break; - - begin[first + i] = begin[last + i]; - } - - // Adjust the head to reflect the new size - head -= last - first; - } - - // Remove the most recent element in the array - void PopBack() - { - if(Size() > 0) - head--; - } - - // Empty the vector, or to be precise - forget the fact that there was ever anything in there. - void Clear() { head = -1; } - - // Returns a bool indicating whether or not there are any elements in the array - bool Empty() { return head == -1; } - - // Returns the oldest element in the array (the one added before any other) - VectorType const &Back() { return *begin; } - - // Returns the newest element in the array (the one added after every other) - VectorType const &Front() { return begin[head]; } - - // Returns the nth element in the vector - VectorType &operator[](int n) - { - if(n < Size()) - return begin[n]; - else - return OB; // out of bounds - } - - // Returns a pointer such that the vector's data is laid out between ret to ret + size - VectorType *Data() { return begin; } - - // Recreates the vector to hold len elements, all being copies of val - void Assign(int len, const VectorType &val) - { - delete[] begin; - - // Allocate an array the same size as the one passed in - begin = new VectorType[len]; - storage = begin + len; - - // Refresh the head and tail, assuming the array is in order, which it really has to be - head = len - 1; - - for(int i = 0 ; i < Size(); i++) - begin[i] = val; - } - - // Recreates the vector using an external array - void Assign(VectorType *array, int len) - { - delete[] begin; - - // Allocate an array the same size as the one passed in - begin = new VectorType[len]; - storage = begin + len; - - // Refresh the head and tail, assuming the array is in order, which it really has to be - head = len - 1; - - // Copy over the memory - memcpy(begin, array, sizeof(VectorType) * len); - } - - // Returns the number of elements that the vector will support before needing resizing - int Capacity() { return (storage - begin); } - - // Returns the number of elements in vector - int Size() { return head + 1; } - - // Requests that the capacity of the allocated storage space for the elements - // of the vector be at least enough to hold size elements. - void Reserve(unsigned int size) - { - if(size > Capacity()) - ReAllocate(size); - } - - // Resizes the vector - void Resize(unsigned int size) - { - // If necessary, resize the underlying array to fit the new size - if(size > Capacity()) - ReAllocate(size); - - // Now revise the head and size (tail needn't change) to reflect the new size - head = size - 1; - } - -private: - - void ReAllocate(unsigned int size) - { - // Just in case we're re-allocating less room than we had before, make sure that we don't overrun the buffer by trying to write more elements than - // are now possible for this vector to hold. - if(Size() > (int)size) - head = size - 1; - - // Allocate an array twice the size of that of the old - VectorType *_begin = new VectorType[size]; - VectorType *_storage = _begin + size; - - int _head = Size() - 1; - - // Copy across all the old array's data and rearrange it! - for(int i = 0; i < Size(); i++) - _begin[i] = (*this)[i]; - - // Free the old memory - delete[] begin; - - // Redirect the old array to point to the new one - begin = _begin; - storage = _storage; - head = _head; - } -}; - -#endif \ No newline at end of file From 6c8452f91894246d005704c8eb1f6da4f5ac50c0 Mon Sep 17 00:00:00 2001 From: Mathieu Carbou Date: Thu, 14 Nov 2024 15:24:10 +0100 Subject: [PATCH 2/6] Support move semantic in ESP-DASH --- src/Card.cpp | 24 ++++++++++++++++++++++++ src/Card.h | 2 ++ src/Statistic.cpp | 9 +++++++++ src/Statistic.h | 2 ++ 4 files changed, 37 insertions(+) diff --git a/src/Card.cpp b/src/Card.cpp index c890c944..07b6cd2e 100644 --- a/src/Card.cpp +++ b/src/Card.cpp @@ -1,5 +1,7 @@ #include "Card.h" +#include + /* Constructor */ @@ -128,6 +130,28 @@ void Card::update(const char* value){ _value_s = value; } +void Card::update(String&& value, const char* symbol){ + if(_value_type == Card::STRING){ + if(_value_s != value) + _changed = true; + } + if (strcmp(_symbol.c_str(), symbol) != 0) { + _changed = true; + } + _value_type = Card::STRING; + _symbol = symbol; + _value_s = std::move(value); +} + +void Card::update(String&& value){ + if(_value_type == Card::STRING){ + if(_value_s != value) + _changed = true; + } + _value_type = Card::STRING; + _value_s = std::move(value); +} + void Card::update(bool value, const char* symbol){ /* Clear String if it was used before */ if(_value_type == Card::STRING){ diff --git a/src/Card.h b/src/Card.h index 5e2948b1..530bb694 100644 --- a/src/Card.h +++ b/src/Card.h @@ -73,6 +73,8 @@ class Card { void update(const char* value, const char* symbol); void update(const String &value); void update(const String &value, const char* symbol); + void update(String &&value); + void update(String &&value, const char* symbol); ~Card(); friend class ESPDash; diff --git a/src/Statistic.cpp b/src/Statistic.cpp index 46995903..71307a4a 100644 --- a/src/Statistic.cpp +++ b/src/Statistic.cpp @@ -1,5 +1,7 @@ #include "Statistic.h" +#include + Statistic::Statistic(ESPDash *dashboard, const char *key, const char *value) { _dashboard = dashboard; _id = dashboard->nextId(); @@ -16,6 +18,13 @@ void Statistic::set(const char *value) { _value = value; } +void Statistic::set(String&& value) { + // Safe copy + _changed = _value != value; + if(_changed) + _value = std::move(value); +} + Statistic::~Statistic() { _dashboard->remove(this); } \ No newline at end of file diff --git a/src/Statistic.h b/src/Statistic.h index ae3cadf3..6efe148c 100644 --- a/src/Statistic.h +++ b/src/Statistic.h @@ -21,6 +21,8 @@ class Statistic { public: Statistic(ESPDash *dashboard, const char *key, const char *value = ""); void set(const char *value); + void set(const String& value) { set(value.c_str()); } + void set(String&& value); ~Statistic(); friend class ESPDash; From 1322da8b500195de8ef4847d656a3592a285e35c Mon Sep 17 00:00:00 2001 From: Mathieu Carbou Date: Thu, 14 Nov 2024 15:25:59 +0100 Subject: [PATCH 3/6] Fix CI --- .github/workflows/ci.yml | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5976c27e..61a00df3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,10 +28,12 @@ jobs: core: esp32:esp32 board: esp32:esp32:esp32 index_url: https://espressif.github.io/arduino-esp32/package_esp32_index.json - - name: package_esp32_dev_index.json - core: esp32:esp32 - board: esp32:esp32:esp32 - index_url: https://espressif.github.io/arduino-esp32/package_esp32_dev_index.json + # Disabled due to an error coming from the package file: + # Error during install: Cannot install tool esp32:esp32-arduino-libs@idf-release_v5.3-a0f798cf: testing local archive integrity: testing archive checksum: archive hash differs from hash in index + # - name: package_esp32_dev_index.json + # core: esp32:esp32 + # board: esp32:esp32:esp32 + # index_url: https://espressif.github.io/arduino-esp32/package_esp32_dev_index.json - name: package_esp8266com_index.json core: esp8266:esp8266 board: esp8266:esp8266:huzzah From 928036c8711c7011a013eafadbd63f1b8dbff8d3 Mon Sep 17 00:00:00 2001 From: Mathieu Carbou Date: Fri, 22 Nov 2024 21:20:23 +0100 Subject: [PATCH 4/6] Add support for STL std::string in ESP-DASH with -D DASH_USE_STL_STRING=1 --- examples/Benchmark/Benchmark.ino | 2 +- platformio.ini | 1 + src/Card.cpp | 8 ++++---- src/Card.h | 23 +++++++++++++++++------ src/Chart.cpp | 4 ++-- src/Chart.h | 17 ++++++++++++++--- src/ESPDash.cpp | 6 +++--- src/ESPDash.h | 6 +++--- src/Statistic.cpp | 2 +- src/Statistic.h | 17 ++++++++++++++--- 10 files changed, 60 insertions(+), 26 deletions(-) diff --git a/examples/Benchmark/Benchmark.ino b/examples/Benchmark/Benchmark.ino index 48c8bb99..084f9e24 100644 --- a/examples/Benchmark/Benchmark.ino +++ b/examples/Benchmark/Benchmark.ino @@ -53,7 +53,7 @@ Card slider(&dashboard, SLIDER_CARD, "Test Slider", "", 0, 255); Chart bar(&dashboard, BAR_CHART, "Power Usage (kWh)"); // Bar Chart Data -String XAxis[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; +dash::string XAxis[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; int YAxis[] = {0, 0, 0, 0, 0, 0, 0}; diff --git a/platformio.ini b/platformio.ini index ba95bc4d..37e55b3c 100644 --- a/platformio.ini +++ b/platformio.ini @@ -14,6 +14,7 @@ build_flags = -D CONFIG_ARDUHAL_LOG_COLORS -D CORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG ; -D DASH_USE_LEGACY_CHART_STORAGE=1 + ; -D DASH_USE_STL_STRING=1 lib_deps = bblanchon/ArduinoJson@^7.1.0 mathieucarbou/ESPAsyncWebServer@^3.3.14 diff --git a/src/Card.cpp b/src/Card.cpp index 07b6cd2e..46194a97 100644 --- a/src/Card.cpp +++ b/src/Card.cpp @@ -99,11 +99,11 @@ void Card::update(float value){ _value_f = value; } -void Card::update(const String &value, const char* symbol){ +void Card::update(const dash::string &value, const char* symbol){ update(value.c_str(), symbol); } -void Card::update(const String &value){ +void Card::update(const dash::string &value){ update(value.c_str()); } @@ -130,7 +130,7 @@ void Card::update(const char* value){ _value_s = value; } -void Card::update(String&& value, const char* symbol){ +void Card::update(dash::string&& value, const char* symbol){ if(_value_type == Card::STRING){ if(_value_s != value) _changed = true; @@ -143,7 +143,7 @@ void Card::update(String&& value, const char* symbol){ _value_s = std::move(value); } -void Card::update(String&& value){ +void Card::update(dash::string&& value){ if(_value_type == Card::STRING){ if(_value_s != value) _changed = true; diff --git a/src/Card.h b/src/Card.h index 530bb694..6da4ddd1 100644 --- a/src/Card.h +++ b/src/Card.h @@ -8,6 +8,17 @@ #include "ESPDash.h" #include "ArduinoJson.h" +#ifdef DASH_USE_STL_STRING +#include +namespace dash { + using string = std::string; +} +#else +namespace dash { + using string = String; +} +#endif + struct CardNames { int value; const char* type; @@ -41,7 +52,7 @@ class Card { float _value_f; int _value_i; }; - String _value_s; + dash::string _value_s; union alignas(4) { float _value_min_f; int _value_min; @@ -54,7 +65,7 @@ class Card { float _value_step_f; int _value_step; }; - String _symbol; + dash::string _symbol; std::function _callback = nullptr; std::function _callback_f = nullptr; @@ -71,10 +82,10 @@ class Card { void update(float value, const char* symbol); void update(const char* value); void update(const char* value, const char* symbol); - void update(const String &value); - void update(const String &value, const char* symbol); - void update(String &&value); - void update(String &&value, const char* symbol); + void update(const dash::string &value); + void update(const dash::string &value, const char* symbol); + void update(dash::string &&value); + void update(dash::string &&value, const char* symbol); ~Card(); friend class ESPDash; diff --git a/src/Chart.cpp b/src/Chart.cpp index fadb6610..a45466bf 100644 --- a/src/Chart.cpp +++ b/src/Chart.cpp @@ -76,7 +76,7 @@ void Chart::updateX(float arr_x[], size_t x_size){ _x_changed = true; } -void Chart::updateX(String arr_x[], size_t x_size){ +void Chart::updateX(dash::string arr_x[], size_t x_size){ _x_axis_type = GraphAxisType::STRING; #if DASH_USE_LEGACY_CHART_STORAGE == 1 emptyXAxisVectors(); @@ -96,7 +96,7 @@ void Chart::updateX(const char* arr_x[], size_t x_size){ #if DASH_USE_LEGACY_CHART_STORAGE == 1 emptyXAxisVectors(); for(int i=0; i < x_size; i++){ - _x_axis_s.push_back(String(arr_x[i])); + _x_axis_s.push_back(arr_x[i]); } #else clearXAxisPointers(); diff --git a/src/Chart.h b/src/Chart.h index 55a44022..94df80b4 100644 --- a/src/Chart.h +++ b/src/Chart.h @@ -15,6 +15,17 @@ #include #endif +#ifdef DASH_USE_STL_STRING +#include +namespace dash { + using string = std::string; +} +#else +namespace dash { + using string = String; +} +#endif + // Default to Line Chart enum { BAR_CHART, @@ -47,7 +58,7 @@ class Chart { /* X-Axis */ std::vector _x_axis_i; std::vector _x_axis_f; - std::vector _x_axis_s; + std::vector _x_axis_s; /* Y-Axis */ std::vector _y_axis_i; std::vector _y_axis_f; @@ -59,7 +70,7 @@ class Chart { int *_x_axis_i_ptr = nullptr; float *_x_axis_f_ptr = nullptr; const char **_x_axis_char_ptr = nullptr; - String *_x_axis_s_ptr = nullptr; + dash::string *_x_axis_s_ptr = nullptr; unsigned int _x_axis_ptr_size = 0; /* Y-Axis */ int *_y_axis_i_ptr = nullptr; @@ -74,7 +85,7 @@ class Chart { Chart(ESPDash *dashboard, const int type, const char* name); void updateX(int arr_x[], size_t x_size); void updateX(float arr_x[], size_t x_size); - void updateX(String arr_x[], size_t x_size); + void updateX(dash::string arr_x[], size_t x_size); void updateX(const char* arr_x[], size_t x_size); void updateY(int arr_y[], size_t y_size); void updateY(float arr_y[], size_t y_size); diff --git a/src/ESPDash.cpp b/src/ESPDash.cpp index 7ea478a5..f7eab206 100644 --- a/src/ESPDash.cpp +++ b/src/ESPDash.cpp @@ -119,7 +119,7 @@ void ESPDash::setAuthentication(const char *user, const char *pass) { } } -void ESPDash::setAuthentication(const String &user, const String &pass) { +void ESPDash::setAuthentication(const dash::string &user, const dash::string &pass) { setAuthentication(user.c_str(), pass.c_str()); } @@ -371,7 +371,7 @@ void ESPDash::generateComponentJSON(JsonObject& doc, Card* card, bool change_onl } } } - if(change_only || !card->_symbol.isEmpty()) + if(change_only || card->_symbol.length()) doc["s"] = card->_symbol; switch (card->_value_type) { @@ -382,7 +382,7 @@ void ESPDash::generateComponentJSON(JsonObject& doc, Card* card, bool change_onl doc["v"] = String(card->_value_f, 2); break; case Card::STRING: - if(change_only || !card->_value_s.isEmpty()) { + if(change_only || card->_value_s.length()) { doc["v"] = card->_value_s; } break; diff --git a/src/ESPDash.h b/src/ESPDash.h index 80e116d5..02597b9a 100644 --- a/src/ESPDash.h +++ b/src/ESPDash.h @@ -117,8 +117,8 @@ class ESPDash{ std::vector statistics; bool default_stats_enabled = false; bool basic_auth = false; - String username; - String password; + dash::string username; + dash::string password; uint32_t _idCounter = 0; BeforeUpdateCallback _beforeUpdateCallback = nullptr; @@ -140,7 +140,7 @@ class ESPDash{ // Set Authentication void setAuthentication(const char* user, const char* pass); - void setAuthentication(const String &user, const String &pass); + void setAuthentication(const dash::string &user, const dash::string &pass); // Add Card void add(Card *card); diff --git a/src/Statistic.cpp b/src/Statistic.cpp index 71307a4a..64029d17 100644 --- a/src/Statistic.cpp +++ b/src/Statistic.cpp @@ -18,7 +18,7 @@ void Statistic::set(const char *value) { _value = value; } -void Statistic::set(String&& value) { +void Statistic::set(dash::string&& value) { // Safe copy _changed = _value != value; if(_changed) diff --git a/src/Statistic.h b/src/Statistic.h index 6efe148c..855d9867 100644 --- a/src/Statistic.h +++ b/src/Statistic.h @@ -7,6 +7,17 @@ #include "ESPDash.h" #include "ArduinoJson.h" +#ifdef DASH_USE_STL_STRING +#include +namespace dash { + using string = std::string; +} +#else +namespace dash { + using string = String; +} +#endif + // Forward Declaration class ESPDash; @@ -15,14 +26,14 @@ class Statistic { ESPDash *_dashboard; uint32_t _id; const char *_key; - String _value; + dash::string _value; bool _changed = false; public: Statistic(ESPDash *dashboard, const char *key, const char *value = ""); void set(const char *value); - void set(const String& value) { set(value.c_str()); } - void set(String&& value); + void set(const dash::string& value) { set(value.c_str()); } + void set(dash::string&& value); ~Statistic(); friend class ESPDash; From a29ca8f92ce3887c6ec3a1bd4d21b4805db0f1d5 Mon Sep 17 00:00:00 2001 From: Mathieu Carbou Date: Sun, 24 Nov 2024 14:46:08 +0100 Subject: [PATCH 5/6] Fix example to use macros --- examples/Benchmark/Benchmark.ino | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/Benchmark/Benchmark.ino b/examples/Benchmark/Benchmark.ino index 084f9e24..a43f340d 100644 --- a/examples/Benchmark/Benchmark.ino +++ b/examples/Benchmark/Benchmark.ino @@ -107,10 +107,10 @@ void loop() { generic.update((int)random(0, 100)); temp.update((int)random(0, 100)); hum.update((int)random(0, 100)); - status1.update("success"); - status2.update("warning"); - status3.update("danger"); - status4.update("idle"); + status1.update(DASH_STATUS_SUCCESS); + status2.update(DASH_STATUS_WARNING); + status3.update(DASH_STATUS_DANGER); + status4.update(DASH_STATUS_IDLE); progress.update((int)random(0, 100)); dashboard.sendUpdates(); From edd847ee0bc01f19a2bea93837c681acb75e8026 Mon Sep 17 00:00:00 2001 From: Mathieu Carbou Date: Fri, 29 Nov 2024 18:59:14 +0100 Subject: [PATCH 6/6] Dependency Upgrade --- .github/workflows/ci.yml | 4 ++-- library.json | 4 ++-- platformio.ini | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 61a00df3..6ed7e2a6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -64,10 +64,10 @@ jobs: run: ARDUINO_LIBRARY_ENABLE_UNSAFE_INSTALL=true arduino-cli lib install --git-url https://github.com/mathieucarbou/esphome-ESPAsyncTCP#v2.0.0 - name: Install ESPAsyncWebServer - run: ARDUINO_LIBRARY_ENABLE_UNSAFE_INSTALL=true arduino-cli lib install --git-url https://github.com/mathieucarbou/ESPAsyncWebServer#v3.3.14 + run: ARDUINO_LIBRARY_ENABLE_UNSAFE_INSTALL=true arduino-cli lib install --git-url https://github.com/mathieucarbou/ESPAsyncWebServer#v3.3.23 - name: Install ArduinoJson - run: ARDUINO_LIBRARY_ENABLE_UNSAFE_INSTALL=true arduino-cli lib install --git-url https://github.com/bblanchon/ArduinoJson#v7.1.0 + run: ARDUINO_LIBRARY_ENABLE_UNSAFE_INSTALL=true arduino-cli lib install --git-url https://github.com/bblanchon/ArduinoJson#v7.2.1 - name: Build AccessPoint run: arduino-cli compile --library . --warnings none -b ${{ matrix.board }} "examples/AccessPoint/AccessPoint.ino" diff --git a/library.json b/library.json index 2ae4065d..083e21bf 100644 --- a/library.json +++ b/library.json @@ -22,13 +22,13 @@ { "owner": "bblanchon", "name": "ArduinoJson", - "version": "^7.1.0", + "version": "^7.2.1", "platforms": ["espressif8266", "espressif32"] }, { "owner": "mathieucarbou", "name": "ESPAsyncWebServer", - "version": "^3.3.14", + "version": "^3.3.23", "platforms": ["espressif8266", "espressif32"] } ] diff --git a/platformio.ini b/platformio.ini index 37e55b3c..5f7a50f3 100644 --- a/platformio.ini +++ b/platformio.ini @@ -16,8 +16,8 @@ build_flags = ; -D DASH_USE_LEGACY_CHART_STORAGE=1 ; -D DASH_USE_STL_STRING=1 lib_deps = - bblanchon/ArduinoJson@^7.1.0 - mathieucarbou/ESPAsyncWebServer@^3.3.14 + bblanchon/ArduinoJson@^7.2.1 + mathieucarbou/ESPAsyncWebServer@^3.3.23 upload_protocol = esptool monitor_speed = 115200 monitor_filters = esp32_exception_decoder, log2file