LCDGFX LCD display driver  1.2.0
Lightweight graphics library for SSD1306, SSD1325, SSD1327, SSD1331, SSD1351, SH1106, SH1107, IL9163, ST7735, ST7789, ILI9341, PCD8544 displays over I2C/SPI
docs/touch.md
1 # Touch input (XPT2046)
2 
3 `lcdgfx` v1.3.0 ships a templated XPT2046 resistive-touch driver in
4 `src/touch/xpt2046.h`. The driver is transport-agnostic: any class
5 exposing `begin()`, `end()`, and `transfer(uint8_t)` works.
6 
7 ## Wiring
8 
9 | XPT2046 | MCU pin (typical) |
10 |---------|-------------------|
11 | `T_CLK` | SPI SCK |
12 | `T_DIN` | SPI MOSI |
13 | `T_DO` | SPI MISO |
14 | `T_CS` | any GPIO |
15 | `T_IRQ` | any GPIO (optional, falling-edge) |
16 
17 `T_CS` may safely be the same physical CS as a non-XPT slave on the
18 same bus as long as you select only one device at a time. The driver
19 uses 2.5 MHz SPI mode 0.
20 
21 ## Minimal example
22 
23 ```cpp
24 #include "lcdgfx_xpt2046.h" // pulled in by lcdgfx_gui.h
25 
26 LcdGfxXpt2046<SPI> touch(/*cs*/ 8, /*irq*/ 9);
27 
28 void setup() {
29  touch.begin();
30  touch.setCalibration({ /*ax*/ 0.072f, /*bx*/ -16.0f,
31  /*ay*/ 0.085f, /*by*/ -12.0f,
32  /*swap*/ false, /*invert_x*/ false,
33  /*invert_y*/ false,
34  /*w*/ 240, /*h*/ 320 });
35 }
36 
37 void loop() {
38  if (touch.isPressed()) {
39  int16_t x, y;
40  if (touch.read(x, y)) menu.onTouch(x, y);
41  }
42 }
43 ```
44 
45 `TouchCalibration` performs a per-axis affine transform (`x' =
46 ax * raw + bx`) followed by optional axis swap, axis invert, and
47 output clamping. A 3-point or 5-point calibration helper is *not*
48 shipped — pick three on-screen targets, record the raw samples, and
49 solve for `ax/bx`.
50 
51 See `examples/gui/touch_demo` for a full sketch.