SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
hi8561_get_point_lilygo_p4.ino
Go to the documentation of this file.
1
36#include <Arduino.h>
37#include <TouchDrv.hpp>
38
39#ifdef ARDUINO_ESP32P4_DEV
40
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."
43#endif
44
45#include <IoExpanderDrv.hpp>
46#include <SensorWireHelper.h>
47#include "esp_lcd_mipi_dsi.h"
48#include "esp_lcd_panel_ops.h"
49#include "hi8561_driver.h"
50#include "esp_ldo_regulator.h"
51#include <algorithm>
52
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."
55#endif
56
57// Pin definitions for the LilyGo-P4
58#define P4_TOUCH_SDA 7
59#define P4_TOUCH_SCL 8
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
68
69// HI8561 display parameters
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
83
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);
87
90esp_lcd_panel_handle_t panelDrv = NULL;
91
92static bool setupDisplay(esp_lcd_panel_handle_t *disp_panel);
93
94void TouchDrvDigitalWrite(uint8_t gpio, uint8_t level)
95{
96 if (gpio & 0x80) {
97 Serial.print("Expander DigitalWrite: "); Serial.print(gpio & 0x7F); Serial.print(" Level: "); Serial.println(level);
98 expander.digitalWrite(gpio & 0x7F, level);
99 } else {
100 Serial.print("GPIO DigitalWrite: "); Serial.print(gpio); Serial.print(" Level: "); Serial.println(level);
101 digitalWrite(gpio, level);
102 }
103}
104
105uint8_t TouchDrvDigitalRead(uint8_t gpio)
106{
107 if (gpio & 0x80) {
108 Serial.print("Expander DigitalRead: "); Serial.print(gpio & 0x7F); Serial.println();
109 return expander.digitalRead(gpio & 0x7F);
110 } else {
111 Serial.print("GPIO DigitalRead: "); Serial.print(gpio); Serial.println();
112 return digitalRead(gpio);
113 }
114}
115
116void TouchDrvPinMode(uint8_t gpio, uint8_t mode)
117{
118 if (gpio & 0x80) {
119 Serial.print("Expander PinMode: "); Serial.print(gpio & 0x7F); Serial.print(" Mode: "); Serial.println(mode);
120 expander.pinMode(gpio & 0x7F, mode);
121 } else {
122 Serial.print("GPIO PinMode: "); Serial.print(gpio); Serial.print(" Mode: "); Serial.println(mode);
123 pinMode(gpio, mode);
124 }
125}
126
127void setup()
128{
129 Serial.begin(115200);
130
131 while (!Serial);
132
133 Wire.begin(P4_TOUCH_SDA, P4_TOUCH_SCL);
134
135 // Scan I2C bus for devices
136 SensorWireHelper::dumpDevices(Wire);
137
138 // The LilyGo-P4 relies on an external I/O extender to control the power supply of peripheral devices.
139 // First, initialize the external extender.
140 if (!expander.begin(Wire, XL9555_SLAVE_ADDRESS0)) {
141 while (1) {
142 Serial.println("Failed to find XL9555 - check your wiring!");
143 delay(1000);
144 }
145 }
146 Serial.println("Sensor Expander Initialized");
147
148 // Enable power supply for T-Display-P4 peripherals
149 expander.pinMode(IO_EXPANDER_P4_VCCA_POWER_EN, OUTPUT);
150 expander.digitalWrite(IO_EXPANDER_P4_VCCA_POWER_EN, LOW);
151
152 expander.pinMode(IO_EXPANDER_5V0_POWER_EN, OUTPUT);
153 expander.digitalWrite(IO_EXPANDER_5V0_POWER_EN, HIGH);
154
155 expander.pinMode(IO_EXPANDER_3V3_POWER_EN, OUTPUT);
156 expander.digitalWrite(IO_EXPANDER_3V3_POWER_EN, LOW);
157
158 // Configure LDO for 1.8V output
159 esp_ldo_channel_handle_t ldo_channel_handle = NULL;
160 esp_ldo_channel_config_t ldo_channel_config = {
161 .chan_id = 3,
162 .voltage_mv = 2500,
163 };
164 if (esp_ldo_acquire_channel(&ldo_channel_config, &ldo_channel_handle) != ESP_OK) {
165 Serial.println("Set LDO channel failed");
166 return;
167 }
168
169 // Reset the display
170 expander.pinMode(IO_EXPANDER_DISP_RST, OUTPUT);
171 expander.digitalWrite(IO_EXPANDER_DISP_RST, HIGH);
172 delay(20);
173 expander.digitalWrite(IO_EXPANDER_DISP_RST, LOW);
174 delay(60);
175 expander.digitalWrite(IO_EXPANDER_DISP_RST, HIGH);
176 delay(20);
177
178
179 // The screen must be initialized and made functional before touch initialization can proceed.
180 if (!setupDisplay(&panelDrv)) {
181 while (1) {
182 Serial.println("Failed to setup display - check your wiring!");
183 delay(1000);
184 }
185 }
186
187 // LilyGo-Display-P4 touch interrupt pin and touch reset pin aux to expander , use gpio custom callback
189
190 // Set touch interrupt and reset Pin
192
193 // Screen must be initialized first before calling this class for initialization.
194 if (!touch.begin(Wire, HI8561_SLAVE_ADDRESS)) {
195 while (1) {
196 Serial.println("Failed to find HI8561 - check your wiring!");
197 delay(1000);
198 }
199 }
200 Serial.println("Touch Driver Initialized");
201
202 Serial.print("Chip ID : 0x"); Serial.println(touch.getChipID(), HEX);
203
204 // Set the maximum coordinates ,used for mirroring touch coordinates
205 // touch.setMaxCoordinates(568, 1232);
206
207 // Set swap xy
208 // touch.setSwapXY(true);
209
210 // Set mirror xy
211 // touch.setMirrorXY(true, true);
212}
213
214void loop()
215{
216
217 // * TouchPoints is configured with a 5-point touch point buffer by default,
218 // * so it can return touch data from a maximum of 5 points. Although the HI8561
219 // * supports 10-point touch
220 TouchPoints touch_points = touch.getTouchPoints();
221 if (touch_points.hasPoints()) {
222 for (int i = 0; i < touch_points.getPointCount(); ++i) {
223 const TouchPoint &point = touch_points.getPoint(i);
224 Serial.print("ID: ");
225 Serial.print(point.id);
226 Serial.print(" ");
227 Serial.print("X: ");
228 Serial.print(point.x);
229 Serial.print(" ");
230 Serial.print("Y: ");
231 Serial.print(point.y);
232 Serial.print(" ");
233 Serial.print("Pressure: ");
234 Serial.print(point.pressure);
235 Serial.println();
236 }
237
238 Serial.println();
239
240 // When touch points are detected, fill the draw buffer with random colors
241 std::fill(drawBuffer, drawBuffer + pixel_count, random(0x0000, 0xFFFF));
242 esp_lcd_panel_draw_bitmap(panelDrv, 0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT, drawBuffer);
243
244 }
245 delay(50);
246}
247
248
249static bool setupDisplay(esp_lcd_panel_handle_t *disp_panel)
250{
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 = {
254 .bus_id = 0,
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,
258 };
259
260 esp_err_t ret = esp_lcd_new_dsi_bus(&bus_config, &mipi_dsi_bus);
261 if (ret != ESP_OK) {
262 Serial.printf("esp_lcd_new_dsi_bus fail (error code: %#X)\n", ret);
263 return false;
264 }
265 esp_lcd_dbi_io_config_t dbi_io_config = {
266 .virtual_channel = 0,
267 .lcd_cmd_bits = 8,
268 .lcd_param_bits = 8,
269 };
270 ret = esp_lcd_new_panel_io_dbi(mipi_dsi_bus, &dbi_io_config, &mipi_dbi_io);
271 if (ret != ESP_OK) {
272 Serial.printf("esp_lcd_new_panel_io_dbi fail (error code: %#X)\n", ret);
273 return false;
274 }
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,
280 .num_fbs = 0,
281 .video_timing = {
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,
290 },
291 .flags = {
292 .use_dma2d = true,
293 }
294 };
295 hi8561_vendor_config_t vendor_config = {
296 .mipi_config = {
297 .dsi_bus = mipi_dsi_bus,
298 .dpi_config = &dpi_config,
299 },
300 };
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,
306 };
307 ret = esp_lcd_new_panel_hi8561(mipi_dbi_io, &dev_config, disp_panel);
308 if (ret != ESP_OK) {
309 Serial.printf("esp_lcd_new_panel_hi8561 fail (error code: %#X)\n", ret);
310 return false;
311 }
312
313 // Initialize the display panel
314 ret = esp_lcd_panel_init(*disp_panel);
315 if (ret != ESP_OK) {
316 Serial.printf("esp_lcd_panel_init fail (error code: %#X)\n", ret);
317 }
318
319 // Allocate memory for the draw buffer
320 drawBuffer = (uint16_t*)heap_caps_malloc(buffer_size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
321 if (!drawBuffer) {
322 Serial.println("Failed to allocate memory for draw buffer");
323 while (1);
324 }
325
326 // Fill the draw buffer with random colors
327 std::fill(drawBuffer, drawBuffer + pixel_count, random(0x0000, 0xFFFF));
328
329 esp_lcd_panel_draw_bitmap(panelDrv, 0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT, drawBuffer);
330
331 // Enable backlight
332 ledcAttach(P4_DISPLAY_BACKLIGHT, 200, 8);
333 ledcWrite(P4_DISPLAY_BACKLIGHT, 255);
334
335 return true;
336}
337
338#else
339void setup()
340{
341 Serial.begin(115200);
342}
343void loop()
344{
345 Serial.println("Sketch only works with LilyGo-T-Display-P4 TFT(HI8561) Version");
346 delay(1000);
347}
348#endif
@license MIT License
@license MIT License
@license MIT License
uint8_t level
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 P4_TOUCH_SCL
#define IO_EXPANDER_TOUCH_IRQ
#define P4_TOUCH_SDA
#define IO_EXPANDER_TOUCH_RST
IoExpanderPCA9570 expander
uint8_t i
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.
TouchDrvCSTXXX touch
Definition touch_drv.cpp:42
void TouchDrvPinMode(uint8_t gpio, uint8_t mode)
void TouchDrvDigitalWrite(uint8_t gpio, uint8_t level)
uint8_t TouchDrvDigitalRead(uint8_t gpio)