[max6921] remove brightness handling (should be handled by monochromatic light in yaml)

This commit is contained in:
endym 2024-09-27 18:45:45 +02:00
parent 9e99a9ae99
commit 67dcc8b4bb
8 changed files with 6 additions and 164 deletions

View file

@ -11,13 +11,6 @@ namespace max6921 {
class Max6921Display;
template<typename... Ts> class SetBrightnessAction : public Action<Ts...>, public Parented<MAX6921Component> {
public:
TEMPLATABLE_VALUE(float, brightness)
void play(Ts... x) override { this->parent_->set_brightness(this->brightness_.value(x...)); }
};
template<typename... Ts> class SetTextAction : public Action<Ts...> {
public:
explicit SetTextAction(MAX6921Component *max9621) : max9621_(max9621) {}
@ -48,18 +41,6 @@ template<typename... Ts> class SetTextAction : public Action<Ts...> {
MAX6921Component *max9621_;
};
#if 0
template<typename... Ts> class SetDemoModeAction : public Action<Ts...>, public Parented<MAX6921Component> {
public:
TEMPLATABLE_VALUE(DemoMode, mode)
TEMPLATABLE_VALUE(uint8_t, cycle_num)
void play(Ts... x) override {
this->parent_->set_demo_mode(this->mode_.value(x...), this->cycle_num_.optional_value(x...));
}
};
#endif
template<typename... Ts> class SetDemoModeAction : public Action<Ts...> {
public:
explicit SetDemoModeAction(MAX6921Component *max9621) : max9621_(max9621) {}
@ -68,18 +49,6 @@ template<typename... Ts> class SetDemoModeAction : public Action<Ts...> {
TEMPLATABLE_VALUE(uint32_t, demo_update_interval)
TEMPLATABLE_VALUE(uint8_t, demo_cycle_num)
// overlay to cover string inputs
// void set_mode(const std::string mode);
// void set_mode(const std::string mode) {
// if (str_equals_case_insensitive(mode, "off")) {
// this->set_mode(DEMO_MODE_OFF);
// } else if (str_equals_case_insensitive(mode, "scroll_font")) {
// this->set_mode(DEMO_MODE_SCROLL_FONT);
// } else {
// output log message (TAG, "Invalid demo mode %s", mode.c_str());
// }
// }
void play(Ts... x) override {
auto update_interval = this->demo_update_interval_.value(x...);
auto cycle_num = this->demo_cycle_num_.value(x...);
@ -87,9 +56,7 @@ template<typename... Ts> class SetDemoModeAction : public Action<Ts...> {
}
protected:
// TemplatableValue<const std::string, Ts...> mode{};
MAX6921Component *max9621_;
// DemoMode mode_;
};
} // namespace max6921

View file

@ -218,7 +218,6 @@ void Max6921Display::dump_config() {
for (uint i = 0; i < this->seg_to_out_map_.size(); i++) {
ESP_LOGCONFIG(TAG, " Display position %2u: OUT%u", i, this->pos_to_out_map_[i]);
}
ESP_LOGCONFIG(TAG, " Brightness: %.1f", get_brightness());
}
/**
@ -302,17 +301,6 @@ void Max6921Display::restore_update_interval() {
* @brief Updates the display.
*/
void Max6921Display::update() {
// handle display brightness...
if (this->brightness_cfg_changed_) {
uint32_t inverted_duty =
this->brightness_max_duty_ -
this->brightness_max_duty_ * this->brightness_cfg_value_; // calc duty for low-active BLANK pin
ESP_LOGD(TAG, "Change display brightness to %.1f (off-time duty=%u/%u)", brightness_cfg_value_, inverted_duty,
this->brightness_max_duty_);
ledcWrite(this->brightness_pwm_channel_, inverted_duty);
this->brightness_cfg_changed_ = false;
}
// handle text effects...
if ((this->mode != DISP_MODE_PRINT) && (this->disp_text_.effect != TEXT_EFFECT_NONE)) { // any effect enabled?
switch (this->disp_text_.effect) {
@ -587,7 +575,7 @@ int Max6921Display::update_out_buf_() {
ESP_LOGW(TAG, "Encountered unsupported character (0x%02x): %c", pos_char, (pos_char >= 0x20) ? pos_char : ' ');
out_data = SEG_UNSUPPORTED_CHAR;
}
ESP_LOGVV(TAG, "%s(): segment data: 0x%06x", __func__, out_data);
ESP_LOGVV(TAG, "%s(): segment data: 0x%06" PRIx32, __func__, out_data);
#if 0
// At the moment an unsupport character is equal to blank (' ').
// To distinguish an unsupported character from blank we would need to
@ -599,12 +587,12 @@ int Max6921Display::update_out_buf_() {
// shift data to the smallest segment OUT position...
out_data <<= (this->seg_out_smallest_);
ESP_LOGVV(TAG, "%s(): segment data shifted to first segment bit (OUT%u): 0x%06x", __func__, this->seg_out_smallest_,
out_data);
ESP_LOGVV(TAG, "%s(): segment data shifted to first segment bit (OUT%u): 0x%06" PRIx32, __func__,
this->seg_out_smallest_, out_data);
// add position data...
out_data |= (1 << this->pos_to_out_map_[pos]);
ESP_LOGVV(TAG, "%s(): OUT data with position: 0x%06x", __func__, out_data);
ESP_LOGVV(TAG, "%s(): OUT data with position: 0x%06" PRIx32, __func__, out_data);
// write to appropriate position of display buffer...
this->out_buf_[pos * 3 + 0] |= (uint8_t) ((out_data >> 16) & 0xFF);
@ -935,46 +923,6 @@ bool Max6921DisplayText::scroll_left() {
return false;
}
/**
* @brief Configures the PWM for display brightness control.
*
* @param pwm_pin_no PWM pin number
* @param channel PWM channel
* @param resolution PWM resolution
* @param freq PWM frequency
*
* @return frequency supported by hardware (0 = no support)
*/
uint32_t Max6921DisplayBrightness::config_brightness_pwm(uint8_t pwm_pin_no, uint8_t channel, uint8_t resolution,
uint32_t freq) {
uint32_t freq_supported;
if ((freq_supported = ledcSetup(channel, freq, resolution)) != 0) {
ledcAttachPin(pwm_pin_no, channel);
this->brightness_pwm_channel_ = channel;
this->brightness_max_duty_ = pow(2, resolution); // max. duty value for given resolution
ESP_LOGD(TAG, "Prepare brightness PWM: pin=%u, channel=%u, resolution=%ubit, freq=%uHz", pwm_pin_no, channel,
resolution, freq_supported);
} else {
ESP_LOGD(TAG, "Failed to configure brightness PWM");
}
return freq_supported;
}
/**
* @brief Sets the display brightness.
*
* @param percent brightness in percent (0.0-1.0)
*/
void Max6921DisplayBrightness::set_brightness(float percent) {
if ((percent >= 0.0) && (percent <= 1.0)) {
this->brightness_cfg_value_ = percent;
this->brightness_cfg_changed_ = true;
} else
ESP_LOGW(TAG, "Invalid brightness value: %f", percent);
}
/**
* @brief Constructor.
*/

View file

@ -2,7 +2,6 @@
#ifdef USE_ESP32_FRAMEWORK_ARDUINO
#include <esp32-hal-gpio.h>
#include <string>
#include <vector>
#include "esphome/core/time.h"
@ -37,19 +36,6 @@ enum DemoModeT {
DEMO_MODE_SCROLL_FONT,
};
class Max6921DisplayBrightness {
public:
uint32_t config_brightness_pwm(uint8_t pwm_pin_no, uint8_t channel, uint8_t resolution, uint32_t freq_wanted);
float get_brightness() { return this->brightness_cfg_value_; }
void set_brightness(float percent);
protected:
float brightness_cfg_value_; // brightness in percent (0.0-1.0)
bool brightness_cfg_changed_;
uint32_t brightness_max_duty_;
uint8_t brightness_pwm_channel_;
};
class Max6921DisplayMode {
public:
DisplayModeT mode;
@ -99,7 +85,7 @@ class Max6921DisplayText {
void init_text_effect_();
};
class Max6921Display : public Max6921DisplayBrightness, public Max6921DisplayMode {
class Max6921Display : public Max6921DisplayMode {
public:
Max6921Display(MAX6921Component *max6921) { max6921_ = max6921; }
void clear(int pos = -1);

View file

@ -3,7 +3,6 @@ import esphome.codegen as cg
from esphome.components import display, spi
import esphome.config_validation as cv
from esphome.const import (
CONF_BRIGHTNESS,
CONF_DURATION,
CONF_EFFECT,
CONF_ID,
@ -17,7 +16,6 @@ from esphome.const import (
DEPENDENCIES = ["spi", "esp32"]
CODEOWNERS = ["@endym"]
CONF_LOAD_PIN = "load_pin"
CONF_BLANK_PIN = "blank_pin"
CONF_OUT_PIN_MAPPING = "out_pin_mapping"
CONF_SEG_TO_OUT_MAP = "seg_to_out_map"
CONF_SEG_A_PIN = "seg_a_pin"
@ -55,7 +53,6 @@ MAX6921Component = max6921_ns.class_(
"MAX6921Component", cg.PollingComponent, spi.SPIDevice
)
MAX6921ComponentRef = MAX6921Component.operator("ref")
SetBrightnessAction = max6921_ns.class_("SetBrightnessAction", automation.Action)
SetDemoModeAction = max6921_ns.class_("SetDemoModeAction", automation.Action)
SetTextAction = max6921_ns.class_("SetTextAction", automation.Action)
@ -117,9 +114,7 @@ CONFIG_SCHEMA = (
{
cv.GenerateID(): cv.declare_id(MAX6921Component),
cv.Required(CONF_LOAD_PIN): pins.gpio_input_pin_schema,
cv.Required(CONF_BLANK_PIN): pins.internal_gpio_output_pin_schema,
cv.Required(CONF_OUT_PIN_MAPPING): OUT_PIN_MAPPING_SCHEMA,
cv.Optional(CONF_BRIGHTNESS, default=1.0): cv.templatable(cv.percentage),
}
)
.extend(cv.polling_component_schema("500ms"))
@ -134,8 +129,6 @@ async def to_code(config):
load_pin = await cg.gpio_pin_expression(config[CONF_LOAD_PIN])
cg.add(var.set_load_pin(load_pin))
blank_pin = await cg.gpio_pin_expression(config[CONF_BLANK_PIN])
cg.add(var.set_blank_pin(blank_pin))
# pass array of display segment pin numbers sorted by pin name...
sorted_list_of_tuples = sorted(
config[CONF_OUT_PIN_MAPPING][CONF_SEG_TO_OUT_MAP].items()
@ -154,7 +147,6 @@ async def to_code(config):
cg.ArrayInitializer(*[tuple[1] for tuple in sorted_list_of_tuples])
)
)
cg.add(var.set_brightness(config[CONF_BRIGHTNESS]))
if CONF_LAMBDA in config:
lambda_ = await cg.process_lambda(
@ -170,30 +162,6 @@ ACTION_SCHEMA = cv.Schema(
)
ACTION_SET_BRIGHTNESS_SCHEMA = cv.All(
automation.maybe_simple_id(
ACTION_SCHEMA.extend(
cv.Schema(
{
cv.Required(CONF_BRIGHTNESS): cv.templatable(cv.percentage),
}
)
)
),
)
@automation.register_action(
"max6921.set_brightness", SetBrightnessAction, ACTION_SET_BRIGHTNESS_SCHEMA
)
async def max6921_set_brightness_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
template_ = await cg.templatable(config[CONF_BRIGHTNESS], args, float)
cg.add(var.set_brightness(template_))
return var
def validate_action_set_text(value):
duration = value.get(CONF_DURATION)
effect_cycle_num = value.get(CONF_EFFECT_CYCLE_NUM)

View file

@ -31,35 +31,15 @@ void MAX6921Component::setup() {
this->display_ = make_unique<Max6921Display>(this);
this->display_->setup(this->seg_to_out_map_, this->pos_to_out_map_);
// setup display brightness (PWM for BLANK pin)...
if (this->display_->config_brightness_pwm(this->blank_pin_->get_pin(), 0, pwm_resolution, pwm_freq_wanted) == 0) {
ESP_LOGE(TAG, "Failed to configure PWM -> set to max. brightness");
this->blank_pin_->pin_mode(gpio::FLAG_OUTPUT);
this->blank_pin_->setup();
this->disable_blank_(); // enable display (max. brightness)
}
this->setup_finished_ = true;
}
void MAX6921Component::dump_config() {
ESP_LOGCONFIG(TAG, "MAX6921:");
LOG_PIN(" LOAD Pin: ", this->load_pin_);
ESP_LOGCONFIG(TAG, " BLANK Pin: GPIO%u", this->blank_pin_->get_pin());
this->display_->dump_config();
}
void MAX6921Component::set_brightness(float brightness) {
if (!this->setup_finished_) {
ESP_LOGD(TAG, "Set brightness: setup not finished -> discard brightness value");
return;
}
if ((brightness == 0.0) || (brightness != this->display_->get_brightness())) {
this->display_->set_brightness(brightness);
ESP_LOGD(TAG, "Set brightness: %.1f", this->display_->get_brightness());
}
}
/**
* @brief Clocks data into MAX6921 via SPI (MSB first).
* Data must contain 3 bytes with following format:
@ -91,7 +71,7 @@ void MAX6921Component::update() {
/*
* Evaluates lambda function
* start_pos: 0..n = left..right display position
* vi_text : display text
* vi_text : display text
*/
uint8_t MAX6921Component::print(uint8_t start_pos, const char *str) {
if (this->display_->mode != DISP_MODE_PRINT) // not in "it.print" mode?

View file

@ -27,8 +27,6 @@ class MAX6921Component : public PollingComponent,
float get_setup_priority() const override;
uint8_t print(uint8_t pos, const char *str);
uint8_t print(const char *str);
void set_blank_pin(InternalGPIOPin *pin) { blank_pin_ = pin; }
void set_brightness(float brightness);
void set_load_pin(GPIOPin *load) { this->load_pin_ = load; }
void set_seg_to_out_pin_map(const std::vector<uint8_t> &pin_map) { this->seg_to_out_map_ = pin_map; }
void set_pos_to_out_pin_map(const std::vector<uint8_t> &pin_map) { this->pos_to_out_map_ = pin_map; }
@ -40,14 +38,11 @@ class MAX6921Component : public PollingComponent,
void update() override;
protected:
InternalGPIOPin *blank_pin_{};
GPIOPin *load_pin_{};
std::vector<uint8_t> pos_to_out_map_; // mapping of display positions to MAX6921 OUT pins
std::vector<uint8_t> seg_to_out_map_; // mapping of display segments to MAX6921 OUT pins
bool setup_finished_{false};
void disable_blank_() { this->blank_pin_->digital_write(false); } // display on
void IRAM_ATTR HOT disable_load_() { this->load_pin_->digital_write(false); }
void enable_blank_() { this->blank_pin_->digital_write(true); } // display off
void IRAM_ATTR HOT enable_load_() { this->load_pin_->digital_write(true); }
optional<max6921_writer_t> writer_{};
};

View file

@ -6,7 +6,6 @@ spi:
display:
- platform: max6921
load_pin: 12
blank_pin: 14
out_pin_mapping:
seg_to_out_map:
seg_a_pin: 0

View file

@ -6,7 +6,6 @@ spi:
display:
- platform: max6921
load_pin: 8
blank_pin: 9
out_pin_mapping:
seg_to_out_map:
seg_a_pin: 0