Skip to content

Commit

Permalink
Merge pull request #237 from andrewjswan/2024.6.1-Add_multicolor_text…
Browse files Browse the repository at this point in the history
…_support

2024.6.1: Add `multicolor text` support
  • Loading branch information
lubeda authored Jun 8, 2024
2 parents 115d1b9 + a2cab15 commit 86b82ed
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 12 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,29 @@ Example:

**rainbow_shimmer** (optional, boolean): If true, enables color shimmer when displaying text in rainbow modes.

**multicolor_text** (optional, boolean): If true, enables text multi color support when displaying text.

Example:
```Yaml
ehmtxv2:
id: rgb8x32
...
multicolor_text: true
```

```Yaml
service: esphome.ulanzi_text_screen
data:
default_font: true
text: "Test Test #00FF00Test #FF0000Test #0000FFTest"
lifetime: 2
screen_time: 10
r: 255
g: 255
b: 255
```
Shows text in different colors, `Test Test` in the default color `#FFFFFF` (r: 255, g:255, b: 255), followed by `Test` in green `#00FF00`, then `Test` in red `#FF0000` and finally `Test` in blue `#0000FF`.

**icons2html** (optional, boolean): If true, generate the HTML-file (*filename*.html) to show all included icons. (default = `false`)

**iconscache** (optional, boolean): If true, it caches icons in the `.cache\icons` folder and if it finds the specified icons in the cache, it uses them instead of trying to download them again from the Internet. (default = `false`)
Expand Down
67 changes: 66 additions & 1 deletion components/ehmtxv2/EHMTX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2591,7 +2591,7 @@ namespace esphome
}
}

uint8_t x = xpos - full_length / 2;
int x = xpos - full_length / 2;
for (int i = 0; i < parts.size(); i++)
{
if (parts.at(i).length() > 0)
Expand Down Expand Up @@ -2789,6 +2789,71 @@ namespace esphome
}
#endif

void EHMTX::draw_text(std::string text, esphome::display::BaseFont *font, Color color, int xpos, int ypos)
{
#ifdef EHMTXv2_MULTICOLOR_TEXT
std::size_t pos = text.find("#");
if (pos == std::string::npos)
{
this->display->print(xpos, ypos, font, color, esphome::display::TextAlign::BASELINE_LEFT, text.c_str());
return;
}

std::regex regex ("(#[A-Fa-f0-9]{6})|(.+?)");

std::regex_iterator<std::string::iterator> next ( text.begin(), text.end(), regex );
std::regex_iterator<std::string::iterator> end;

std::vector<std::string> res;

std::string iter = "";
while (next != end)
{
std::string part = next->str();
if (part.length() == 7)
{
if (iter.length() > 0)
{
res.push_back (iter);
iter = "";
}
res.push_back (part);
}
else
{
iter += part;
}
next++;
}
if (iter.length() > 0)
{
res.push_back (iter);
}

Color c = color;
int x = xpos;
std::regex is_color ("^#[A-Fa-f0-9]{6}$");
for (int i = 0; i < res.size(); i++)
{
if (res.at(i).length() > 0)
{
int r, g, b;
if (res.at(i).length() == 7 && std::regex_match(res.at(i), is_color) && sscanf(&res.at(i).c_str()[1], "%02x%02x%02x", &r, &g, &b))
{
c = Color(r, g ,b);
}
else
{
this->display->print(x, ypos, font, c, esphome::display::TextAlign::BASELINE_LEFT, res.at(i).c_str());
x += this->GetTextWidth(font, "%s", res.at(i).c_str());
}
}
}
#else
this->display->print(xpos, ypos, font, color, esphome::display::TextAlign::BASELINE_LEFT, text.c_str());
#endif
}

