changed "effect update interval" to general "update interval", use it for "demo mode" as well

This commit is contained in:
endym 2024-07-01 14:50:25 +02:00
parent 0720c98fb6
commit 0141880577
4 changed files with 36 additions and 28 deletions

View file

@ -66,6 +66,7 @@ template<typename... Ts> class SetDemoModeAction : public Action<Ts...> {
explicit SetDemoModeAction(MAX6921Component *max9621) : max9621_(max9621) {}
TEMPLATABLE_VALUE(std::string, mode)
TEMPLATABLE_VALUE(uint32_t, demo_update_interval)
TEMPLATABLE_VALUE(uint8_t, demo_cycle_num)
// overlay to cover string inputs
@ -81,8 +82,11 @@ template<typename... Ts> class SetDemoModeAction : public Action<Ts...> {
// }
void play(Ts... x) override {
auto update_interval = this->demo_update_interval_.value(x...);
auto cycle_num = this->demo_cycle_num_.value(x...);
this->max9621_->display_->set_demo_mode(this->mode_.value(x...), cycle_num);
this->max9621_->display_->set_demo_mode(this->mode_.value(x...),
update_interval,
cycle_num);
}
protected:

View file

@ -345,12 +345,11 @@ void Display::update(void) {
}
}
void Display::set_demo_mode(demo_mode_t mode, uint8_t cycle_num) {
void Display::set_demo_mode(demo_mode_t mode, uint32_t interval, uint8_t cycle_num) {
uint text_idx, font_idx;
ESP_LOGD(TAG, "Set demo mode: mode=%i, cycle_num=%u", mode, cycle_num);
// this->display.demo_mode_cycle_num = cycle_num;
// this->display.demo_mode = mode;
ESP_LOGD(TAG, "Set demo mode: mode=%i, update-interval=%" PRIu32 "ms, cycle_num=%u",
mode, interval, cycle_num);
switch (mode) {
case DEMO_MODE_SCROLL_FONT:
@ -373,14 +372,16 @@ void Display::set_demo_mode(demo_mode_t mode, uint8_t cycle_num) {
set_mode(DISP_MODE_PRINT);
break;
}
if ((mode != DEMO_MODE_OFF) && (interval > 0))
set_update_interval(interval);
clear();
}
void Display::set_demo_mode(const std::string& mode, uint8_t cycle_num) {
void Display::set_demo_mode(const std::string& mode, uint32_t interval, uint8_t cycle_num) {
if (str_equals_case_insensitive(mode, "off")) {
this->set_demo_mode(DEMO_MODE_OFF, cycle_num);
this->set_demo_mode(DEMO_MODE_OFF, interval, cycle_num);
} else if (str_equals_case_insensitive(mode, "scroll_font")) {
this->set_demo_mode(DEMO_MODE_SCROLL_FONT, cycle_num);
this->set_demo_mode(DEMO_MODE_SCROLL_FONT, interval, cycle_num);
} else {
ESP_LOGW(TAG, "Invalid demo mode: %s", mode.c_str());
}
@ -402,8 +403,8 @@ void Display::set_demo_mode(const std::string& mode, uint8_t cycle_num) {
int Display::set_text(const std::string& text, uint8_t start_pos, const std::string& align,
uint32_t duration, const std::string& effect, uint32_t interval,
uint8_t cycle_num) {
ESP_LOGD(TAG, "Set text (given): text=%s, start-pos=%u, align=%s, duration=%" PRIu32 ", "\
"effect=%s, effect-update-interval=%" PRIu32 ", cycles=%u",
ESP_LOGD(TAG, "Set text (given): text=%s, start-pos=%u, align=%s, duration=%" PRIu32 "ms, "\
"effect=%s, effect-update-interval=%" PRIu32 "ms, cycles=%u",
text.c_str(), start_pos, align.c_str(), duration, effect.c_str(),
interval, cycle_num);

View file

@ -101,8 +101,8 @@ class Display : public DisplayBrightness,
bool isPointSegOnly(char c);
void restore_update_interval(void);
void setup(std::vector<uint8_t>& seg_to_out_map, std::vector<uint8_t>& pos_to_out_map);
void set_demo_mode(demo_mode_t mode, uint8_t cycle_num);
void set_demo_mode(const std::string& mode, uint8_t cycle_num);
void set_demo_mode(demo_mode_t mode, uint32_t interval, uint8_t cycle_num);
void set_demo_mode(const std::string& mode, uint32_t interval, uint8_t cycle_num);
int set_text(const char *text, uint8_t start_pos);
int set_text(const std::string& text, uint8_t start_pos, const std::string& align,
uint32_t duration, const std::string& effect, uint32_t interval, uint8_t cycle_num);

View file

@ -10,6 +10,7 @@ from esphome.const import (
CONF_POSITION,
CONF_EFFECT,
CONF_DURATION,
CONF_UPDATE_INTERVAL,
)
@ -45,7 +46,6 @@ CONF_POS_12_PIN = "pos_12_pin"
CONF_TEXT = "text"
CONF_ALIGN = "align"
CONF_CYCLE_NUM = "cycle_num"
CONF_EFFECT_UPDATE_INTERVAL = "effect_update_interval"
max6921_ns = cg.esphome_ns.namespace("max6921")
@ -209,14 +209,13 @@ async def max6921_set_brightness_to_code(config, action_id, template_arg, args):
def validate_action_set_text(value):
duration = value[CONF_DURATION]
cycle_num = value.get(CONF_CYCLE_NUM)
if isinstance(cycle_num, cv.Lambda):
cycle_num = 1
if duration.total_milliseconds > 0 and cycle_num > 0:
raise cv.Invalid(
f"Only one of following config value must be set: {CONF_CYCLE_NUM}, {CONF_DURATION}"
)
# duration = value.get(CONF_DURATION)
# cycle_num = value.get(CONF_CYCLE_NUM)
# if not isinstance(duration, cv.Lambda) and not isinstance(cycle_num, cv.Lambda):
# if duration.total_milliseconds > 0 and cycle_num > 0:
# raise cv.Invalid(
# f"Only one of following config value must be set: {CONF_CYCLE_NUM}, {CONF_DURATION}"
# )
return value
@ -236,9 +235,9 @@ ACTION_SET_TEXT_SCHEMA = cv.All(
cv.positive_time_period_milliseconds
),
cv.Optional(CONF_EFFECT, default="none"): cv.templatable(cv.string),
cv.Optional(
CONF_EFFECT_UPDATE_INTERVAL, default="150ms"
): cv.templatable(cv.positive_time_period_milliseconds),
cv.Optional(CONF_UPDATE_INTERVAL, default="150ms"): cv.templatable(
cv.positive_time_period_milliseconds
),
cv.Optional(CONF_CYCLE_NUM, default=0): cv.templatable(cv.uint8_t),
}
)
@ -266,10 +265,8 @@ async def max6921_set_text_to_code(config, action_id, template_arg, args):
if CONF_EFFECT in config:
template_ = await cg.templatable(config[CONF_EFFECT], args, cg.std_string)
cg.add(var.set_text_effect(template_))
if CONF_EFFECT_UPDATE_INTERVAL in config:
template_ = await cg.templatable(
config[CONF_EFFECT_UPDATE_INTERVAL], args, cg.uint32
)
if CONF_UPDATE_INTERVAL in config:
template_ = await cg.templatable(config[CONF_UPDATE_INTERVAL], args, cg.uint32)
cg.add(var.set_text_effect_update_interval(template_))
if CONF_CYCLE_NUM in config:
template_ = await cg.templatable(config[CONF_CYCLE_NUM], args, cg.uint8)
@ -284,6 +281,9 @@ ACTION_SET_DEMO_MODE_SCHEMA = cv.All(
{
# cv.Required(CONF_MODE): cv.templatable(cv.enum(DEMO_MODES, lower=True)),
cv.Required(CONF_MODE): cv.templatable(cv.string),
cv.Optional(CONF_UPDATE_INTERVAL, default="150ms"): cv.templatable(
cv.positive_time_period_milliseconds
),
cv.Optional(CONF_CYCLE_NUM, default=0): cv.templatable(cv.uint8_t),
}
)
@ -301,6 +301,9 @@ async def max6921_set_demo_mode_to_code(config, action_id, template_arg, args):
# template_ = await cg.templatable(config[CONF_MODE], args, DemoMode)
template_ = await cg.templatable(config[CONF_MODE], args, cg.std_string)
cg.add(var.set_mode(template_))
if CONF_UPDATE_INTERVAL in config:
template_ = await cg.templatable(config[CONF_UPDATE_INTERVAL], args, cg.uint32)
cg.add(var.set_demo_update_interval(template_))
if CONF_CYCLE_NUM in config:
template_ = await cg.templatable(config[CONF_CYCLE_NUM], args, cg.uint8)
cg.add(var.set_demo_cycle_num(template_))