From 046dfbb6e6f06602319b2294b7759f7dd7c95b8a Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Fri, 14 Feb 2025 17:19:13 -0500 Subject: [PATCH] Add Display::set_text. --- esp/cpp/07_lcd-panel/main/display.cpp | 23 +++++++++++++++++++++++ esp/cpp/07_lcd-panel/main/display.h | 9 +++++++++ esp/cpp/07_lcd-panel/main/main.cpp | 27 +++++++++------------------ 3 files changed, 41 insertions(+), 18 deletions(-) diff --git a/esp/cpp/07_lcd-panel/main/display.cpp b/esp/cpp/07_lcd-panel/main/display.cpp index ab50384..e5d2e57 100644 --- a/esp/cpp/07_lcd-panel/main/display.cpp +++ b/esp/cpp/07_lcd-panel/main/display.cpp @@ -1,4 +1,5 @@ #include "display.h" +#include "widgets/label/lv_label.h" #include #include @@ -97,6 +98,28 @@ Display::Display(const I2C &i2c) : nullptr, LVGL_TASK_PRIORITY, nullptr); } +void Display::set_text(const char *text, + const char *name, + lv_label_long_mode_t long_mode, + lv_align_t align) +{ + // Lock the mutex due to the LVGL APIs are not thread-safe. + ScopedLock lock; + + ESP_LOGI(TAG, "Display LVGL Scroll Text"); + lv_obj_t *scr = lv_display_get_screen_active(display_); + objects_[name] = lv_label_create(scr); + // Circular scroll + lv_label_set_long_mode(objects_[name], long_mode); + lv_label_set_text(objects_[name], text); + + // Set the size of the screen. + // If you use rotation 90 or 270 use lv_display_get_vertical_resolution. + lv_obj_set_width(objects_[name], + lv_display_get_horizontal_resolution(display_)); + lv_obj_align(objects_[name], align, 0, 0); +} + bool Display::lvgl_flush_ready(esp_lcd_panel_io_handle_t, esp_lcd_panel_io_event_data_t *, void *user_ctx) diff --git a/esp/cpp/07_lcd-panel/main/display.h b/esp/cpp/07_lcd-panel/main/display.h index 983708f..ee7be22 100644 --- a/esp/cpp/07_lcd-panel/main/display.h +++ b/esp/cpp/07_lcd-panel/main/display.h @@ -5,9 +5,11 @@ #include #include #include +#include #include "misc/lv_types.h" #include "misc/lv_area.h" #include "display/lv_display.h" +#include "widgets/label/lv_label.h" #define I2C_BUS_PORT 0 #define LVGL_TICK_PERIOD_MS 5 @@ -66,6 +68,11 @@ public: [[nodiscard]] inline lv_display_t *operator*() { return get(); } + void set_text(const char *text, + const char *name, + lv_label_long_mode_t long_mode = LV_LABEL_LONG_SCROLL_CIRCULAR, + lv_align_t align = LV_ALIGN_TOP_MID); + static bool lvgl_flush_ready(esp_lcd_panel_io_handle_t panel, esp_lcd_panel_io_event_data_t *data, void *user_ctx); @@ -93,6 +100,8 @@ protected: private: // virtual void *vendor_config() = 0; + std::unordered_map objects_; + lv_display_t *display_; void *vendor_config_; diff --git a/esp/cpp/07_lcd-panel/main/main.cpp b/esp/cpp/07_lcd-panel/main/main.cpp index 1df6c26..55f7ec1 100644 --- a/esp/cpp/07_lcd-panel/main/main.cpp +++ b/esp/cpp/07_lcd-panel/main/main.cpp @@ -1,31 +1,22 @@ #include "display.h" -#include "esp_log.h" -#include "lvgl.h" - I2C i2c; void setup() { Display d(i2c); - // UI function scope. - { - // Lock the mutex due to the LVGL APIs are not thread-safe - ScopedLock lock; + d.set_text("Test test 12345678910", + "test-text1", + LV_LABEL_LONG_SCROLL, + LV_ALIGN_CENTER); - ESP_LOGI(TAG, "Display LVGL Scroll Text"); - lv_obj_t *scr = lv_display_get_screen_active(d.get()); - lv_obj_t *label = lv_label_create(scr); - // Circular scroll - lv_label_set_long_mode(label, LV_LABEL_LONG_SCROLL_CIRCULAR); - lv_label_set_text(label, "Hello hello hello hello hello hello hello."); + d.set_text("Hello hello hello hello hello hello hello hello!", "test-text2"); - // Size of the screen - // if you use rotation 90 or 270 use lv_display_get_vertical_resolution - lv_obj_set_width(label, lv_display_get_horizontal_resolution(*d)); - lv_obj_align(label, LV_ALIGN_TOP_MID, 0, 0); - } + d.set_text("A random sentence with no meaning at all.", + "test-text3", + LV_LABEL_LONG_CLIP, + LV_ALIGN_BOTTOM_MID); } void loop() { }