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 db80f53
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 10 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.size()) {
doc["v"] = card->_value_s;
}
break;
Expand Down
7 changes: 7 additions & 0 deletions src/ESPDash.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ Github URL: https://github.com/ayushsharma82/ESP-DASH
#define DASH_MAX_WS_CLIENTS DEFAULT_MAX_WS_CLIENTS
#endif

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

// Forward Declaration
class Card;
class Chart;
Expand Down

0 comments on commit db80f53

Please sign in to comment.