diff --git a/components/t-deck/example/main/t_deck_example.cpp b/components/t-deck/example/main/t_deck_example.cpp index 4d83710fd..48fa5597b 100644 --- a/components/t-deck/example/main/t_deck_example.cpp +++ b/components/t-deck/example/main/t_deck_example.cpp @@ -27,7 +27,18 @@ extern "C" void app_main(void) { espp::TDeck &tdeck = espp::TDeck::get(); tdeck.set_log_level(espp::Logger::Verbosity::INFO); + lv_obj_t *bg = nullptr; + static auto rotation = LV_DISPLAY_ROTATION_0; + static auto rotate_display = [&]() { + std::lock_guard lock(lvgl_mutex); + clear_circles(); + rotation = static_cast((static_cast(rotation) + 1) % 4); + lv_display_t *disp = lv_display_get_default(); + lv_disp_set_rotation(disp, rotation); + // update the size of the screen + lv_obj_set_size(bg, tdeck.rotated_display_width(), tdeck.rotated_display_height()); + }; auto keypress_callback = [&](uint8_t key) { logger.info("Key pressed: {}", key); @@ -41,9 +52,7 @@ extern "C" void app_main(void) { logger.info("Rotating display"); std::lock_guard lock(lvgl_mutex); clear_circles(); - rotation = static_cast((static_cast(rotation) + 1) % 4); - lv_display_t *disp = lv_display_get_default(); - lv_disp_set_rotation(disp, rotation); + rotate_display(); } else if (key == 'm') { // 'm' key will toggle audio mute logger.info("Toggling mute"); @@ -126,7 +135,7 @@ extern "C" void app_main(void) { } // set the background color to black - lv_obj_t *bg = lv_obj_create(lv_screen_active()); + bg = lv_obj_create(lv_screen_active()); lv_obj_set_size(bg, tdeck.lcd_width(), tdeck.lcd_height()); lv_obj_set_style_bg_color(bg, lv_color_make(0, 0, 0), 0); @@ -147,15 +156,7 @@ extern "C" void app_main(void) { // center the text in the button lv_obj_align(label_btn, LV_ALIGN_CENTER, 0, 0); lv_obj_add_event_cb( - btn, - [](auto event) { - std::lock_guard lock(lvgl_mutex); - clear_circles(); - rotation = static_cast((static_cast(rotation) + 1) % 4); - lv_display_t *disp = lv_display_get_default(); - lv_disp_set_rotation(disp, rotation); - }, - LV_EVENT_PRESSED, nullptr); + btn, [](auto event) { rotate_display(); }, LV_EVENT_PRESSED, nullptr); // disable scrolling on the screen (so that it doesn't behave weirdly when // rotated and drawing with your finger) diff --git a/components/t-deck/include/t-deck.hpp b/components/t-deck/include/t-deck.hpp index d864f3534..c5a746961 100644 --- a/components/t-deck/include/t-deck.hpp +++ b/components/t-deck/include/t-deck.hpp @@ -317,6 +317,22 @@ class TDeck : public BaseComponent { /// \return The GPIO pin for the LCD data/command signal static constexpr auto get_lcd_dc_gpio() { return lcd_dc_io; } + /// Get the display width in pixels + /// \return The display width in pixels + static constexpr size_t display_width() { return lcd_width_; } + + /// Get the display height in pixels + /// \return The display height in pixels + static constexpr size_t display_height() { return lcd_height_; } + + /// Get the display width in pixels, according to the current orientation + /// \return The display width in pixels, according to the current orientation + size_t rotated_display_width() const; + + /// Get the display height in pixels, according to the current orientation + /// \return The display height in pixels, according to the current orientation + size_t rotated_display_height() const; + /// Get a shared pointer to the display /// \return A shared pointer to the display std::shared_ptr> display() const; diff --git a/components/t-deck/src/t-deck.cpp b/components/t-deck/src/t-deck.cpp index bee0045af..682985ce8 100644 --- a/components/t-deck/src/t-deck.cpp +++ b/components/t-deck/src/t-deck.cpp @@ -543,3 +543,33 @@ float TDeck::brightness() const { } return 0.0f; } + +size_t TDeck::rotated_display_width() const { + auto rotation = lv_display_get_rotation(lv_display_get_default()); + switch (rotation) { + // swap + case LV_DISPLAY_ROTATION_90: + case LV_DISPLAY_ROTATION_270: + return lcd_height_; + // as configured + case LV_DISPLAY_ROTATION_0: + case LV_DISPLAY_ROTATION_180: + default: + return lcd_width_; + } +} + +size_t TDeck::rotated_display_height() const { + auto rotation = lv_display_get_rotation(lv_display_get_default()); + switch (rotation) { + // swap + case LV_DISPLAY_ROTATION_90: + case LV_DISPLAY_ROTATION_270: + return lcd_width_; + // as configured + case LV_DISPLAY_ROTATION_0: + case LV_DISPLAY_ROTATION_180: + default: + return lcd_height_; + } +}