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 14, 2024
1 parent 87b3b37 commit 83292f5
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 14 deletions.
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_STR &value, const char* symbol){
update(value.c_str(), symbol);
}

void Card::update(const String &value){
void Card::update(const DASH_STR &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_STR&& 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_STR&& value){
if(_value_type == Card::STRING){
if(_value_s != value)
_changed = true;
Expand Down
17 changes: 12 additions & 5 deletions src/Card.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
#include "ESPDash.h"
#include "ArduinoJson.h"

#ifdef DASH_USE_STL_STRING
#include <string>
#define DASH_STR std::string
#else
#define DASH_STR String
#endif

struct CardNames {
int value;
const char* type;
Expand Down Expand Up @@ -41,7 +48,7 @@ class Card {
float _value_f;
int _value_i;
};
String _value_s;
DASH_STR _value_s;
union alignas(4) {
float _value_min_f;
int _value_min;
Expand Down Expand Up @@ -71,10 +78,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_STR &value);
void update(const DASH_STR &value, const char* symbol);
void update(DASH_STR &&value);
void update(DASH_STR &&value, const char* symbol);
~Card();

friend class ESPDash;
Expand Down
2 changes: 1 addition & 1 deletion src/ESPDash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
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_STR&& value) {
// Safe copy
_changed = _value != value;
if(_changed)
Expand Down
13 changes: 10 additions & 3 deletions src/Statistic.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
#include "ESPDash.h"
#include "ArduinoJson.h"

#ifdef DASH_USE_STL_STRING
#include <string>
#define DASH_STR std::string
#else
#define DASH_STR String
#endif

// Forward Declaration
class ESPDash;

Expand All @@ -15,14 +22,14 @@ class Statistic {
ESPDash *_dashboard;
uint32_t _id;
const char *_key;
String _value;
DASH_STR _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_STR& value) { set(value.c_str()); }
void set(DASH_STR&& value);
~Statistic();

friend class ESPDash;
Expand Down

0 comments on commit 83292f5

Please sign in to comment.