klips/esp/cpp/07_lcd-panel/main/display.cpp

161 lines
5.3 KiB
C++
Raw Normal View History

2025-02-15 10:11:49 -05:00
2025-02-14 15:02:49 -05:00
#include "display.h"
2025-02-15 10:11:49 -05:00
#include "ssd1306.h"
2025-02-14 15:02:49 -05:00
#include <esp_timer.h>
2025-02-15 18:26:26 -05:00
2025-02-14 15:02:49 -05:00
#include <lv_init.h>
2025-02-15 18:26:26 -05:00
2025-02-14 15:02:49 -05:00
#include <mutex>
2025-02-14 16:33:41 -05:00
// LVGL library is not thread-safe, this example calls LVGL APIs from tasks.
// We must use a mutex to protect it.
2025-02-15 18:26:26 -05:00
_lock_t Display::ScopedLock::lv_lock_;
2025-02-14 15:02:49 -05:00
2025-02-15 18:26:26 -05:00
Display::Display(IPanelDevice &device) :
2025-02-15 17:12:06 -05:00
panel_(device)
2025-02-14 15:02:49 -05:00
{
2025-02-14 17:47:44 -05:00
if (!lv_is_initialized()) {
ESP_LOGI(TAG, "Initialize LVGL");
lv_init();
}
2025-02-14 15:02:49 -05:00
2025-02-15 18:26:26 -05:00
ESP_LOGI(TAG, "Creating LVGL display");
2025-02-15 17:12:06 -05:00
lv_display_ = panel_.device_->create_display();
2025-02-14 15:02:49 -05:00
// associate the i2c panel handle to the display
2025-02-15 17:12:06 -05:00
lv_display_set_user_data(lv_display_, panel_.esp_panel_);
2025-02-15 14:04:08 -05:00
// Create draw buffer.
2025-02-14 15:02:49 -05:00
ESP_LOGI(TAG, "Allocate separate LVGL draw buffers");
2025-02-15 17:12:06 -05:00
lv_buf_ = heap_caps_calloc(1, panel_.device_->lv_buf_size_,
MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
assert(lv_buf_);
2025-02-14 15:02:49 -05:00
2025-02-15 18:26:26 -05:00
ESP_LOGI(TAG, "Set LVGL draw buffers");
// Color format must be set first, LVGL9 suooprt new monochromatic format.
2025-02-15 17:12:06 -05:00
lv_display_set_color_format(lv_display_, LV_COLOR_FORMAT_I1);
lv_display_set_buffers(lv_display_, lv_buf_, nullptr,
panel_.device_->lv_buf_size_,
2025-02-14 15:02:49 -05:00
LV_DISPLAY_RENDER_MODE_FULL);
2025-02-15 17:12:06 -05:00
lv_display_set_rotation(lv_display_, LV_DISPLAY_ROTATION_0);
2025-02-15 18:26:26 -05:00
ESP_LOGI(TAG, "Set LVGL callback for flushing to the display");
2025-02-15 17:12:06 -05:00
lv_display_set_flush_cb(lv_display_, Display::lvgl_flush_cb);
2025-02-14 15:02:49 -05:00
2025-02-15 14:04:08 -05:00
ESP_LOGI(TAG, "Register io panel callback for LVGL flush ready notification");
2025-02-14 15:02:49 -05:00
const esp_lcd_panel_io_callbacks_t cbs = {
.on_color_trans_done = Display::lvgl_flush_ready,
};
ESP_ERROR_CHECK(
2025-02-15 18:26:26 -05:00
esp_lcd_panel_io_register_event_callbacks(panel_.esp_io_handle_, &cbs,
2025-02-15 17:12:06 -05:00
lv_display_));
2025-02-14 15:02:49 -05:00
2025-02-15 18:26:26 -05:00
ESP_LOGI(TAG, "Use esp_timer to increase LVGL tick");
2025-02-14 15:02:49 -05:00
const esp_timer_create_args_t lvgl_tick_timer_args = {
.callback = &Display::lvgl_increase_tick,
.name = "lvgl_tick"
};
esp_timer_handle_t lvgl_tick_timer = nullptr;
ESP_ERROR_CHECK(esp_timer_create(&lvgl_tick_timer_args, &lvgl_tick_timer));
ESP_ERROR_CHECK(esp_timer_start_periodic(lvgl_tick_timer,
LVGL_TICK_PERIOD_MS * 1000));
ESP_LOGI(TAG, "Create LVGL task");
xTaskCreate(Display::lvgl_port_task, "LVGL", LVGL_TASK_STACK_SIZE,
nullptr, LVGL_TASK_PRIORITY, nullptr);
}
2025-02-14 17:19:13 -05:00
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");
2025-02-15 17:12:06 -05:00
lv_obj_t *scr = lv_display_get_screen_active(lv_display_);
2025-02-15 18:26:26 -05:00
lv_objects_[name] = lv_label_create(scr);
2025-02-15 10:23:59 -05:00
// Circular scroll.
2025-02-15 18:26:26 -05:00
auto obj = lv_objects_[name];
2025-02-15 10:23:59 -05:00
lv_label_set_long_mode(obj, long_mode);
lv_label_set_text(obj, text);
2025-02-14 17:19:13 -05:00
// Set the size of the screen.
// If you use rotation 90 or 270 use lv_display_get_vertical_resolution.
2025-02-15 17:12:06 -05:00
lv_obj_set_width(obj, lv_display_get_horizontal_resolution(lv_display_));
2025-02-15 10:23:59 -05:00
lv_obj_align(obj, align, 0, 0);
2025-02-14 17:19:13 -05:00
}
2025-02-14 15:02:49 -05:00
bool Display::lvgl_flush_ready(esp_lcd_panel_io_handle_t,
esp_lcd_panel_io_event_data_t *,
void *user_ctx)
{
auto *disp = (lv_display_t *) user_ctx;
lv_display_flush_ready(disp);
return false;
}
void Display::lvgl_flush_cb(lv_display_t *display, const lv_area_t *area,
uint8_t *px_map)
{
auto panel_handle =
(esp_lcd_panel_handle_t) lv_display_get_user_data(display);
// Necessary because LVGL reserves 2x4 bytes in the buffer for a palette.
// For more information about the monochrome, please refer to:
// https://docs.lvgl.io/9.2/porting/display.html#monochrome-displays
// Skip the palette here.
2025-02-14 15:02:49 -05:00
px_map += LVGL_PALETTE_SIZE;
uint16_t hor_res = lv_display_get_physical_horizontal_resolution(display);
int32_t x1 = area->x1;
int32_t x2 = area->x2;
int32_t y1 = area->y1;
int32_t y2 = area->y2;
for (int32_t y = y1; y <= y2; y++) {
for (int32_t x = x1; x <= x2; x++) {
/* The order of bits is MSB first.
2025-02-14 15:02:49 -05:00
MSB LSB
bits 7 6 5 4 3 2 1 0
pixels 0 1 2 3 4 5 6 7
Left Right
*/
bool chroma_color = (px_map[(hor_res >> 3) * y + (x >> 3)] &
1 << (7 - x % 8));
// Write to the buffer as required for the display.
// It writes only 1-bit for monochrome displays mapped vertically.
2025-02-15 10:11:49 -05:00
uint8_t *buf = SSD1306::oled_buffer_ + hor_res * (y >> 3) + (x);
2025-02-14 15:02:49 -05:00
if (chroma_color) {
(*buf) &= ~(1 << (y % 8));
} else {
(*buf) |= (1 << (y % 8));
}
}
}
// Pass the draw buffer to the driver.
2025-02-14 15:02:49 -05:00
ESP_ERROR_CHECK(
esp_lcd_panel_draw_bitmap(panel_handle, x1, y1, x2 + 1, y2 + 1,
2025-02-15 10:11:49 -05:00
SSD1306::oled_buffer_));
2025-02-14 15:02:49 -05:00
}
void Display::lvgl_increase_tick(void *)
2025-02-14 15:02:49 -05:00
{
// Tell LVGL how many milliseconds has elapsed
2025-02-14 15:02:49 -05:00
lv_tick_inc(LVGL_TICK_PERIOD_MS);
}
2025-02-15 10:11:49 -05:00
[[noreturn]] void Display::lvgl_port_task(void *)
2025-02-14 15:02:49 -05:00
{
ESP_LOGI(TAG, "Starting LVGL task");
2025-02-15 10:11:49 -05:00
for (uint32_t time_till_next_ms = 0; true;) {
2025-02-15 18:26:26 -05:00
_lock_acquire(&ScopedLock::lv_lock_);
2025-02-14 15:02:49 -05:00
time_till_next_ms = lv_timer_handler();
2025-02-15 18:26:26 -05:00
_lock_release(&ScopedLock::lv_lock_);
2025-02-14 15:02:49 -05:00
usleep(1000 * time_till_next_ms);
}
}