Arduino example: XPT2046 touch and ILI9341 tft.

Cellie
Posts: 37
Joined: Sat Feb 27, 2016 9:47 pm

Arduino example: XPT2046 touch and ILI9341 tft.

Postby Cellie » Sun Nov 26, 2017 6:05 pm

Simple etch-a-sketch for ESP32 and a SPI ILI9341 tft board with XPT2046 touch controller from ebay etc.

Uses the Adafruit ILI9341 library as the ILI9341_t3 does not compile for me.

Code: Select all

/* Map XPT2046 input to ILI9341 320x240 raster */
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <XPT2046_Touchscreen.h> /* https://github.com/PaulStoffregen/XPT2046_Touchscreen */

#define TFT_DC             27
#define _sclk              25
#define _mosi              32
#define _miso              39
#define TFT_CS              4
#define TFT_RST            12
#define TFT_BACKLIGHT_PIN   2 /* -via transistor- */
#define TOUCH_CS_PIN       33
#define TOUCH_IRQ_PIN      35

#define TFT_NORMAL          1
#define TFT_UPSIDEDOWN      3

const uint8_t TFT_ORIENTATION = TFT_NORMAL;

Adafruit_ILI9341 tft = Adafruit_ILI9341( TFT_CS, TFT_DC, TFT_RST );

XPT2046_Touchscreen touch( TOUCH_CS_PIN, TOUCH_IRQ_PIN );

void setup() {
  Serial.begin( 115200 );
  SPI.begin( _sclk, _miso, _mosi );
  SPI.setFrequency( 60000000 );

  tft.begin( 10000000, SPI );

  tft.setRotation( TFT_ORIENTATION );
  tft.fillScreen( ILI9341_BLACK );
  tft.setTextColor( ILI9341_GREEN, ILI9341_BLACK );

  pinMode( TFT_BACKLIGHT_PIN, OUTPUT );
  digitalWrite( TFT_BACKLIGHT_PIN, HIGH );

  touch.begin();
  Serial.println( "Touch screen ready." );
}


TS_Point rawLocation;

void loop() {
    while ( touch.touched() )
    {
      rawLocation = touch.getPoint();
      /*
        tft.setCursor( 100, 150 );
        tft.print( "X = " );
        tft.print( rawLocation.x );
        tft.setCursor(100, 180);
        tft.print( "Y = " );
        tft.print( rawLocation.y );

        Serial.print("x = ");
        Serial.print(rawLocation.x);
        Serial.print(", y = ");
        Serial.print(rawLocation.y);
        Serial.print(", z = ");
        Serial.println(rawLocation.z);
      */
      if ( TFT_ORIENTATION == TFT_UPSIDEDOWN )
      {
        tft.drawPixel( map( rawLocation.x, 340, 3900, 0, 320 ),
                       map( rawLocation.y, 200, 3850, 0, 240 ),
                       ILI9341_GREEN );
      }
      if ( TFT_ORIENTATION == TFT_NORMAL )
      {
        tft.drawPixel( map( rawLocation.x, 340, 3900, 320, 0 ),
                       map( rawLocation.y, 200, 3850, 240, 0 ),
                       ILI9341_GREEN );
      }
    }
  //tft.fillScreen(ILI9341_BLACK);
}

Who is online

Users browsing this forum: No registered users and 2 guests