39#ifdef ARDUINO_ESP32P4_DEV
41#if ESP_ARDUINO_VERSION_VAL(3,3,7) > ESP_ARDUINO_VERSION
42#error "This example requires Arduino-ESP32 version 3.3.7 or higher.Please update your Arduino-ESP32 core to the latest version to run this example."
47#include "esp_lcd_mipi_dsi.h"
48#include "esp_lcd_panel_ops.h"
50#include "esp_ldo_regulator.h"
53#if (ESP_ARDUINO_VERSION < ESP_ARDUINO_VERSION_VAL(3,2,0))
54#error "This example requires Arduino-ESP32 version 3.2.0 or higher. Please update your Arduino-ESP32 core to the latest version to run this example."
60#define P4_DISPLAY_BACKLIGHT 51
61#define P4_IO_EXPANDER_IRQ 5
62#define IO_EXPANDER_TOUCH_RST (3 | 0x80)
63#define IO_EXPANDER_TOUCH_IRQ (4 | 0x80)
64#define IO_EXPANDER_3V3_POWER_EN 0
65#define IO_EXPANDER_5V0_POWER_EN 6
66#define IO_EXPANDER_P4_VCCA_POWER_EN 8
67#define IO_EXPANDER_DISP_RST 2
70#define HI8561_SCREEN_WIDTH 540
71#define HI8561_SCREEN_HEIGHT 1168
72#define HI8561_SCREEN_MIPI_DSI_DPI_CLK_MHZ 60
73#define HI8561_SCREEN_MIPI_DSI_HSYNC 28
74#define HI8561_SCREEN_MIPI_DSI_HBP 26
75#define HI8561_SCREEN_MIPI_DSI_HFP 20
76#define HI8561_SCREEN_MIPI_DSI_VSYNC 2
77#define HI8561_SCREEN_MIPI_DSI_VBP 22
78#define HI8561_SCREEN_MIPI_DSI_VFP 200
79#define HI8561_SCREEN_DATA_LANE_NUM 2
80#define HI8561_SCREEN_LANE_BIT_RATE_MBPS 1000
81#define DISPLAY_WIDTH 540
82#define DISPLAY_HEIGHT 1168
84static uint16_t *drawBuffer =
nullptr;
85static const size_t pixel_count = DISPLAY_WIDTH * DISPLAY_HEIGHT;
86static const size_t buffer_size = pixel_count *
sizeof(uint16_t);
90esp_lcd_panel_handle_t panelDrv = NULL;
92static bool setupDisplay(esp_lcd_panel_handle_t *disp_panel);
97 Serial.print(
"Expander DigitalWrite: "); Serial.print(gpio & 0x7F); Serial.print(
" Level: "); Serial.println(
level);
100 Serial.print(
"GPIO DigitalWrite: "); Serial.print(gpio); Serial.print(
" Level: "); Serial.println(
level);
101 digitalWrite(gpio,
level);
108 Serial.print(
"Expander DigitalRead: "); Serial.print(gpio & 0x7F); Serial.println();
111 Serial.print(
"GPIO DigitalRead: "); Serial.print(gpio); Serial.println();
112 return digitalRead(gpio);
119 Serial.print(
"Expander PinMode: "); Serial.print(gpio & 0x7F); Serial.print(
" Mode: "); Serial.println(mode);
122 Serial.print(
"GPIO PinMode: "); Serial.print(gpio); Serial.print(
" Mode: "); Serial.println(mode);
129 Serial.begin(115200);
136 SensorWireHelper::dumpDevices(Wire);
142 Serial.println(
"Failed to find XL9555 - check your wiring!");
146 Serial.println(
"Sensor Expander Initialized");
159 esp_ldo_channel_handle_t ldo_channel_handle = NULL;
160 esp_ldo_channel_config_t ldo_channel_config = {
164 if (esp_ldo_acquire_channel(&ldo_channel_config, &ldo_channel_handle) != ESP_OK) {
165 Serial.println(
"Set LDO channel failed");
180 if (!setupDisplay(&panelDrv)) {
182 Serial.println(
"Failed to setup display - check your wiring!");
194 if (!
touch.
begin(Wire, HI8561_SLAVE_ADDRESS)) {
196 Serial.println(
"Failed to find HI8561 - check your wiring!");
200 Serial.println(
"Touch Driver Initialized");
202 Serial.print(
"Chip ID : 0x"); Serial.println(
touch.
getChipID(), HEX);
224 Serial.print(
"ID: ");
225 Serial.print(point.
id);
228 Serial.print(point.
x);
231 Serial.print(point.
y);
233 Serial.print(
"Pressure: ");
241 std::fill(drawBuffer, drawBuffer + pixel_count, random(0x0000, 0xFFFF));
242 esp_lcd_panel_draw_bitmap(panelDrv, 0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT, drawBuffer);
249static bool setupDisplay(esp_lcd_panel_handle_t *disp_panel)
251 esp_lcd_dsi_bus_handle_t mipi_dsi_bus;
252 esp_lcd_panel_io_handle_t mipi_dbi_io;
253 esp_lcd_dsi_bus_config_t bus_config = {
255 .num_data_lanes = HI8561_SCREEN_DATA_LANE_NUM,
256 .phy_clk_src = MIPI_DSI_PHY_PLLREF_CLK_SRC_DEFAULT_LEGACY,
257 .lane_bit_rate_mbps = HI8561_SCREEN_LANE_BIT_RATE_MBPS,
260 esp_err_t ret = esp_lcd_new_dsi_bus(&bus_config, &mipi_dsi_bus);
262 Serial.printf(
"esp_lcd_new_dsi_bus fail (error code: %#X)\n", ret);
265 esp_lcd_dbi_io_config_t dbi_io_config = {
266 .virtual_channel = 0,
270 ret = esp_lcd_new_panel_io_dbi(mipi_dsi_bus, &dbi_io_config, &mipi_dbi_io);
272 Serial.printf(
"esp_lcd_new_panel_io_dbi fail (error code: %#X)\n", ret);
275 esp_lcd_dpi_panel_config_t dpi_config = {
276 .virtual_channel = 0,
277 .dpi_clk_src = MIPI_DSI_DPI_CLK_SRC_DEFAULT,
278 .dpi_clock_freq_mhz = HI8561_SCREEN_MIPI_DSI_DPI_CLK_MHZ,
279 .pixel_format = LCD_COLOR_PIXEL_FORMAT_RGB565,
282 .h_size = HI8561_SCREEN_WIDTH,
283 .v_size = HI8561_SCREEN_HEIGHT,
284 .hsync_pulse_width = HI8561_SCREEN_MIPI_DSI_HSYNC,
285 .hsync_back_porch = HI8561_SCREEN_MIPI_DSI_HBP,
286 .hsync_front_porch = HI8561_SCREEN_MIPI_DSI_HFP,
287 .vsync_pulse_width = HI8561_SCREEN_MIPI_DSI_VSYNC,
288 .vsync_back_porch = HI8561_SCREEN_MIPI_DSI_VBP,
289 .vsync_front_porch = HI8561_SCREEN_MIPI_DSI_VFP,
295 hi8561_vendor_config_t vendor_config = {
297 .dsi_bus = mipi_dsi_bus,
298 .dpi_config = &dpi_config,
301 esp_lcd_panel_dev_config_t dev_config = {
302 .reset_gpio_num = -1,
303 .rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB,
304 .bits_per_pixel = 16,
305 .vendor_config = &vendor_config,
307 ret = esp_lcd_new_panel_hi8561(mipi_dbi_io, &dev_config, disp_panel);
309 Serial.printf(
"esp_lcd_new_panel_hi8561 fail (error code: %#X)\n", ret);
314 ret = esp_lcd_panel_init(*disp_panel);
316 Serial.printf(
"esp_lcd_panel_init fail (error code: %#X)\n", ret);
320 drawBuffer = (uint16_t*)heap_caps_malloc(
buffer_size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
322 Serial.println(
"Failed to allocate memory for draw buffer");
327 std::fill(drawBuffer, drawBuffer + pixel_count, random(0x0000, 0xFFFF));
329 esp_lcd_panel_draw_bitmap(panelDrv, 0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT, drawBuffer);
332 ledcAttach(P4_DISPLAY_BACKLIGHT, 200, 8);
333 ledcWrite(P4_DISPLAY_BACKLIGHT, 255);
341 Serial.begin(115200);
345 Serial.println(
"Sketch only works with LilyGo-T-Display-P4 TFT(HI8561) Version");
bool begin(SensorCommCustom::CustomCallback callback, uint8_t addr)
Initialize the sensor using custom callback interface.
bool digitalRead(uint8_t pin)
Read the digital value from an input pin.
void pinMode(uint8_t pin, uint8_t mode)
Set the direction of a GPIO pin.
void digitalWrite(uint8_t pin, bool value)
Write a digital value to an output pin.
Concrete driver for the XL9555 16‑bit I2C GPIO expander.
bool begin(SensorCommCustom::CustomCallback callback, SensorCommCustomHal::CustomHalCallback hal_callback, uint8_t addr) override
Initialize the touch driver for custom communication.
const TouchPoints & getTouchPoints() override
Get the touch points.
uint32_t getChipID() override
Get the chip ID.
HI8561 Touchscreen Display Chip Touch Class This class provides the touch reading functionality for t...
virtual void setGpioCallback(SensorHalCustom::CustomMode mode_cb, SensorHalCustom::CustomWrite write_cb, SensorHalCustom::CustomRead read_cb)
Set custom GPIO callbacks for pin control (e.g., reset, interrupt).
virtual void setPins(int rst, int irq)
Set the hardware reset and interrupt pin numbers.
Container for touch point data and optional gesture information.
uint8_t getPointCount() const
Returns the number of stored touch points.
TouchPoint & getPoint(uint8_t index)
Gets a reference to a touch point at the specified index.
bool hasPoints() const
Checks whether any touch points are stored.
#define IO_EXPANDER_TOUCH_IRQ
#define IO_EXPANDER_TOUCH_RST
IoExpanderPCA9570 expander
const uint16_t buffer_size
Represents a single touch point with its properties.
uint16_t pressure
Pressure value (0 if not supported by the chip).
uint8_t id
Touch point ID for multi‑touch tracking.
uint16_t x
X coordinate of the touch point.
uint16_t y
Y coordinate of the touch point.
void TouchDrvPinMode(uint8_t gpio, uint8_t mode)
void TouchDrvDigitalWrite(uint8_t gpio, uint8_t level)
uint8_t TouchDrvDigitalRead(uint8_t gpio)