SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
gt9895_get_point_lilygo_p4.ino
Go to the documentation of this file.
1
30#include <TouchDrv.hpp>
31#include <IoExpanderDrv.hpp>
32#include <SensorWireHelper.h>
33
34// Pin definitions for the LilyGo-P4
35#define P4_TOUCH_SDA 7
36#define P4_TOUCH_SCL 8
37#define P4_IO_EXPANDER_IRQ 5
38#define IO_EXPANDER_TOUCH_RST (3 | 0x80)
39#define IO_EXPANDER_TOUCH_IRQ (4 | 0x80)
40
41#ifdef ARDUINO_ESP32P4_DEV
42
45volatile bool ioExpanderInterruptTriggered = false;
46
47void TouchDrvDigitalWrite(uint8_t gpio, uint8_t level)
48{
49 if (gpio & 0x80) {
50 Serial.print("Expander DigitalWrite: "); Serial.print(gpio & 0x7F); Serial.print(" Level: "); Serial.println(level);
51 expander.digitalWrite(gpio & 0x7F, level);
52 } else {
53 Serial.print("GPIO DigitalWrite: "); Serial.print(gpio); Serial.print(" Level: "); Serial.println(level);
54 digitalWrite(gpio, level);
55 }
56}
57
58uint8_t TouchDrvDigitalRead(uint8_t gpio)
59{
60 if (gpio & 0x80) {
61 Serial.print("Expander DigitalRead: "); Serial.print(gpio & 0x7F); Serial.println();
62 return expander.digitalRead(gpio & 0x7F);
63 } else {
64 Serial.print("GPIO DigitalRead: "); Serial.print(gpio); Serial.println();
65 return digitalRead(gpio);
66 }
67}
68
69void TouchDrvPinMode(uint8_t gpio, uint8_t mode)
70{
71 if (gpio & 0x80) {
72 Serial.print("Expander PinMode: "); Serial.print(gpio & 0x7F); Serial.print(" Mode: "); Serial.println(mode);
73 expander.pinMode(gpio & 0x7F, mode);
74 } else {
75 Serial.print("GPIO PinMode: "); Serial.print(gpio); Serial.print(" Mode: "); Serial.println(mode);
76 pinMode(gpio, mode);
77 }
78}
79
80void setup()
81{
82 Serial.begin(115200);
83
84 while (!Serial);
85
86 Serial.println("Sketch only works with LilyGo-T-Display-P4 AMOLED(GT9895) Version");
87
88 Wire.begin(P4_TOUCH_SDA, P4_TOUCH_SCL);
89
90 // Scan I2C bus for devices
91 SensorWireHelper::dumpDevices(Wire);
92
93 // The LilyGo-P4 relies on an external I/O extender to control the power supply of peripheral devices.
94 // First, initialize the external extender.
95 if (!expander.begin(Wire, XL9555_SLAVE_ADDRESS0)) {
96 while (1) {
97 Serial.println("Failed to find io expander - check your wiring!");
98 delay(1000);
99 }
100 }
101 Serial.println("Sensor Expander Initialized");
102
103 // LilyGo-Display-P4 touch interrupt pin and touch reset pin aux to expander , use gpio custom callback
105
106 // Set touch interrupt and reset Pin
108
109 if (!touch.begin(Wire, GT9895_SLAVE_ADDRESS_L)) {
110 while (1) {
111 Serial.println("Failed to find touch device - check your wiring!");
112 delay(1000);
113 }
114 }
115
116 Serial.println("GT9895 Touch Initialized");
117
118 Serial.print("Chip ID : 0x"); Serial.println(touch.getChipID(), HEX);
119
120 // Sleep touch
121 // touch.sleep();
122
123 // int i = 10;
124 // while (i--) {
125 // Serial.print("Wake up after ");
126 // Serial.print(i);
127 // Serial.println(" seconds");
128 // delay(1000);
129 // }
130
131 // Wakeup touch
132 // touch.wakeup();
133
134 // Set the original touch panel resolution, LilyGo-P4 chip cannot automatically obtain the resolution;
135 // the original resolution must be manually set.
136 touch.setResolution(1060, 2400);
137
138 // Set the target resolution, and the scaling factor will be calculated automatically.
139 touch.setTargetResolution(568, 1232);
140
141 // Set the maximum coordinates ,used for mirroring touch coordinates
142 // touch.setMaxCoordinates(568, 1232);
143
144 // Set swap xy
145 // touch.setSwapXY(true);
146
147 // Set mirror xy
148 // touch.setMirrorXY(true, true);
149}
150
151void loop()
152{
153 /*
154 * Methods to increase the refresh rate:
155 * 1. Set the touch interrupt to a low level when touching, rather than a falling edge.
156 * Some are falling edge triggered, with a trigger period of about 10ms, depending on the firmware
157 * This requires the touch screen manufacturer to provide a firmware update, which is currently unavailable.
158 * 2. Use polling registers instead of interrupts, but this will consume CPU
159 */
160
161 // * TouchPoints is configured with a 5-point touch point buffer by default,
162 // * so it can return touch data from a maximum of 5 points. Although the GT9895
163 // * supports 10-point touch
164 TouchPoints touch_points = touch.getTouchPoints();
165 if (touch_points.hasPoints()) {
166 if (touch_points.hasPoints()) {
167 for (int i = 0; i < touch_points.getPointCount(); ++i) {
168 const TouchPoint &point = touch_points.getPoint(i);
169 Serial.print("ID: ");
170 Serial.print(point.id);
171 Serial.print(" ");
172 Serial.print("X: ");
173 Serial.print(point.x);
174 Serial.print(" ");
175 Serial.print("Y: ");
176 Serial.print(point.y);
177 Serial.print(" ");
178 Serial.print("Pressure: ");
179 Serial.print(point.pressure);
180 Serial.println();
181 }
182 Serial.println();
183 }
184 }
185 delay(50);
186}
187
188#else
189void setup()
190{
191 Serial.begin(115200);
192}
193void loop()
194{
195 Serial.println("Sketch only works with LilyGo-T-Display-P4 AMOLED(GT9895) Version");
196 delay(1000);
197}
198#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.
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 setResolution(uint16_t width, uint16_t height)
Set the raw resolution of the touch panel (physical maximum coordinates).
virtual void setPins(int rst, int irq)
Set the hardware reset and interrupt pin numbers.
virtual void setTargetResolution(uint16_t width, uint16_t height)
Set the target display resolution to map touch coordinates to screen pixels.
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
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)