[hub75] Add set_brightness action (#12521)

This commit is contained in:
Stuart Parmenter
2025-12-16 19:12:33 -08:00
committed by GitHub
parent 1122ec354f
commit 084f517a20
7 changed files with 57 additions and 19 deletions

View File

@@ -1,6 +1,6 @@
from typing import Any
from esphome import pins
from esphome import automation, pins
import esphome.codegen as cg
from esphome.components import display
from esphome.components.esp32 import add_idf_component
@@ -17,6 +17,8 @@ from esphome.const import (
CONF_OE_PIN,
CONF_UPDATE_INTERVAL,
)
from esphome.core import ID
from esphome.cpp_generator import MockObj, TemplateArgsType
import esphome.final_validate as fv
from esphome.types import ConfigType
@@ -135,6 +137,7 @@ CLOCK_SPEEDS = {
HUB75Display = hub75_ns.class_("HUB75Display", cg.PollingComponent, display.Display)
Hub75Config = cg.global_ns.struct("Hub75Config")
Hub75Pins = cg.global_ns.struct("Hub75Pins")
SetBrightnessAction = hub75_ns.class_("SetBrightnessAction", automation.Action)
def _merge_board_pins(config: ConfigType) -> ConfigType:
@@ -576,3 +579,27 @@ async def to_code(config: ConfigType) -> None:
config[CONF_LAMBDA], [(display.DisplayRef, "it")], return_type=cg.void
)
cg.add(var.set_writer(lambda_))
@automation.register_action(
"hub75.set_brightness",
SetBrightnessAction,
cv.maybe_simple_value(
{
cv.GenerateID(): cv.use_id(HUB75Display),
cv.Required(CONF_BRIGHTNESS): cv.templatable(cv.int_range(min=0, max=255)),
},
key=CONF_BRIGHTNESS,
),
)
async def hub75_set_brightness_to_code(
config: ConfigType,
action_id: ID,
template_arg: cg.TemplateArguments,
args: TemplateArgsType,
) -> MockObj:
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
template_ = await cg.templatable(config[CONF_BRIGHTNESS], args, cg.uint8)
cg.add(var.set_brightness(template_))
return var

View File

@@ -179,7 +179,7 @@ void HOT HUB75Display::draw_pixels_at(int x_start, int y_start, int w, int h, co
}
}
void HUB75Display::set_brightness(int brightness) {
void HUB75Display::set_brightness(uint8_t brightness) {
this->brightness_ = brightness;
this->enabled_ = (brightness > 0);
if (this->driver_ != nullptr) {

View File

@@ -5,6 +5,7 @@
#include <utility>
#include "esphome/components/display/display_buffer.h"
#include "esphome/core/automation.h"
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
@@ -34,7 +35,7 @@ class HUB75Display : public display::Display {
display::ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) override;
// Brightness control (runtime mutable)
void set_brightness(int brightness);
void set_brightness(uint8_t brightness);
protected:
// Display internal methods
@@ -46,10 +47,17 @@ class HUB75Display : public display::Display {
Hub75Config config_; // Immutable configuration
// Runtime state (mutable)
int brightness_{128};
uint8_t brightness_{128};
bool enabled_{false};
};
template<typename... Ts> class SetBrightnessAction : public Action<Ts...>, public Parented<HUB75Display> {
public:
TEMPLATABLE_VALUE(uint8_t, brightness)
void play(const Ts &...x) override { this->parent_->set_brightness(this->brightness_.value(x...)); }
};
} // namespace esphome::hub75
#endif

View File

@@ -0,0 +1,12 @@
esphome:
on_boot:
# Test simple value
- hub75.set_brightness: 200
# Test templatable value
- hub75.set_brightness: !lambda 'return 100;'
# Test with explicit ID
- hub75.set_brightness:
id: my_hub75
brightness: 50

View File

@@ -1,8 +1,3 @@
esp32:
board: esp32dev
framework:
type: esp-idf
display:
- platform: hub75
id: my_hub75
@@ -37,3 +32,5 @@ display:
then:
lambda: |-
ESP_LOGD("display", "1 -> 2");
<<: !include common.yaml

View File

@@ -1,8 +1,3 @@
esp32:
board: esp32-s3-devkitc-1
framework:
type: esp-idf
display:
- platform: hub75
id: hub75_display_board
@@ -24,3 +19,5 @@ display:
then:
lambda: |-
ESP_LOGD("display", "1 -> 2");
<<: !include common.yaml

View File

@@ -1,8 +1,3 @@
esp32:
board: esp32-s3-devkitc-1
framework:
type: esp-idf
display:
- platform: hub75
id: my_hub75
@@ -37,3 +32,5 @@ display:
then:
lambda: |-
ESP_LOGD("display", "1 -> 2");
<<: !include common.yaml