Giter Club home page Giter Club logo

xpt2046_touchscreen's Introduction

XPT2046 Touchscreen Arduino Library

XPT2046_Touchscreen is a library for the XPT2046 resistive touchscreen controllers used on many low cost TFT displays.

ILI9431Test Example Program

Setup Functions

First, create an instance of the library for your touchscreen. The digital pin used for chip select is required. The normal MISO, MOSI and SCK pins will be used automatically.

#define CS_PIN  8
XPT2046_Touchscreen ts(CS_PIN);

The use of the Touch interrupt pin can be optionally specified. If the Teensy pin specified is actively connected to the T_IRQ display pin then the normal touch calls will respond, but can be called more often as each call returns without hardware access when no interrupt was recorded.

#define TIRQ_PIN  2
XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN);

In setup(), use the begin() function to initialize the touchscreen, and optionally use setRotation(n), where n is 0 to 3, matching the rotation setting in ILI9341_t3, Adafruit_ILI9341 or other Adafruit compatible TFT libraries.

  ts.begin();
  ts.setRotation(1);

Reading Touch Info

The touched() function tells if the display is currently being touched, returning true or false.

  if (ts.touched()) {
    // do something....
  }

You can read the touch coordinates with readData()

  uint16_t x, y, z;
  ts.readData(&x, &y, &z);

or with getPoint(), which returns a TS_Point object:

  TS_Point p = ts.getPoint();
  Serial.print("x = ");
  Serial.print(p.x);
  Serial.print(", y = ");
  Serial.print(p.y);

The Z coordinate represents the amount of pressure applied to the screen.

Reading ADCs Info

The XPT2046 is loaded with different ADC inputs thats can be read

  • VBat input with an internal voltage divider 1:4, so you can input 0.125V to 6V without issues.
  • AuxIn you can input 0.125V to 1.5V
  • Temp/Temp0 are used for ambient temperature. The resolution is 1.6°C per bit and fluctuates a lot (+/- 3°C), but gives a general idea.

Using the following functions, you can read the ADCs and they'll return a float:

  Serial.println(ts.getVBat());
  Serial.println(ts.getAuxIn());
  Serial.println(ts.getTemp());
  Serial.println(ts.getTempF());

The following function allows you to read the raw ADC results, pass either ADC_VBAT,ADC_AUXIN or ADC_TEMP to return an int16_t: Serial.println(ts.updateADC(adc);

Adafruit Library Compatibility

XPT2046_Touchscreen is meant to be a compatible with sketches written for Adafruit_STMPE610, offering the same functions, parameters and numerical ranges as Adafruit's library.

Using The Interrupt Pin : Built in support when connected nothing else is needed. When specified as above

no SPI calls are made unless a Touch was detected. On normal connections - this means the Teensy LED won't blink on every touch query.

Using The Interrupt Pin : Custom use would preclude the normal built in usage. The warning below is justified.

The XPT2046 chip has an interrupt output, which is typically labeled T_IRQ on many low cost TFT displays. No special software support is needed in this library. The interrupt pin always outputs a digital signal related to the touch controller signals, which is LOW when the display is touched. It also is driven low while software reads the touch position.

The interrupt can be used as a wakeup signal, if you put your microcontroller into a deep sleep mode. Normally, you would stop reading the touch data, then enable the interrupt pin with attachInterrupt(), and then configure your processor to wake when the interrupt occurs, before enter a deep sleep mode. Upon waking, you would normally disable the interrupt before reading the display, to prevent false interrupts caused by the process of reading touch positions.

You can also use the interrupt to respond to touch events. Setup might look similar to this:

  SPI.usingInterrupt(digitalPinToInterrupt(pin))
  attachInterrupt(digitalPinToInterrupt(pin), myFunction, FALLING);

However, inside your interrupt function, if the display is no longer being touched, any attempt to read the touch position will cause the interrupt pin to create another falling edge. This can lead to an infinite loop of falsely triggered interrupts. Special care is needed to avoid triggering more interrupts on the low signal due to reading the touch position.

For most applications, regularly reading the touch position from the main program is much simpler.

xpt2046_touchscreen's People

Contributors

celliesprojects avatar defragster avatar garethhcoleman avatar ivankravets avatar kurte avatar paulstoffregen avatar xxxajk avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.