Touch Input (TFT carrier)
The GT911 capacitive touch controller on the TFT carrier and the GT911 library that reads it.
The TFT carrier, an OV5640 carrier, has a panel that includes a Goodix GT911 capacitive touch controller on the shared I2C bus. The GT911 library polls the controller, reports up to five touch points in panel coordinates, and provides a rectangle hit test.
#include <Wire.h>
#include <GT911.h>
GT911 touch;
void setup() {
Wire.begin();
if (!touch.begin()) Serial.println("no touch panel found");
}
void loop() {
touch.read();
if (touch.clicked(20, 400, 120, 48)) {
// the rectangle at (20, 400), 120 by 48, was touched this frame
}
}Reading points
read() polls the controller and returns the number of active points. x(i), y(i), size(i) and id(i) return the properties of point i, from 0 to 4. Without an index, x() and y() return point 0.
read() retains the previous points when the controller has no new sample, so a stationary finger remains reported. Polling faster than the panel samples repeats the same position.
Hit testing
| Method | Behaviour |
|---|---|
pressed(x, y, w, h) | True while any finger is within the rectangle |
clicked(x, y, w, h) | True once, on the frame a touch begins within the rectangle |
clicked() is the appropriate test for buttons; pressed() retriggers on every frame a finger rests. clicked() is a const test and the edge state is held by read(), so several rectangles can be tested in one frame without the first consuming the event.
Buttons are drawn on the overlay with the same rectangle used in the test. See The On-board Display. The face recognition example uses a drawn rectangle labelled REMEMBER as an enrolment button.
Coordinate mapping
On the TFT carrier the default mapping is correct: 800x480, no axis swap, no flips. Measured on the panel, the top left corner reads approximately (15, 26), the top right (788, 56) and the bottom left (5, 475). setMapping() is not required.
setMapping(width, height, swap_xy, flip_x, flip_y) supports other panels or rotated mounts. The example File > Examples > GT911 > CalibrateTouch prompts for three corners, derives the swap and flips, and prints the setMapping call to paste into a sketch. Three corners are used because the top left and top right corners isolate the X axis and reveal a swap, whereas two opposite corners cannot distinguish a swap from a double flip. The example uses no camera and no display.
Hardware notes
Reset. The GT911's RESET line is tied to the system reset. The controller selects its I2C address from the state of its INT line during reset release, and that sequence cannot be driven from software on this carrier. begin() therefore probes both 0x5D and 0x14 and verifies the chip ID.
Polling. The driver polls rather than using the INT line, so it operates regardless of the trigger mode in the panel's flashed configuration. INT (PD9) is readable through intAsserted().
Diagnostics
chipId() returns the ID string, address() the responding I2C address (zero if none), and raw(i, buffer) copies the eight raw bytes of a point. The bench sketch DisplayTouchBringUp combines the panel and touch controller in one test.