Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Commit

Permalink
introduced actions
Browse files Browse the repository at this point in the history
  • Loading branch information
lubeda committed Feb 11, 2022
1 parent 180ebf4 commit a982943
Show file tree
Hide file tree
Showing 5 changed files with 286 additions and 43 deletions.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,48 @@ ehmtx:
iconname: !lambda "return x.c_str();"
text: !lambda "return y.c_str();"
```
### Actions

For local automations you can use actions. This is the normal way of automations. The ```id(rgb8x32)->``` style will also work.

#### Indicator on

You have to use use id of your ehmtx component

```
- ehmtx.indicator.on:
id: rgb8x32
red: !lambda return r;
green: !lambda return g;
blue: !lambda return b;
```

- ```red, green, blue```: the color components (0..255) (default=80)

#### Indicator off

```
- ehmtx.indicator.off:
id: rgb8x32
```

#### add screen to loop

```
- ehmtx.add.screen:
id: rgb8x32
text: !lambda return text;
icon_name: !lambda return icon_name;
duration: 7
alarm: false
```

Parameters:
**id (required, ID):** ID of the ehmtx component
**text (required, string):** the text to display
**icon_name (required, string):** the name of the icon to display
**duration (optional, int):** the lifetime of the screen in minutes (default=5)
**alarm (optional, bool):** if alarm set true (default=false)

## Integration in homeassistant

Expand Down
46 changes: 30 additions & 16 deletions components/ehmtx/EHMTX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace esphome
void EHMTX::set_indicator_color(int r, int g, int b)
{
this->indicator_color = Color((uint8_t)r & 248, (uint8_t)g & 252, (uint8_t)b & 248);
ESP_LOGD("EHMTX", "Indicator r: %d g: %d b: %d", r, g, b);
}

uint8_t EHMTX::find_icon(std::string name)
Expand Down Expand Up @@ -44,11 +45,13 @@ namespace esphome

void EHMTX::set_indicator_off()
{
this->showindicator = false;
this->show_indicator = false;
ESP_LOGD("EHMTX", "Indicator off");
}
void EHMTX::set_indicator_on()
{
this->showindicator = true;
this->show_indicator = true;
ESP_LOGD("EHMTX", "Indicator on");
}

void EHMTX::draw_clock()
Expand Down Expand Up @@ -97,7 +100,7 @@ namespace esphome
bool has_next_screen = this->store->move_next();
if (has_next_screen)
{
this->show_screen = true;
this->show_screen = true;
}
}
if (this->show_screen == false)
Expand All @@ -108,8 +111,9 @@ namespace esphome
else
{
this->next_action_time = ts + (int)this->store->current()->display_duration;
for (auto *t : on_next_screen_triggers_) {
t->process(this->iconnames[this->store->current()->icon],this->store->current()->text);
for (auto *t : on_next_screen_triggers_)
{
t->process(this->iconnames[this->store->current()->icon], this->store->current()->text);
}
}
}
Expand All @@ -132,6 +136,15 @@ namespace esphome
this->clock->now().month, this->clock->now().year,
this->clock->now().hour, this->clock->now().minute);
ESP_LOGI(TAG, "status brightness: %d (0..255)", this->brightness_);
if (this->show_indicator)
{
ESP_LOGD("EHMTX", "Indicator on");
}
else
{
ESP_LOGD("EHMTX", "Indicator off");
}

this->store->log_status();

for (uint8_t i = 0; i < this->icon_count; i++)
Expand Down Expand Up @@ -169,7 +182,7 @@ namespace esphome
void EHMTX::add_alarm(uint8_t icon, std::string text)
{
int x, y, w, h;
EHMTX_screen* screen = this->store->find_free_screen(icon);
EHMTX_screen *screen = this->store->find_free_screen(icon);
this->display->get_text_bounds(0, 0, text.c_str(), this->font, display::TextAlign::LEFT, &x, &y, &w, &h);
if (icon >= this->icon_count)
{
Expand All @@ -184,7 +197,7 @@ namespace esphome
void EHMTX::add_screen(uint8_t icon, std::string text)
{
int x, y, w, h;
EHMTX_screen* screen = this->store->find_free_screen(icon);
EHMTX_screen *screen = this->store->find_free_screen(icon);
this->display->get_text_bounds(0, 0, text.c_str(), this->font, display::TextAlign::LEFT, &x, &y, &w, &h);
if (icon >= this->icon_count)
{
Expand Down Expand Up @@ -216,7 +229,7 @@ namespace esphome
icon = 0;
}

EHMTX_screen* screen = this->store->find_free_screen(icon);
EHMTX_screen *screen = this->store->find_free_screen(icon);
this->display->get_text_bounds(0, 0, text.c_str(), this->font, display::TextAlign::LEFT, &x, &y, &w, &h);
screen->set_text(text, icon, w, this->duration);
screen->alarm = alarm;
Expand All @@ -232,16 +245,16 @@ namespace esphome
icon = 0;
}

EHMTX_screen* screen = this->store->find_free_screen(icon);
EHMTX_screen *screen = this->store->find_free_screen(icon);
this->display->get_text_bounds(0, 0, text.c_str(), this->font, display::TextAlign::LEFT, &x, &y, &w, &h);
screen->set_text(text, icon, w, this->duration);
ESP_LOGD(TAG, "add_screen_n icon: %d iconname: %s text: %s", icon, iname.c_str(),text.c_str());
ESP_LOGD(TAG, "add_screen_n icon: %d iconname: %s text: %s", icon, iname.c_str(), text.c_str());
}

void EHMTX::add_screen_t(uint8_t icon, std::string text, uint16_t t)
{
int x, y, w, h;
EHMTX_screen* screen = this->store->find_free_screen(icon);
EHMTX_screen *screen = this->store->find_free_screen(icon);
this->display->get_text_bounds(0, 0, text.c_str(), this->font, display::TextAlign::LEFT, &x, &y, &w, &h);
if (icon >= this->icon_count)
{
Expand Down Expand Up @@ -317,7 +330,7 @@ namespace esphome
{
this->draw_clock();
}
if (this->showindicator)
if (this->show_indicator)
{
this->display->line(31, 5, 29, 7, this->indicator_color);
this->display->draw_pixel_at(30, 7, this->indicator_color);
Expand All @@ -326,10 +339,11 @@ namespace esphome
}
}

/* Trigger */
/* Trigger */

void EHMTXNextScreenTrigger::process(std::string iconname,std::string text) {
this->trigger(iconname,text);
}
void EHMTXNextScreenTrigger::process(std::string iconname, std::string text)
{
this->trigger(iconname, text);
}

}
65 changes: 63 additions & 2 deletions components/ehmtx/EHMTX.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace esphome
Color text_color, alarm_color;
void dump_config();
bool show_screen;
bool showindicator;
bool show_indicator;
display::Animation *icons[MAXICONS];
const char *iconnames[MAXICONS];
void add_icon(display::Animation *icon,const char *name);
Expand All @@ -41,7 +41,7 @@ namespace esphome
display::Font *font;
int8_t yoffset,xoffset;
uint8_t find_icon(std::string name);
int8_t duration; // in minutes how long is a screen valid
uint8_t duration; // in minutes how long is a screen valid
uint16_t scroll_intervall; // ms to between scrollsteps
uint16_t anim_intervall; // ms to next_frame()
uint16_t clock_time; // ms display of clock/date 0.5 clock then 0.5 date
Expand Down Expand Up @@ -138,6 +138,67 @@ class EHMTXNextScreenTrigger : public Trigger<std::string,std::string> {
};


template<typename... Ts> class AddScreenAction : public Action<Ts...> {
public:
AddScreenAction(EHMTX *parent) : parent_(parent) {}
TEMPLATABLE_VALUE(std::string, icon)
TEMPLATABLE_VALUE(std::string, text)
TEMPLATABLE_VALUE(uint8_t, duration)
TEMPLATABLE_VALUE(bool, alarm)

void play(Ts... x) override {
this->parent_->add_screen_u(this->icon_.value(x...), this->text_.value(x...), this->duration_.value(x...),
this->alarm_.value(x...));
}

protected:
EHMTX *parent_;
};


template<typename... Ts> class SetIndicatorOn : public Action<Ts...> {
public:
SetIndicatorOn(EHMTX *parent) : parent_(parent) {}
TEMPLATABLE_VALUE(uint8_t, red)
TEMPLATABLE_VALUE(uint8_t, green)
TEMPLATABLE_VALUE(uint8_t, blue)

void play(Ts... x) override {
this->parent_->set_indicator_on();
this->parent_->set_indicator_color(this->red_.value(x...), this->green_.value(x...), this->blue_.value(x...));
}

protected:
EHMTX *parent_;
};

template<typename... Ts> class SetIndicatorOff : public Action<Ts...> {
public:
SetIndicatorOff(EHMTX *parent) : parent_(parent) {}

void play(Ts... x) override {
this->parent_->set_indicator_off();
}

protected:
EHMTX *parent_;
};


template<typename... Ts> class DeleteScreen : public Action<Ts...> {
public:
DeleteScreen(EHMTX *parent) : parent_(parent) {}
TEMPLATABLE_VALUE(std::string, icon)

void play(Ts... x) override {
this->parent_->del_screen(this->parent_->find_icon(this->icon_.value(x...)));
}

protected:
EHMTX *parent_;
};


}

#endif
Loading

0 comments on commit a982943

Please sign in to comment.