Skip to content

Commit 9f75a17

Browse files
authored
feat(esp-box): Add methods / tests for getting the curent rotated display size, use it to update display when it rotates (#552)
1 parent 39872d3 commit 9f75a17

File tree

3 files changed

+64
-12
lines changed

3 files changed

+64
-12
lines changed

components/esp-box/example/main/esp_box_example.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,17 @@ extern "C" void app_main(void) {
164164
lv_line_set_points(line1, line_points1, 2);
165165
lv_obj_add_style(line1, &style_line1, 0);
166166

167+
static auto rotate_display = [&]() {
168+
std::lock_guard<std::recursive_mutex> lock(lvgl_mutex);
169+
clear_circles();
170+
static auto rotation = LV_DISPLAY_ROTATION_0;
171+
rotation = static_cast<lv_display_rotation_t>((static_cast<int>(rotation) + 1) % 4);
172+
lv_display_t *disp = lv_display_get_default();
173+
lv_disp_set_rotation(disp, rotation);
174+
// update the size of the screen
175+
lv_obj_set_size(bg, box.rotated_display_width(), box.rotated_display_height());
176+
};
177+
167178
// add a button in the top left which (when pressed) will rotate the display
168179
// through 0, 90, 180, 270 degrees
169180
lv_obj_t *btn = lv_btn_create(lv_screen_active());
@@ -174,16 +185,7 @@ extern "C" void app_main(void) {
174185
// center the text in the button
175186
lv_obj_align(label_btn, LV_ALIGN_CENTER, 0, 0);
176187
lv_obj_add_event_cb(
177-
btn,
178-
[](auto event) {
179-
std::lock_guard<std::recursive_mutex> lock(lvgl_mutex);
180-
clear_circles();
181-
static auto rotation = LV_DISPLAY_ROTATION_0;
182-
rotation = static_cast<lv_display_rotation_t>((static_cast<int>(rotation) + 1) % 4);
183-
lv_display_t *disp = lv_display_get_default();
184-
lv_disp_set_rotation(disp, rotation);
185-
},
186-
LV_EVENT_PRESSED, nullptr);
188+
btn, [](auto event) { rotate_display(); }, LV_EVENT_PRESSED, nullptr);
187189

188190
// disable scrolling on the screen (so that it doesn't behave weirdly when
189191
// rotated and drawing with your finger)
@@ -276,13 +278,15 @@ extern "C" void app_main(void) {
276278

277279
// use the pitch to to draw a line on the screen indiating the
278280
// direction from the center of the screen to "down"
279-
int x0 = box.lcd_width() / 2;
280-
int y0 = box.lcd_height() / 2;
281+
int x0 = box.rotated_display_width() / 2;
282+
int y0 = box.rotated_display_height() / 2;
281283

282284
int x1 = x0 + 50 * gravity_vector.x;
283285
int y1 = y0 + 50 * gravity_vector.y;
284286

285287
static lv_point_precise_t line_points0[] = {{x0, y0}, {x1, y1}};
288+
line_points0[0].x = x0;
289+
line_points0[0].y = y0;
286290
line_points0[1].x = x1;
287291
line_points0[1].y = y1;
288292

@@ -317,6 +321,8 @@ extern "C" void app_main(void) {
317321
y1 = y0 + 50 * vy;
318322

319323
static lv_point_precise_t line_points1[] = {{x0, y0}, {x1, y1}};
324+
line_points1[0].x = x0;
325+
line_points1[0].y = y0;
320326
line_points1[1].x = x1;
321327
line_points1[1].y = y1;
322328

components/esp-box/include/esp-box.hpp

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

169+
/// Get the display width in pixels
170+
/// \return The display width in pixels
171+
static constexpr size_t display_width() { return lcd_width_; }
172+
173+
/// Get the display height in pixels
174+
/// \return The display height in pixels
175+
static constexpr size_t display_height() { return lcd_height_; }
176+
177+
/// Get the display width in pixels, according to the current orientation
178+
/// \return The display width in pixels, according to the current orientation
179+
size_t rotated_display_width() const;
180+
181+
/// Get the display height in pixels, according to the current orientation
182+
/// \return The display height in pixels, according to the current orientation
183+
size_t rotated_display_height() const;
184+
169185
/// Get a shared pointer to the display
170186
/// \return A shared pointer to the display
171187
std::shared_ptr<Display<Pixel>> display() const { return display_; }

components/esp-box/src/video.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,33 @@ float EspBox::brightness() const {
308308
}
309309
return 0.0f; // if no backlight, return 0
310310
}
311+
312+
size_t EspBox::rotated_display_width() const {
313+
auto rotation = lv_display_get_rotation(lv_display_get_default());
314+
switch (rotation) {
315+
// swap
316+
case LV_DISPLAY_ROTATION_90:
317+
case LV_DISPLAY_ROTATION_270:
318+
return lcd_height_;
319+
// as configured
320+
case LV_DISPLAY_ROTATION_0:
321+
case LV_DISPLAY_ROTATION_180:
322+
default:
323+
return lcd_width_;
324+
}
325+
}
326+
327+
size_t EspBox::rotated_display_height() const {
328+
auto rotation = lv_display_get_rotation(lv_display_get_default());
329+
switch (rotation) {
330+
// swap
331+
case LV_DISPLAY_ROTATION_90:
332+
case LV_DISPLAY_ROTATION_270:
333+
return lcd_width_;
334+
// as configured
335+
case LV_DISPLAY_ROTATION_0:
336+
case LV_DISPLAY_ROTATION_180:
337+
default:
338+
return lcd_height_;
339+
}
340+
}

0 commit comments

Comments
 (0)