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-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";
|
|
|
|
|
|
|
|
// https://www.digikey.com/en/products/detail/winstar-display/WEA012864DWPP3N00003/20533255
|
|
|
|
// According to SSD1306 datasheet
|
|
|
|
// 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
|
|
|
|
#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
|
|
|
|
#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
|
|
|
};
|
|
|
|
|
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_;
|
|
|
|
|
|
|
|
esp_lcd_panel_ssd1306_config_t ssd1306_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
|
|
|
};
|
|
|
|
|
|
|
|
//class SSD1306 : public Display {
|
|
|
|
//public:
|
|
|
|
// SSD1306();
|
|
|
|
//
|
|
|
|
// void * vendor_config() override;
|
|
|
|
//
|
|
|
|
//private:
|
|
|
|
// esp_lcd_panel_dev_config_t panel_config_;
|
|
|
|
// esp_lcd_panel_ssd1306_config_t ssd1306_config_;
|
|
|
|
//};
|
|
|
|
|
|
|
|
#endif // DISPLAY_H
|