Skip to content

Commit

Permalink
Add support for STL std::string in ESP-DASH with -D DASH_USE_STL_STRI…
Browse files Browse the repository at this point in the history
…NG=1
  • Loading branch information
mathieucarbou committed Nov 22, 2024
1 parent 87c67ef commit 6e4ea68
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 26 deletions.
2 changes: 1 addition & 1 deletion 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
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/Card.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand All @@ -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;
Expand All @@ -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;
Expand Down
23 changes: 17 additions & 6 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,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;
Expand Down
4 changes: 2 additions & 2 deletions src/Chart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
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.push_back(String(arr_x[i]));
_x_axis_s.push_back(arr_x[i]);
}
#else
clearXAxisPointers();
Expand Down
17 changes: 14 additions & 3 deletions src/Chart.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@
#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 @@ -47,7 +58,7 @@ class Chart {
/* X-Axis */
std::vector<int> _x_axis_i;
std::vector<float> _x_axis_f;
std::vector<String> _x_axis_s;
std::vector<dash::string> _x_axis_s;
/* Y-Axis */
std::vector<int> _y_axis_i;
std::vector<float> _y_axis_f;
Expand All @@ -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;
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/ESPDash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/ESPDash.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ class ESPDash{
std::vector<Statistic*> 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;

Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Statistic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 14 additions & 3 deletions src/Statistic.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,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

// Forward Declaration
class ESPDash;

Expand All @@ -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;
Expand Down

0 comments on commit 6e4ea68

Please sign in to comment.