261 lines
8.3 KiB
C++
261 lines
8.3 KiB
C++
#ifndef PANEL_DEVICE_H
|
|
#define PANEL_DEVICE_H
|
|
|
|
#include <esp_lcd_panel_dev.h>
|
|
#include <esp_lcd_panel_ops.h>
|
|
#include <esp_lcd_panel_io.h>
|
|
#include <esp_log.h>
|
|
|
|
#include <display/lv_display.h>
|
|
|
|
#include "i2c.h"
|
|
|
|
// LVGL reserves 2x4 bytes in the buffer to be used as a palette.
|
|
// This additional space must be added to the IPanelDevice::buf_size_.
|
|
#define LVGL_PALETTE_SIZE 8
|
|
#define LVGL_TICK_PERIOD_MS 5
|
|
#define LVGL_TASK_STACK_SIZE (4 * 1024)
|
|
#define LVGL_TASK_PRIORITY 2
|
|
|
|
#define LCD_H_RES 128
|
|
#define LCD_V_RES 64
|
|
|
|
/**
|
|
* Encapsulates vendor specific ESP LCD panel initialization logic.
|
|
* This pure virtual interface can be inherited from for using new LCD devices.
|
|
* See SSD1306 as an example to implement IPanelDevice for NT35510 or ST7789.
|
|
*
|
|
* At this time only I2C is supported.
|
|
* Classes that inherit from this interface should likely be marked final.
|
|
*/
|
|
class IPanelDevice {
|
|
public:
|
|
/**
|
|
* Construct an IPanelDevice.
|
|
*
|
|
* @param i2c I2C object. Eventually this will mature to IProtocol or similar.
|
|
* @param config I2C configuration for this device.
|
|
* @param height Height of the device screen in pixels.
|
|
* @param width Width of the device screen in pixels.
|
|
*/
|
|
explicit IPanelDevice(I2C &i2c,
|
|
esp_lcd_panel_io_i2c_config_t config,
|
|
int width,
|
|
int height) :
|
|
IPanelDevice(i2c, config, width, height,
|
|
width * height / 8 + LVGL_PALETTE_SIZE) { }
|
|
|
|
/**
|
|
* Construct an IPanelDevice.
|
|
*
|
|
* @param i2c I2C object. Eventually this will mature to IProtocol or similar.
|
|
* @param config I2C configuration for this device.
|
|
* @param height Height of the device screen in pixels.
|
|
* @param width Width of the device screen in pixels.
|
|
* @param draw_buf_size Size of the draw buffer for this device.
|
|
*/
|
|
explicit IPanelDevice(I2C &i2c,
|
|
esp_lcd_panel_io_i2c_config_t io_config,
|
|
int width,
|
|
int height,
|
|
size_t draw_buf_size) :
|
|
width_(width),
|
|
height_(height),
|
|
rst_num_(i2c.rst_num_),
|
|
lv_buf_size_(draw_buf_size),
|
|
esp_io_config_(io_config) { }
|
|
|
|
virtual ~IPanelDevice() = default;
|
|
|
|
//
|
|
// PUBLIC METHODS
|
|
|
|
/**
|
|
* Create an LVGL display using the width and height of this device.
|
|
*
|
|
* @return Handle to the created LVGL display.
|
|
*/
|
|
[[nodiscard]] lv_display_t *create_display() const
|
|
{
|
|
auto display = lv_display_create(width_, height_);
|
|
assert(display);
|
|
return display;
|
|
}
|
|
|
|
/**
|
|
* Create an ESP LCD panel IO handle.
|
|
*
|
|
* @return The created ESP LCD panel IO handle.
|
|
*/
|
|
[[nodiscard]] esp_lcd_panel_io_handle_t create_io_handle()
|
|
{
|
|
ESP_LOGI(TAG, "Creating panel IO handle");
|
|
esp_lcd_panel_io_handle_t handle = nullptr;
|
|
ESP_ERROR_CHECK(
|
|
esp_lcd_new_panel_io_i2c(I2C::get(), &esp_io_config_, &handle));
|
|
return handle;
|
|
}
|
|
|
|
/**
|
|
* Create and initialize an ESP panel handle.
|
|
* IPanelDevice implementors must initialize the panel within init_panel.
|
|
*
|
|
* @param config ESP LCD panel configuration.
|
|
* @param io ESP LCD panel IO handle.
|
|
* @param [out] panel ESP LCD panel handle output pointer location.
|
|
*/
|
|
void 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_LOGI(TAG, "Removing unused panel");
|
|
esp_lcd_panel_del(panel);
|
|
}
|
|
|
|
ESP_LOGI(TAG, "Install SSD1306 panel driver");
|
|
// Call pure virtual method responsible for initializing the panel handle.
|
|
init_panel(config, io, panel);
|
|
}
|
|
|
|
/**
|
|
* Retrieve the device specific vendor configuration structure.
|
|
*
|
|
* @return Address of vendor configuration structure.
|
|
* @sa SSD1306::vendor_config
|
|
*/
|
|
virtual void *vendor_config() = 0;
|
|
|
|
//
|
|
// PUBLIC MEMBERS
|
|
|
|
/// Width of the device screen in pixels.
|
|
int32_t width_;
|
|
|
|
/// Height of the device screen in pixels.
|
|
int32_t height_;
|
|
|
|
/// RST GPIO pin number.
|
|
int rst_num_;
|
|
|
|
/// LVGL draw buffer size for the device.
|
|
size_t lv_buf_size_;
|
|
|
|
/// ESP LCD panel IO configuration.
|
|
esp_lcd_panel_io_i2c_config_t esp_io_config_;
|
|
|
|
/**
|
|
* Registers LVGL draw buffers for this display.
|
|
*
|
|
* An implementation of the interface can optionally override this method to
|
|
* provide custom LVGL callbacks and display configurations.
|
|
*
|
|
* @param display_handle LVGL display handle to use for rendering.
|
|
* @param io_handle IO handle for the ESP LCD panel.
|
|
*/
|
|
virtual void register_draw_buffer(lv_display_t *display_handle,
|
|
esp_lcd_panel_io_handle_t io_handle);
|
|
|
|
/**
|
|
* Registers LVGL ticker timer callback for rendering this display.
|
|
*
|
|
* An implementation of the interface can optionally override this method to
|
|
* provide custom LVGL callbacks and tick configurations.
|
|
*/
|
|
virtual void register_lvgl_tick_timer();
|
|
|
|
private:
|
|
|
|
//
|
|
// PRIVATE METHODS
|
|
|
|
/**
|
|
* Initializes the ESP panel using vendor specific APIs and configurations.
|
|
* This method should implement any setup logic specific to the device.
|
|
*
|
|
* @param config ESP LCD panel configuration.
|
|
* @param io ESP LCD panel IO handle.
|
|
* @param [out] panel ESP LCD panel handle output pointer location.
|
|
*/
|
|
virtual void init_panel(esp_lcd_panel_dev_config_t &config,
|
|
esp_lcd_panel_io_handle_t io,
|
|
esp_lcd_panel_handle_t &panel) = 0;
|
|
|
|
//
|
|
// PRIVATE STATIC METHODS
|
|
|
|
/**
|
|
* The callback invoked when panel IO finishes transferring color data.
|
|
* This signals that the panel is ready to flush image data to the display.
|
|
*
|
|
* @param panel LCD panel IO handles.
|
|
* @param data Panel IO event data, fed by driver.
|
|
* @param user_ctx User data, passed from `esp_lcd_panel_io_xxx_config_t`.
|
|
* @return Whether a high priority task has been waken up by this function.
|
|
* @sa SSD1306::SSD1306 for setting user_ctx data passed to the callback.
|
|
* @sa register_rendering_data for overriding this callback.
|
|
*/
|
|
static bool lvgl_flush_ready_cb(esp_lcd_panel_io_handle_t panel,
|
|
esp_lcd_panel_io_event_data_t *data,
|
|
void *user_ctx);
|
|
|
|
/**
|
|
* The callback invoked for flushing the rendered image to the display.
|
|
*
|
|
* `px_map` contains the rendered image as raw pixel map and it should be
|
|
* copied to `area` on the display.
|
|
*
|
|
* @param display LVGL display handle to use for rendering.
|
|
* @param area Area of the display being flushed.
|
|
* @param px_map Rendered image data for writing to the display area.
|
|
* @sa register_rendering_data for overriding this callback.
|
|
*/
|
|
static void lvgl_flush_cb(lv_display_t *display,
|
|
const lv_area_t *area,
|
|
uint8_t *px_map);
|
|
|
|
/**
|
|
* Callback invoked for every period of the timer.
|
|
*
|
|
* This callback _must_ call lv_tick_inc to inform LVGL how much time has
|
|
* elapsed since the last period of the timer.
|
|
*
|
|
* @param data User data passed to the callback.
|
|
* @sa register_lvgl_tick_timer for setting user data and the tick period of
|
|
* the timer, or overriding this callback entirely.
|
|
*/
|
|
static void lvgl_increase_tick_cb(void *data);
|
|
|
|
/**
|
|
* FreeRTOS task callback invoked for handling LVGL events or updating the UI.
|
|
*
|
|
* This function is intentionally an endless loop and should never return.
|
|
* LVGL initialization logic can optionally be added before entering the loop.
|
|
* Input logic can optionally be handled within the loop.
|
|
*
|
|
* This callback _must_ call lv_timer_handler to handle LVGL periodic timers.
|
|
*
|
|
* @param data User data passed to the callback.
|
|
* @sa register_lvgl_tick_timer for overriding this callback.
|
|
*/
|
|
[[noreturn]] static void lvgl_port_task(void *data);
|
|
|
|
/**
|
|
* Draw buffer for this panel device.
|
|
* For LV_COLOR_FORMAT_I1 we need an extra buffer to hold the converted data.
|
|
*/
|
|
static uint8_t oled_buffer_[LCD_H_RES * LCD_V_RES / 8];
|
|
|
|
//
|
|
// PRIVATE MEMBERS
|
|
|
|
/// LVGL draw buffer associated with this Display's lv_display_t.
|
|
void *lv_buf_;
|
|
|
|
/// Tag used for ESP logging.
|
|
constexpr static const char *TAG = "IPanelDevice";
|
|
};
|
|
|
|
#endif // PANEL_DEVICE_H
|