189 lines
5.1 KiB
C
Raw Normal View History

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>
#include <esp_lcd_panel_ops.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
#define I2C_BUS_PORT 0
#define LVGL_TICK_PERIOD_MS 5
#define LVGL_TASK_STACK_SIZE (4 * 1024)
#define LVGL_TASK_PRIORITY 2
#define LVGL_PALETTE_SIZE 8
static const char *TAG = "lcd-panel";
// According to SSD1306 datasheet
// https://www.digikey.com/en/products/detail/winstar-display/WEA012864DWPP3N00003/20533255
2025-02-14 15:02:49 -05:00
// Bit number used to represent command and parameter
#define SCREEN_WIDTH 128 // OLED display width, in pixels.
#define SCREEN_HEIGHT 64 // OLED display height, in pixels.
2025-02-14 15:02:49 -05:00
#define LCD_H_RES SCREEN_WIDTH
#define LCD_V_RES SCREEN_HEIGHT
#define I2C_HW_ADDR 0x3C
#define LCD_PIXEL_CLOCK_HZ (400 * 1000)
#define LCD_CMD_BITS 8
#define LCD_PARAM_BITS 8
// Pin may vary based on your schematic.
2025-02-14 15:02:49 -05:00
#define PIN_SDA GPIO_NUM_21
#define PIN_SCL GPIO_NUM_22
#define PIN_RST -1
2025-02-14 16:48:04 -05:00
struct I2C {
I2C();
~I2C() = default;
i2c_master_bus_handle_t i2c_bus_;
};
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
};
template<typename T>
class PanelDevice {
public:
PanelDevice(size_t width, size_t height) : width_(width), height_(height) { }
~PanelDevice() = default;
[[nodiscard]] size_t get_width() const { return width_; }
[[nodiscard]] size_t get_height() const { return height_; }
[[nodiscard]] size_t get_area() const { return width_ * height_; }
[[nodiscard]] size_t get_draw_buffer_size() const
{
// LVGL reserves 2x4 bytes in the buffer to be used as a palette.
return width_ * height_ / 8 + LVGL_PALETTE_SIZE;
}
[[nodiscard]] lv_display_t *create_display() const
{
return lv_display_create(width_, height_);
}
[[nodiscard]] T &get_vendor_config() const { return vendor_config_; }
void create_panel(esp_lcd_panel_dev_config_t &panel_config,
esp_lcd_panel_io_handle_t io_handle,
esp_lcd_panel_handle_t &panel_handle);
private:
size_t width_;
size_t height_;
protected:
T vendor_config_;
};
template<typename T>
void PanelDevice<T>::create_panel(esp_lcd_panel_dev_config_t &config,
esp_lcd_panel_io_handle_t io,
esp_lcd_panel_handle_t &panel)
{
// If the passed handle is already allocated, delete it.
if (panel != nullptr) {
esp_lcd_panel_del(panel);
}
// Set the vendor config and allocate a new panel handle.
config.vendor_config = &vendor_config_;
ESP_ERROR_CHECK(esp_lcd_new_panel_ssd1306(io, &config, &panel));
}
class SSD1306 : public PanelDevice<esp_lcd_panel_ssd1306_config_t> {
public:
explicit SSD1306(esp_lcd_panel_ssd1306_config_t ssd_config,
int width = SCREEN_WIDTH, int height = SCREEN_HEIGHT);
~SSD1306() = default;
};
2025-02-14 17:47:44 -05:00
class Panel {
public:
explicit Panel(i2c_master_bus_handle_t i2c);
explicit Panel(const I2C &i2c) : Panel(i2c.i2c_bus_) { }
~Panel() = default;
[[nodiscard]] inline const esp_lcd_panel_t *get() const { return panel_; }
[[nodiscard]] inline esp_lcd_panel_t *get() { return panel_; }
[[nodiscard]] inline const esp_lcd_panel_t *
operator*() const { return get(); }
[[nodiscard]] inline esp_lcd_panel_t *operator*() { return get(); }
esp_lcd_panel_io_handle_t io_handle_;
esp_lcd_panel_handle_t panel_;
private:
esp_lcd_panel_dev_config_t panel_config_;
};
2025-02-14 15:02:49 -05:00
class Display {
public:
2025-02-14 16:48:04 -05:00
explicit Display(const I2C &i2c);
2025-02-14 15:02:49 -05:00
~Display() = default;
2025-02-14 15:56:15 -05:00
[[nodiscard]] inline const lv_display_t *get() const { return display_; }
[[nodiscard]] inline lv_display_t *get() { return display_; }
[[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);
static void lvgl_port_task(void *arg);
2025-02-14 16:33:41 -05:00
// For LV_COLOR_FORMAT_I1 we need an extra buffer to hold the converted data.
2025-02-14 15:02:49 -05:00
static uint8_t oled_buffer_[LCD_H_RES * LCD_V_RES / 8];
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-14 15:02:49 -05:00
lv_display_t *display_;
2025-02-14 16:48:04 -05:00
void *buf_;
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