feat: add display and touch initialization in DisplayHandler

This commit is contained in:
GW_MC
2026-01-21 14:10:39 +08:00
parent 44fb9aa632
commit 4fa8dc608f
3 changed files with 184 additions and 88 deletions

View File

@@ -1,48 +1,38 @@
#pragma once
#include <stdio.h>
#include <inttypes.h>
#include "driver/spi_master.h"
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
typedef void (*shutdown_display_handlerFunc)(void);
typedef void (*restart_display_handlerFunc)(void);
#include "esp_lcd_touch_gt911.h"
#include "display/constants.h"
#include <driver/i2c.h>
class DisplayHandler {
public:
DisplayHandler(QueueHandle_t touch_queue, SemaphoreHandle_t lvgl_mutex);
// the system_event_group is used to set display-ready bit
virtual void init(EventGroupHandle_t system_event_group) = 0;
virtual void start_event_loop() = 0;
// get a handler to perform display shutdown cleanup, this is called after event loop ends and DisplayHandler is deleted
virtual shutdown_display_handlerFunc get_shutdown_display_handler() = 0;
virtual restart_display_handlerFunc get_restart_display_handler() = 0;
virtual ~DisplayHandler() = 0;
DisplayHandler(
EventGroupHandle_t system_event_group
) : _system_event_group(system_event_group) { }
~DisplayHandler();
// required to be called by inheriting class after SPI device is created
// set set_display_ready to false if further initialization is needed before marking display ready
void init_devices(bool set_display_ready = true);
void epd_write_cmd(uint8_t cmd);
void epd_write_data(uint8_t data);
void epd_write_cmd_with_data(uint8_t cmd, const uint8_t* data, size_t data_len);
private:
DisplayHandler(const DisplayHandler&) = delete;
DisplayHandler& operator=(const DisplayHandler&) = delete;
};
class EInkDisplayHandler : public DisplayHandler {
public:
EInkDisplayHandler(QueueHandle_t touch_queue, SemaphoreHandle_t lvgl_mutex);
void init(EventGroupHandle_t system_event_group) override;
void start_event_loop() override;
shutdown_display_handlerFunc get_shutdown_display_handler() override;
restart_display_handlerFunc get_restart_display_handler() override;
~EInkDisplayHandler() override;
private:
// Task adapter used for FreeRTOS task creation. It forwards to the
// instance `run_event_loop()` method using the `this` pointer passed
// as the task parameter.
static void task_adapter(void* arg);
// Instance method that implements the display task loop.
void run_event_loop();
// prevent copying
EInkDisplayHandler(const EInkDisplayHandler&) = delete;
EInkDisplayHandler& operator=(const EInkDisplayHandler&) = delete;
SemaphoreHandle_t _spi_mutex = xSemaphoreCreateMutex();
spi_device_handle_t _spi = nullptr;
EventGroupHandle_t _system_event_group = nullptr;
esp_lcd_panel_io_handle_t _tp_io_handle = nullptr;
esp_lcd_touch_handle_t _tp_handle = nullptr;
void _dangerous_epd_write_cmd_without_lock(uint8_t cmd);
void _dangerous_epd_write_data_without_lock(uint8_t data);
void _epd_init(void);
void _touch_init(void);
};