2025-02-14 15:02:49 -05:00
|
|
|
#ifndef DISPLAY_H
|
|
|
|
#define DISPLAY_H
|
|
|
|
|
|
|
|
#include <esp_lcd_types.h>
|
|
|
|
#include <esp_lcd_panel_ssd1306.h>
|
|
|
|
#include <driver/i2c_types.h>
|
2025-02-14 16:33:41 -05:00
|
|
|
#include <driver/i2c_master.h>
|
2025-02-14 17:19:13 -05:00
|
|
|
#include <unordered_map>
|
2025-02-15 09:40:02 -05:00
|
|
|
#include <esp_lcd_panel_ops.h>
|
2025-02-15 10:23:59 -05:00
|
|
|
#include <esp_lcd_panel_io.h>
|
2025-02-14 15:02:49 -05:00
|
|
|
#include "misc/lv_types.h"
|
|
|
|
#include "misc/lv_area.h"
|
2025-02-14 16:33:41 -05:00
|
|
|
#include "display/lv_display.h"
|
2025-02-14 17:19:13 -05:00
|
|
|
#include "widgets/label/lv_label.h"
|
2025-02-14 15:02:49 -05:00
|
|
|
|
2025-02-15 10:11:49 -05:00
|
|
|
#include "panel_device.h"
|
|
|
|
|
2025-02-14 15:02:49 -05:00
|
|
|
#define I2C_BUS_PORT 0
|
|
|
|
#define LVGL_TICK_PERIOD_MS 5
|
|
|
|
#define LVGL_TASK_STACK_SIZE (4 * 1024)
|
|
|
|
#define LVGL_TASK_PRIORITY 2
|
|
|
|
|
2025-02-14 16:48:04 -05:00
|
|
|
struct I2C {
|
2025-02-15 17:12:06 -05:00
|
|
|
I2C(gpio_num_t sda, gpio_num_t scl);
|
2025-02-14 16:48:04 -05:00
|
|
|
|
|
|
|
~I2C() = default;
|
|
|
|
|
|
|
|
i2c_master_bus_handle_t i2c_bus_;
|
2025-02-15 10:23:59 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
i2c_master_bus_config_t bus_config_;
|
2025-02-14 16:48:04 -05:00
|
|
|
};
|
|
|
|
|
2025-02-14 15:50:35 -05:00
|
|
|
struct ScopedLock {
|
2025-02-14 16:33:41 -05:00
|
|
|
explicit ScopedLock() { _lock_acquire(&lock_); }
|
2025-02-14 15:50:35 -05:00
|
|
|
|
2025-02-14 16:33:41 -05:00
|
|
|
~ScopedLock() { _lock_release(&lock_); }
|
|
|
|
|
|
|
|
// LVGL library is not thread-safe, this example calls LVGL APIs from tasks.
|
|
|
|
// We must use a mutex to protect it.
|
|
|
|
static _lock_t lock_;
|
2025-02-14 15:50:35 -05:00
|
|
|
};
|
|
|
|
|
2025-02-14 17:47:44 -05:00
|
|
|
class Panel {
|
|
|
|
public:
|
2025-02-15 17:12:06 -05:00
|
|
|
explicit Panel(IPanelDevice *device);
|
2025-02-14 17:47:44 -05:00
|
|
|
|
|
|
|
~Panel() = default;
|
|
|
|
|
2025-02-15 17:12:06 -05:00
|
|
|
IPanelDevice *device_;
|
2025-02-14 17:47:44 -05:00
|
|
|
|
|
|
|
esp_lcd_panel_io_handle_t io_handle_;
|
|
|
|
|
2025-02-15 17:12:06 -05:00
|
|
|
esp_lcd_panel_handle_t esp_panel_;
|
2025-02-14 17:47:44 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
esp_lcd_panel_dev_config_t panel_config_;
|
|
|
|
};
|
|
|
|
|
2025-02-14 15:02:49 -05:00
|
|
|
class Display {
|
|
|
|
public:
|
2025-02-15 17:12:06 -05:00
|
|
|
explicit Display(IPanelDevice *device);
|
2025-02-14 15:02:49 -05:00
|
|
|
|
|
|
|
~Display() = default;
|
|
|
|
|
2025-02-15 17:12:06 -05:00
|
|
|
[[nodiscard]] inline const lv_display_t *get() const { return lv_display_; }
|
2025-02-14 15:56:15 -05:00
|
|
|
|
2025-02-15 17:12:06 -05:00
|
|
|
[[nodiscard]] inline lv_display_t *get() { return lv_display_; }
|
2025-02-14 15:56:15 -05:00
|
|
|
|
|
|
|
[[nodiscard]] inline const lv_display_t *operator*() const { return get(); }
|
|
|
|
|
|
|
|
[[nodiscard]] inline lv_display_t *operator*() { return get(); }
|
2025-02-14 15:02:49 -05:00
|
|
|
|
2025-02-14 17:19:13 -05:00
|
|
|
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);
|
|
|
|
|
2025-02-14 15:02:49 -05:00
|
|
|
static bool lvgl_flush_ready(esp_lcd_panel_io_handle_t panel,
|
|
|
|
esp_lcd_panel_io_event_data_t *data,
|
|
|
|
void *user_ctx);
|
|
|
|
|
2025-02-14 16:33:41 -05:00
|
|
|
static void lvgl_flush_cb(lv_display_t *display,
|
|
|
|
const lv_area_t *area,
|
|
|
|
uint8_t *px_map);
|
2025-02-14 15:02:49 -05:00
|
|
|
|
|
|
|
static void lvgl_increase_tick(void *arg);
|
|
|
|
|
2025-02-15 10:11:49 -05:00
|
|
|
[[noreturn]] static void lvgl_port_task(void *arg);
|
2025-02-14 16:48:04 -05:00
|
|
|
|
2025-02-14 15:02:49 -05:00
|
|
|
private:
|
2025-02-14 17:47:44 -05:00
|
|
|
Panel panel_;
|
2025-02-14 17:19:13 -05:00
|
|
|
|
2025-02-15 17:12:06 -05:00
|
|
|
lv_display_t *lv_display_;
|
2025-02-15 14:04:08 -05:00
|
|
|
|
|
|
|
// Draw buffer associated with the lv_display_t.
|
2025-02-15 17:12:06 -05:00
|
|
|
void *lv_buf_;
|
2025-02-14 17:47:44 -05:00
|
|
|
|
2025-02-15 14:04:08 -05:00
|
|
|
// Objects stored in the screen associated with this display.
|
|
|
|
// @sa Display::set_text
|
|
|
|
// @sa lv_display_get_screen_active
|
2025-02-14 17:47:44 -05:00
|
|
|
std::unordered_map<const char *, lv_obj_t *> objects_;
|
2025-02-14 15:02:49 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // DISPLAY_H
|