#ifdef EHMTXv2_RAINBOW_SHIMMER
void EHMTX::draw_rainbow_text(std::string text, esphome::display::BaseFont *font, int xpos, int ypos)
{
Expand Down
1 change: 1 addition & 0 deletions components/ehmtxv2/EHMTX.h
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ namespace esphome
#ifdef EHMTXv2_RAINBOW_SHIMMER
void draw_rainbow_text(std::string text, esphome::display::BaseFont *font, int xpos, int ypos);
#endif
void draw_text(std::string text, esphome::display::BaseFont *font, Color color, int xpos, int ypos);

void set_replace_time_date_active(bool b=false);
void set_weekday_char_count(uint8_t i);
Expand Down
26 changes: 15 additions & 11 deletions components/ehmtxv2/EHMTX_queue.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include "esphome.h"
#ifdef EHMTXv2_MULTICOLOR_TEXT
#include <regex>
#endif

namespace esphome
{
Expand Down Expand Up @@ -526,8 +529,7 @@ namespace esphome
this->config_->draw_rainbow_text(this->text, font, this->xpos() + xoffset, this->ypos() + yoffset);
else
#endif
this->config_->display->print(this->xpos() + xoffset, this->ypos() + yoffset, font, color_, esphome::display::TextAlign::BASELINE_LEFT,
this->text.c_str());
this->config_->draw_text(this->text, font, color_, this->xpos() + xoffset, this->ypos() + yoffset);
#endif
if (this->sbitmap != NULL)
{
Expand Down Expand Up @@ -895,8 +897,7 @@ namespace esphome
this->config_->draw_rainbow_text(this->text, font, this->xpos() + xoffset, this->ypos() + yoffset);
else
#endif
this->config_->display->print(this->xpos() + xoffset, this->ypos() + yoffset, font, color_, esphome::display::TextAlign::BASELINE_LEFT,
this->text.c_str());
this->config_->draw_text(this->text, font, color_, this->xpos() + xoffset, this->ypos() + yoffset);
#endif
if (this->mode == MODE_ICON_PROGRESS)
{
Expand Down Expand Up @@ -1043,8 +1044,7 @@ namespace esphome
this->config_->draw_rainbow_text(this->text, font, this->xpos() + xoffset, this->ypos() + yoffset);
else
#endif
this->config_->display->print(this->xpos() + xoffset, this->ypos() + yoffset, font, color_, esphome::display::TextAlign::BASELINE_LEFT,
this->text.c_str());
this->config_->draw_text(this->text, font, color_, this->xpos() + xoffset, this->ypos() + yoffset);
#endif
if (this->icon != BLANKICON)
{
Expand Down Expand Up @@ -1092,8 +1092,7 @@ namespace esphome
this->config_->draw_rainbow_text(this->text, font, this->xpos() + xoffset, this->ypos() + yoffset);
else
#endif
this->config_->display->print(this->xpos() + xoffset, this->ypos() + yoffset, font, color_, esphome::display::TextAlign::BASELINE_LEFT,
this->text.c_str());
this->config_->draw_text(this->text, font, color_, this->xpos() + xoffset, this->ypos() + yoffset);
#endif
break;

Expand Down Expand Up @@ -1226,13 +1225,19 @@ namespace esphome
uint8_t startx = 0;
uint16_t max_steps = 0;

std::string text_ = text;
#ifdef EHMTXv2_MULTICOLOR_TEXT
std::regex color_re("(#[A-Fa-f0-9]{6})");
text_ = std::regex_replace(text, color_re, "");
#endif

if (this->default_font)
{
this->config_->display->get_text_bounds(0, 0, text.c_str(), this->config_->default_font, display::TextAlign::LEFT, &x, &y, &w, &h);
this->config_->display->get_text_bounds(0, 0, text_.c_str(), this->config_->default_font, display::TextAlign::LEFT, &x, &y, &w, &h);
}
else
{
this->config_->display->get_text_bounds(0, 0, text.c_str(), this->config_->special_font, display::TextAlign::LEFT, &x, &y, &w, &h);
this->config_->display->get_text_bounds(0, 0, text_.c_str(), this->config_->special_font, display::TextAlign::LEFT, &x, &y, &w, &h);
}

this->pixels_ = w;
Expand Down Expand Up @@ -1296,7 +1301,6 @@ namespace esphome
}

this->scroll_reset = (width - startx) + this->pixels_;
;

ESP_LOGD(TAG, "calc_scroll_time: mode: %d text: \"%s\" pixels %d calculated: %.1f defined: %d max_steps: %d", this->mode, text.c_str(), this->pixels_, this->screen_time_ / 1000.0, screen_time, this->scroll_reset);
}
Expand Down
7 changes: 7 additions & 0 deletions components/ehmtxv2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def rgb565_888(v565):
CONF_BLENDSTEPS = "blend_steps"
CONF_RAINBOWINTERVAL = "rainbow_interval"
CONF_RAINBOWSHIMMER = "rainbow_shimmer"
CONF_MULTICOLOR_TEXT = "multicolor_text"
CONF_FRAMEINTERVAL = "frame_interval"
CONF_DEFAULT_FONT_ID = "default_font_id"
CONF_DEFAULT_FONT = "default_font"
Expand Down Expand Up @@ -242,6 +243,8 @@ def rgb565_888(v565):
): cv.templatable(cv.positive_int),
cv.Optional(CONF_RAINBOWSHIMMER, default=False
): cv.boolean,
cv.Optional(CONF_MULTICOLOR_TEXT, default=False
): cv.boolean,
cv.Optional(CONF_SCROLLCOUNT, default="2"
): cv.templatable(cv.positive_int),
cv.Optional(
Expand Down Expand Up @@ -600,6 +603,10 @@ def thumbnails(frames):
cg.add_define("EHMTXv2_RAINBOW_SHIMMER")
logging.info(f"[X] Rainbow shimmer")

if config[CONF_MULTICOLOR_TEXT]:
cg.add_define("EHMTXv2_MULTICOLOR_TEXT")
logging.info(f"[X] Multi color text")

if config[CONF_SCROLL_SMALL_TEXT]:
cg.add_define("EHMTXv2_SCROLL_SMALL_TEXT")

Expand Down

0 comments on commit 86b82ed

Please sign in to comment.