From 254690100c4768e592017c47550fbf8dbcce69ef Mon Sep 17 00:00:00 2001 From: TJ Horner Date: Sat, 10 Oct 2026 20:52:24 -0700 Subject: [PATCH] [hub75] add set_rotation and get_rotation methods (#13921) Co-authored-by: J. Nick Koston --- esphome/components/display/display.h | 4 +- esphome/components/hub75/display.py | 6 +-- esphome/components/hub75/hub75.cpp | 40 +++++++++++++++++++ esphome/components/hub75/hub75_component.h | 5 ++- .../hub75/test.esp32-s3-idf-rotate.yaml | 7 ++++ 5 files changed, 55 insertions(+), 7 deletions(-) diff --git a/esphome/components/display/display.h b/esphome/components/display/display.h index cc75eb70b8f..74b5bcedd94 100644 --- a/esphome/components/display/display.h +++ b/esphome/components/display/display.h @@ -294,7 +294,7 @@ using display_writer_t = DisplayWriter; prefix type "\n" \ "%s Rotations: %d °\n" \ "%s Dimensions: %dpx x %dpx", \ - prefix, log_display_obj->rotation_, prefix, log_display_obj->get_width(), \ + prefix, log_display_obj->get_rotation(), prefix, log_display_obj->get_width(), \ log_display_obj->get_height()); \ } @@ -710,7 +710,7 @@ class Display : public PollingComponent { // Internal method to set display auto clearing. void set_auto_clear(bool auto_clear_enabled) { this->auto_clear_enabled_ = auto_clear_enabled; } - DisplayRotation get_rotation() const { return this->rotation_; } + virtual DisplayRotation get_rotation() const { return this->rotation_; } /** Get the type of display that the buffer corresponds to. In case of dynamically configurable displays, * returns the type the display is currently configured to. diff --git a/esphome/components/hub75/display.py b/esphome/components/hub75/display.py index 08d068d0f03..823928176a1 100644 --- a/esphome/components/hub75/display.py +++ b/esphome/components/hub75/display.py @@ -601,10 +601,8 @@ async def to_code(config: ConfigType) -> None: pins_struct = _build_pins_struct(pin_expressions, e_pin_num) hub75_config = _build_config_struct(config, pins_struct, min_refresh) - # Rotation is handled by the hub75 driver (config_.rotation already set above). - # Force rotation to 0 for ESPHome's Display base class to avoid double-rotation. - if CONF_ROTATION in config: - config[CONF_ROTATION] = 0 + # The driver rotates the panel itself (config_.rotation above), so the base class must not rotate too + config.pop(CONF_ROTATION, None) # Create display and register var = cg.new_Pvariable(config[CONF_ID], hub75_config) diff --git a/esphome/components/hub75/hub75.cpp b/esphome/components/hub75/hub75.cpp index 674e23cb344..226b59a2912 100644 --- a/esphome/components/hub75/hub75.cpp +++ b/esphome/components/hub75/hub75.cpp @@ -202,6 +202,46 @@ void HUB75Display::set_brightness(uint8_t brightness) { } } +static Hub75Rotation to_hub75_rotation(display::DisplayRotation rotation) { + switch (rotation) { + case display::DisplayRotation::DISPLAY_ROTATION_90_DEGREES: + return Hub75Rotation::ROTATE_90; + case display::DisplayRotation::DISPLAY_ROTATION_180_DEGREES: + return Hub75Rotation::ROTATE_180; + case display::DisplayRotation::DISPLAY_ROTATION_270_DEGREES: + return Hub75Rotation::ROTATE_270; + default: + return Hub75Rotation::ROTATE_0; + } +} + +static display::DisplayRotation to_display_rotation(Hub75Rotation rotation) { + switch (rotation) { + case Hub75Rotation::ROTATE_90: + return display::DisplayRotation::DISPLAY_ROTATION_90_DEGREES; + case Hub75Rotation::ROTATE_180: + return display::DisplayRotation::DISPLAY_ROTATION_180_DEGREES; + case Hub75Rotation::ROTATE_270: + return display::DisplayRotation::DISPLAY_ROTATION_270_DEGREES; + default: + return display::DisplayRotation::DISPLAY_ROTATION_0_DEGREES; + } +} + +// Before setup() creates the driver the configuration holds the rotation, afterwards the driver does +void HUB75Display::set_rotation(display::DisplayRotation rotation) { + const Hub75Rotation hub75_rotation = to_hub75_rotation(rotation); + if (this->driver_ == nullptr) { + this->config_.rotation = hub75_rotation; + return; + } + this->driver_->set_rotation(hub75_rotation); +} + +display::DisplayRotation HUB75Display::get_rotation() const { + return to_display_rotation(this->driver_ == nullptr ? this->config_.rotation : this->driver_->get_rotation()); +} + } // namespace esphome::hub75 #endif diff --git a/esphome/components/hub75/hub75_component.h b/esphome/components/hub75/hub75_component.h index dbadc56c9df..122e8960fb0 100644 --- a/esphome/components/hub75/hub75_component.h +++ b/esphome/components/hub75/hub75_component.h @@ -36,6 +36,9 @@ class HUB75Display final : public display::Display { // Brightness control (runtime mutable) void set_brightness(uint8_t brightness); + void set_rotation(display::DisplayRotation rotation) override; + display::DisplayRotation get_rotation() const override; + protected: // Display internal methods int get_width_internal() override { return this->driver_ != nullptr ? this->driver_->get_width() : 0; } @@ -43,7 +46,7 @@ class HUB75Display final : public display::Display { // Member variables Hub75Driver *driver_{nullptr}; - Hub75Config config_; // Immutable configuration + Hub75Config config_; // Fixed after setup(); only the rotation can change before the driver exists // Runtime state (mutable) uint8_t brightness_{128}; diff --git a/tests/components/hub75/test.esp32-s3-idf-rotate.yaml b/tests/components/hub75/test.esp32-s3-idf-rotate.yaml index 9855fcb4e69..1a08549a2fe 100644 --- a/tests/components/hub75/test.esp32-s3-idf-rotate.yaml +++ b/tests/components/hub75/test.esp32-s3-idf-rotate.yaml @@ -37,3 +37,10 @@ display: } <<: !include common.yaml + +script: + - id: rotate_hub75 + then: + - lambda: |- + id(my_hub75).set_rotation(display::DISPLAY_ROTATION_180_DEGREES); + ESP_LOGD("test", "rotation now %d", static_cast(id(my_hub75).get_rotation()));