Skip to content

Commit

Permalink
Merge branch '2024.6.pre' of https://github.com/lubeda/EspHoMaTriXv2
Browse files Browse the repository at this point in the history
…into 2024.6.pre
  • Loading branch information
lubeda committed Jun 8, 2024
2 parents 1b17d89 + 86b82ed commit d9925a6
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 14 deletions.
25 changes: 25 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 Expand Up @@ -908,6 +931,8 @@ ehmtxv2:

**flip_flop_clock** (optional, boolean): Enables or disables flip_flop_clock clock mode. (default: false), works only in **advanced_clock** mode.

**flip_flop_speed** (optional, int): Set flip_flop_clock speed. (default: 2, range 1..10), works only in **advanced_clock** mode.

**advanced_bitmap** (optional, boolean): Enables or disables advanced bitmap mode. (default: false) [More info](#advanced-bitmap-mode)

**default_font_yoffset** (optional, pixel): yoffset the text is aligned BASELINE_LEFT, the baseline defaults to `6`
Expand Down
71 changes: 68 additions & 3 deletions 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 All @@ -2608,7 +2608,7 @@ namespace esphome

#ifdef EHMTXv2_FLIP_FLOP
bool step = false;
uint8_t y = ystep / 2;
uint8_t y = ystep / EHMTXv2_FLIP_FLOP_SPEED;

if (i == 0) // Hours
{
Expand Down Expand Up @@ -2708,7 +2708,7 @@ namespace esphome
if (step)
{
ystep++;
if (ystep == 16)
if (ystep == 8 * EHMTXv2_FLIP_FLOP_SPEED)
{
hours = h;
minutes = m;
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
14 changes: 14 additions & 0 deletions components/ehmtxv2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def rgb565_888(v565):
CONF_VERTICAL = "vertical_scroll"
CONF_CLOCK = "advanced_clock"
CONF_FLIP_FLOP = "flip_flop_clock"
CONF_FLIP_FLOP_SPEED = "flip_flop_speed"
CONF_BITMAP = "advanced_bitmap"
CONF_BOOTLOGO = "boot_logo"
CONF_BOOTLOGOMODE = "boot_logo_mode"
Expand All @@ -109,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 @@ -173,6 +175,9 @@ def rgb565_888(v565):
cv.Optional(
CONF_FLIP_FLOP, default=False
): cv.boolean,
cv.Optional(
CONF_FLIP_FLOP_SPEED, default="2"
): cv.templatable(cv.int_range(min=1, max=10)),
cv.Optional(
CONF_BITMAP, default=False
): cv.boolean,
Expand Down Expand Up @@ -238,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 @@ -596,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 All @@ -616,6 +627,9 @@ def thumbnails(frames):
if config[CONF_CLOCK] and config[CONF_FLIP_FLOP]:
cg.add_define("EHMTXv2_FLIP_FLOP")
logging.info(f"[X] Flip Flop clock mode")
if config[CONF_FLIP_FLOP_SPEED]:
cg.add_define("EHMTXv2_FLIP_FLOP_SPEED", config[CONF_FLIP_FLOP_SPEED])
logging.info(f"[X] Flip Flop Speed " + str(config[CONF_FLIP_FLOP_SPEED]))

if config[CONF_BITMAP]:
cg.add_define("EHMTXv2_ADV_BITMAP")
Expand Down

0 comments on commit d9925a6

Please sign in to comment.