Last active
July 19, 2019 21:12
-
-
Save CelliesProjects/99a56121e7c8a7cb00d5b46270e74b75 to your computer and use it in GitHub Desktop.
Arduino IDE example for ESP32 showing the mapping of XPT2046 touch coordinates to ILI9341 tft coordinates.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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 /* 10K pull-up */ | |
#define _miso 39 | |
#define TFT_CS 4 /* 10K pull-up */ | |
#define TFT_RST 12 | |
#define TFT_BACKLIGHT_PIN 2 /* -via transistor- */ | |
#define TOUCH_CS_PIN 33 /* 10K pull-up */ | |
#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() { | |
//if ( touch.touched() ) | |
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( mapFloat( rawLocation.x, 340, 3900, 0, 320 ), | |
mapFloat( rawLocation.y, 200, 3850, 0, 240 ), | |
ILI9341_GREEN ); | |
} | |
if ( TFT_ORIENTATION == TFT_NORMAL ) | |
{ | |
tft.drawPixel( mapFloat( rawLocation.x, 340, 3900, 320, 0 ), | |
mapFloat( rawLocation.y, 200, 3850, 240, 0 ), | |
ILI9341_GREEN ); | |
} | |
} | |
//tft.fillScreen(ILI9341_BLACK); | |
} | |
static inline __attribute__((always_inline)) float mapFloat( float x, const float in_min, const float in_max, const float out_min, const float out_max) | |
{ | |
return ( x - in_min ) * ( out_max - out_min ) / ( in_max - in_min ) + out_min; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment