Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Several improvements: Support move semantic + remove vector in favour of std one + add support for std::string with -D DASH_USE_STL_STRING=1 #245

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -62,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"
Expand Down
10 changes: 5 additions & 5 deletions examples/Benchmark/Benchmark.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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};


Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions library.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
]
Expand Down
7 changes: 5 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ build_flags =
-Wall -Wextra
-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
bblanchon/ArduinoJson@^7.2.1
mathieucarbou/ESPAsyncWebServer@^3.3.23
upload_protocol = esptool
monitor_speed = 115200
monitor_filters = esp32_exception_decoder, log2file
Expand All @@ -27,6 +29,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
Expand Down
28 changes: 26 additions & 2 deletions src/Card.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "Card.h"

#include <utility>

/*
Constructor
*/
Expand Down Expand Up @@ -97,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());
}

Expand All @@ -128,6 +130,28 @@ void Card::update(const char* value){
_value_s = value;
}

void Card::update(dash::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(dash::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){
Expand Down
21 changes: 17 additions & 4 deletions src/Card.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@
#include "ESPDash.h"
#include "ArduinoJson.h"

#ifdef DASH_USE_STL_STRING
#include <string>
namespace dash {
using string = std::string;
}
#else
namespace dash {
using string = String;
}
#endif

struct CardNames {
int value;
const char* type;
Expand Down Expand Up @@ -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;
Expand All @@ -54,7 +65,7 @@ class Card {
float _value_step_f;
int _value_step;
};
String _symbol;
dash::string _symbol;
std::function<void(int value)> _callback = nullptr;
std::function<void(float value)> _callback_f = nullptr;

Expand All @@ -71,8 +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(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;
Expand Down
34 changes: 17 additions & 17 deletions src/Chart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -76,12 +76,12 @@ 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();
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();
Expand All @@ -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(arr_x[i]);
}
#else
clearXAxisPointers();
Expand All @@ -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();
Expand All @@ -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();
Expand Down
30 changes: 22 additions & 8 deletions src/Chart.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include <functional>
#include "Arduino.h"
#include "vector.h"

#include "ESPDash.h"
#include "ArduinoJson.h"
Expand All @@ -12,6 +11,21 @@
#define DASH_USE_LEGACY_CHART_STORAGE 0
#endif

#if DASH_USE_LEGACY_CHART_STORAGE == 1
#include <vector>
#endif

#ifdef DASH_USE_STL_STRING
#include <string>
namespace dash {
using string = std::string;
}
#else
namespace dash {
using string = String;
}
#endif

// Default to Line Chart
enum {
BAR_CHART,
Expand Down Expand Up @@ -42,12 +56,12 @@ class Chart {

#if DASH_USE_LEGACY_CHART_STORAGE == 1
/* X-Axis */
Vector<int> _x_axis_i;
Vector<float> _x_axis_f;
Vector<String> _x_axis_s;
std::vector<int> _x_axis_i;
std::vector<float> _x_axis_f;
std::vector<dash::string> _x_axis_s;
/* Y-Axis */
Vector<int> _y_axis_i;
Vector<float> _y_axis_f;
std::vector<int> _y_axis_i;
std::vector<float> _y_axis_f;

void emptyXAxisVectors();
void emptyYAxisVectors();
Expand All @@ -56,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;
Expand All @@ -71,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);
Expand Down
Loading