From 587038de77fc679223d1bf9d640eeaa6fe4937d8 Mon Sep 17 00:00:00 2001 From: NP v/d Spek Date: Tue, 2 Apr 2024 17:08:33 +0200 Subject: [PATCH] add actions to the MAX7219Component --- esphome/components/max7219digit/display.py | 88 ++++++++++++++++++- .../components/max7219digit/max7219digit.h | 66 ++++++++++++++ 2 files changed, 153 insertions(+), 1 deletion(-) diff --git a/esphome/components/max7219digit/display.py b/esphome/components/max7219digit/display.py index 779e385ab1..5b137b0524 100644 --- a/esphome/components/max7219digit/display.py +++ b/esphome/components/max7219digit/display.py @@ -1,7 +1,14 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import display, spi -from esphome.const import CONF_ID, CONF_INTENSITY, CONF_LAMBDA, CONF_NUM_CHIPS +from esphome import automation +from esphome.const import ( + CONF_ID, + CONF_INTENSITY, + CONF_LAMBDA, + CONF_NUM_CHIPS, + CONF_STATE, +) CODEOWNERS = ["@rspaargaren"] DEPENDENCIES = ["spi"] @@ -17,6 +24,7 @@ CONF_REVERSE_ENABLE = "reverse_enable" CONF_NUM_CHIP_LINES = "num_chip_lines" CONF_CHIP_LINES_STYLE = "chip_lines_style" + integration_ns = cg.esphome_ns.namespace("max7219digit") ChipLinesStyle = integration_ns.enum("ChipLinesStyle") CHIP_LINES_STYLE = { @@ -99,3 +107,81 @@ async def to_code(config): config[CONF_LAMBDA], [(MAX7219ComponentRef, "it")], return_type=cg.void ) cg.add(var.set_writer(lambda_)) + + +DisplayInvertAction = max7219_ns.class_("DisplayInvertAction", automation.Action) +DisplayVisiblityAction = max7219_ns.class_("DisplayVisiblityAction", automation.Action) +DisplayReverseAction = max7219_ns.class_("DisplayReverseAction", automation.Action) +DisplayIntensityAction = max7219_ns.class_("DisplayIntensityAction", automation.Action) + + +MAX7219_OFF_ACTION_SCHEMA = automation.maybe_simple_id( + { + cv.Required(CONF_ID): cv.use_id(MAX7219Component), + cv.Optional(CONF_STATE, default=False): False, + } +) +MAX7219_ON_ACTION_SCHEMA = automation.maybe_simple_id( + MAX7219_OFF_ACTION_SCHEMA.extend( + { + cv.Optional(CONF_STATE, default=True): True, + } + ) +) + + +@automation.register_action( + "MAX7219.invert_off", DisplayInvertAction, MAX7219_OFF_ACTION_SCHEMA +) +@automation.register_action( + "MAX7219.inveert_on", DisplayInvertAction, MAX7219_ON_ACTION_SCHEMA +) +async def MAX7219_inveert_to_code(config, action_id, template_arg, args): + paren = await cg.get_variable(config[CONF_ID]) + var = cg.new_Pvariable(action_id, template_arg, paren) + template_ = await cg.templatable(config[CONF_STATE], args, bool) + cg.add(var.set_state(template_)) + + +@automation.register_action( + "MAX7219.turn_off", DisplayVisiblityAction, MAX7219_OFF_ACTION_SCHEMA +) +@automation.register_action( + "MAX7219.turn_on", DisplayVisiblityAction, MAX7219_ON_ACTION_SCHEMA +) +async def MAX7219_visible_to_code(config, action_id, template_arg, args): + paren = await cg.get_variable(config[CONF_ID]) + var = cg.new_Pvariable(action_id, template_arg, paren) + template_ = await cg.templatable(config[CONF_STATE], args, bool) + cg.add(var.set_state(template_)) + + +@automation.register_action( + "MAX7219.reverse_off", DisplayReverseAction, MAX7219_OFF_ACTION_SCHEMA +) +@automation.register_action( + "MAX7219.reverse_on", DisplayReverseAction, MAX7219_ON_ACTION_SCHEMA +) +async def MAX7219_reverse_to_code(config, action_id, template_arg, args): + paren = await cg.get_variable(config[CONF_ID]) + var = cg.new_Pvariable(action_id, template_arg, paren) + template_ = await cg.templatable(config[CONF_STATE], args, bool) + cg.add(var.set_state(template_)) + + +MAX7219_INTENSITY_SCHEMA = automation.maybe_simple_id( + { + cv.Required(CONF_ID): cv.use_id(MAX7219Component), + cv.Optional(CONF_INTENSITY, default=15): cv.int_range(min=0, max=15), + } +) + + +@automation.register_action( + "MAX7219.intensity", DisplayIntensityAction, MAX7219_INTENSITY_SCHEMA +) +async def MAX7219_intensity_to_code(config, action_id, template_arg, args): + paren = await cg.get_variable(config[CONF_ID]) + var = cg.new_Pvariable(action_id, template_arg, paren) + template_ = await cg.templatable(config[CONF_INTENSITY], args, cg.int8) + cg.add(var.set_state(template_)) diff --git a/esphome/components/max7219digit/max7219digit.h b/esphome/components/max7219digit/max7219digit.h index ead8033803..97fc1f6933 100644 --- a/esphome/components/max7219digit/max7219digit.h +++ b/esphome/components/max7219digit/max7219digit.h @@ -120,5 +120,71 @@ class MAX7219Component : public display::DisplayBuffer, optional writer_local_{}; }; + +/* +void invert_on_off(bool on_off); + void invert_on_off(); + + void turn_on_off(bool on_off); + + void draw_absolute_pixel_internal(int x, int y, Color color) override; + int get_height_internal() override; + int get_width_internal() override; + + void set_intensity(uint8_t intensity) { this->intensity_ = intensity; }; + void set_num_chips(uint8_t num_chips) { this->num_chips_ = num_chips; }; + void set_num_chip_lines(uint8_t num_chip_lines) { this->num_chip_lines_ = num_chip_lines; }; + void set_chip_lines_style(ChipLinesStyle chip_lines_style) { this->chip_lines_style_ = chip_lines_style; }; + void set_chip_orientation(uint8_t rotate) { this->orientation_ = rotate; }; + void set_scroll_speed(uint16_t speed) { this->scroll_speed_ = speed; }; + void set_scroll_dwell(uint16_t dwell) { this->scroll_dwell_ = dwell; }; + void set_scroll_delay(uint16_t delay) { this->scroll_delay_ = delay; }; + void set_scroll(bool on_off) { this->scroll_ = on_off; }; + void set_reverse(bool on_off) { this->reverse_ = on_off; }; + void set_flip_x(bool flip_x) { this->flip_x_ = flip_x; }; + void scroll_left(); + +*/ + +template class DisplayInvertAction : public Action { + public: + DisplayInvertAction(MAX7219Component *buffer) : buffer_(buffer) {} + TEMPLATABLE_VALUE(bool, state) + + void play(Ts... x) override { this->buffer_->invert_on_off(this->state_.optional_value(x...)); } + + MAX7219Component *buffer_; +}; + +template class DisplayVisiblityAction : public Action { + public: + DisplayVisiblityAction(MAX7219Component *buffer) : buffer_(buffer) {} + TEMPLATABLE_VALUE(bool, state) + + void play(Ts... x) override { this->buffer_->turn_on_off(this->state_.optional_value(x...)); } + + MAX7219Component *buffer_; +}; + +template class DisplayReverseAction : public Action { + public: + DisplayReverseAction(MAX7219Component *buffer) : buffer_(buffer) {} + TEMPLATABLE_VALUE(bool, state) + + void play(Ts... x) override { this->buffer_->set_reverse(this->state_.optional_value(x...)); } + + MAX7219Component *buffer_; +}; + +template class DisplayIntensityAction : public Action { + public: + DisplayIntensityAction(MAX7219Component *buffer) : buffer_(buffer) {} + TEMPLATABLE_VALUE(uint8_t, state) + + void play(Ts... x) override { this->buffer_->intensity(this->state_.optional_value(x...)); } + + MAX7219Component *buffer_; +}; + } // namespace max7219digit } // namespace esphome