Skip to content

Commit c657746

Browse files
authored
feat(t-deck): Add methods / tests for getting current rotated display size, use it to update display when it rotates in example (#554)
1 parent 9f75a17 commit c657746

3 files changed

Lines changed: 60 additions & 13 deletions

File tree

components/t-deck/example/main/t_deck_example.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,18 @@ extern "C" void app_main(void) {
2727
espp::TDeck &tdeck = espp::TDeck::get();
2828
tdeck.set_log_level(espp::Logger::Verbosity::INFO);
2929

30+
lv_obj_t *bg = nullptr;
31+
3032
static auto rotation = LV_DISPLAY_ROTATION_0;
33+
static auto rotate_display = [&]() {
34+
std::lock_guard<std::recursive_mutex> lock(lvgl_mutex);
35+
clear_circles();
36+
rotation = static_cast<lv_display_rotation_t>((static_cast<int>(rotation) + 1) % 4);
37+
lv_display_t *disp = lv_display_get_default();
38+
lv_disp_set_rotation(disp, rotation);
39+
// update the size of the screen
40+
lv_obj_set_size(bg, tdeck.rotated_display_width(), tdeck.rotated_display_height());
41+
};
3142

3243
auto keypress_callback = [&](uint8_t key) {
3344
logger.info("Key pressed: {}", key);
@@ -41,9 +52,7 @@ extern "C" void app_main(void) {
4152
logger.info("Rotating display");
4253
std::lock_guard<std::recursive_mutex> lock(lvgl_mutex);
4354
clear_circles();
44-
rotation = static_cast<lv_display_rotation_t>((static_cast<int>(rotation) + 1) % 4);
45-
lv_display_t *disp = lv_display_get_default();
46-
lv_disp_set_rotation(disp, rotation);
55+
rotate_display();
4756
} else if (key == 'm') {
4857
// 'm' key will toggle audio mute
4958
logger.info("Toggling mute");
@@ -126,7 +135,7 @@ extern "C" void app_main(void) {
126135
}
127136

128137
// set the background color to black
129-
lv_obj_t *bg = lv_obj_create(lv_screen_active());
138+
bg = lv_obj_create(lv_screen_active());
130139
lv_obj_set_size(bg, tdeck.lcd_width(), tdeck.lcd_height());
131140
lv_obj_set_style_bg_color(bg, lv_color_make(0, 0, 0), 0);
132141

@@ -147,15 +156,7 @@ extern "C" void app_main(void) {
147156
// center the text in the button
148157
lv_obj_align(label_btn, LV_ALIGN_CENTER, 0, 0);
149158
lv_obj_add_event_cb(
150-
btn,
151-
[](auto event) {
152-
std::lock_guard<std::recursive_mutex> lock(lvgl_mutex);
153-
clear_circles();
154-
rotation = static_cast<lv_display_rotation_t>((static_cast<int>(rotation) + 1) % 4);
155-
lv_display_t *disp = lv_display_get_default();
156-
lv_disp_set_rotation(disp, rotation);
157-
},
158-
LV_EVENT_PRESSED, nullptr);
159+
btn, [](auto event) { rotate_display(); }, LV_EVENT_PRESSED, nullptr);
159160

160161
// disable scrolling on the screen (so that it doesn't behave weirdly when
161162
// rotated and drawing with your finger)

components/t-deck/include/t-deck.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,22 @@ class TDeck : public BaseComponent {
317317
/// \return The GPIO pin for the LCD data/command signal
318318
static constexpr auto get_lcd_dc_gpio() { return lcd_dc_io; }
319319

320+
/// Get the display width in pixels
321+
/// \return The display width in pixels
322+
static constexpr size_t display_width() { return lcd_width_; }
323+
324+
/// Get the display height in pixels
325+
/// \return The display height in pixels
326+
static constexpr size_t display_height() { return lcd_height_; }
327+
328+
/// Get the display width in pixels, according to the current orientation
329+
/// \return The display width in pixels, according to the current orientation
330+
size_t rotated_display_width() const;
331+
332+
/// Get the display height in pixels, according to the current orientation
333+
/// \return The display height in pixels, according to the current orientation
334+
size_t rotated_display_height() const;
335+
320336
/// Get a shared pointer to the display
321337
/// \return A shared pointer to the display
322338
std::shared_ptr<Display<Pixel>> display() const;

components/t-deck/src/t-deck.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,3 +543,33 @@ float TDeck::brightness() const {
543543
}
544544
return 0.0f;
545545
}
546+
547+
size_t TDeck::rotated_display_width() const {
548+
auto rotation = lv_display_get_rotation(lv_display_get_default());
549+
switch (rotation) {
550+
// swap
551+
case LV_DISPLAY_ROTATION_90:
552+
case LV_DISPLAY_ROTATION_270:
553+
return lcd_height_;
554+
// as configured
555+
case LV_DISPLAY_ROTATION_0:
556+
case LV_DISPLAY_ROTATION_180:
557+
default:
558+
return lcd_width_;
559+
}
560+
}
561+
562+
size_t TDeck::rotated_display_height() const {
563+
auto rotation = lv_display_get_rotation(lv_display_get_default());
564+
switch (rotation) {
565+
// swap
566+
case LV_DISPLAY_ROTATION_90:
567+
case LV_DISPLAY_ROTATION_270:
568+
return lcd_width_;
569+
// as configured
570+
case LV_DISPLAY_ROTATION_0:
571+
case LV_DISPLAY_ROTATION_180:
572+
default:
573+
return lcd_height_;
574+
}
575+
}

0 commit comments

Comments
 (0)