XPT2046

Touch screen controller module for XPT2046 resistive touch screen controller.

NOTE: XPT2046 is pin and function compatible to ADS7843 touch screen controller.

Current ADS7843 touch screen controller module has challenges with touch screen and display having different size, with touch screen porches (overhanging the display), with alignment of touch screen and display, and with variation by manufacturers for the displays, touch screens, and carrier boards. The simplified calculation of x and y from touch raw data is fix built-in and there is no module (internal) calibration possible.

XPT2046 touch screen controller is based on ADS7843's logic for hardware handling, but allows customizing the externally accessible calculation function. To support a wide variety of customization from simple to complex to comprehensive, the integration of the function passes also the raw data AND the module. The customizable calculation function defines in the returned array what is passed to the callback. In other words, the module is a solid framework to handle the hardware events where as the calculation function is the glue to the application for delivering the desired values and can be adjusted to the needs.

The most obvious difference the ADS7843 module is the externalized calculation function to calculate actual x and y from raw values, value per pixel (scale) and offset to cover all aspects of linearity and offset. Touchscreen can extend beyond the display and therefore x and y can be negative or larger than maximum display size.

Last but not least, the controller chip has auxiliary ADC inputs to measure, for example, battery power, and it has also a built-in temperature sensor. Any of these features are not yet taken advantage of, but could be nicely tapped into with extensions.

Usage:

Landscape format:

SPI2.setup({sck:B13, miso:B14, mosi:B15, baud: 2000000});
var touch = require("XPT2046").connect(
  SPI2, B10, B1, function(x, y){
      if (x !== undefined) {
        console.log( x + " @ " + y);
      }
    }, function(yR, xR, d, m) { // portrait 240 x 320
        return [ //rawVal / valPerPx * offset
            Math.round(xR / -121.44  + 259.707)
          , Math.round(yR /   88.904 + -19.781)
          , d, m
          ];
    }).listen();

Swiping diagonally from top/left to bottom/right on a 240 x 320 portrait display w/ touch screen creates this output:

24 @ 30
45 @ 64
66 @ 108
100 @ 153
139 @ 203
171 @ 262
208 @ 308

Landscape format:

      ...  
      }, function(xR, yR, d, m) { // landscape 320 x 240
          return [ //rawVal / valPerPx + offset
              Math.round(xR /  -88.904 +  339.781)
            , Math.round(yR / -121.44  +  259.707)
      ...

Explained and with onInit():

var touch, touchModule = require("XPT2046");

function onInit() {
  // ...
  // setup touchscreen
  SPI2.setup({sck:B13, miso:B14, mosi:B15, baud: 2000000});
  touch = tMod.connect( // spi, cs, irq, callback, calc
      SPI2, B10, B1, function(x, y, rd, m) { // landscape input
          if (x !== undefined) {
            console.log(x + " @ " + y);
          }
      }, function(yR, xR, d, m) { // calc function (portraying, top/left=0/0)
      // calc function converts raw x / y to x / y coordinates;
      // scale and offset values are calculated using markers
      // and default calc function - values below work well for
      // common 2.2" 240x320 pixels 262K Color TFT LCD display w/ touchscreen
      // see http://forum.espruino.com/conversations/292641 - Touchscreen     
          return [ // next two lines are code templates for calculation
      //      Math.round(xr / xScale + xOff) // / x scale per pixel + x offset
      //    , Math.round(yr / yScale + yOff) // / y scale per pixel + y offset
              Math.round(xr / -121.44  + 259.707)
            , Math.round(yr /   88.904 + -19.781)
            , d, m // data, module
            ];
      }, 50).listen(); // scan interval on touch in milliseconds
  // ...
}

This page is auto-generated from GitHub. If you see any mistakes or have suggestions, please let us know.