#define SW_VERS "v1.0"
// TARGET
// Arduino Mega or Mega 2560
// DEBUG Const
#define DEBUG true //set to true for debug output, false for no debug output
#define DEBUG_SERIAL \
if (DEBUG) Serial
// Add HW target (eventually boards installed)
/* Rui Santos & Sara Santos - Random Nerd Tutorials
THIS EXAMPLE WAS TESTED WITH THE FOLLOWING HARDWARE:
1) ESP32-2432S028R 2.8 inch 240×320 also known as the Cheap Yellow Display (CYD): https://makeradvisor.com/tools/cyd-cheap-yellow-display-esp32-2432s028r/
SET UP INSTRUCTIONS: https://RandomNerdTutorials.com/cyd/
2) REGULAR ESP32 Dev Board + 2.8 inch 240x320 TFT Display: https://makeradvisor.com/tools/2-8-inch-ili9341-tft-240x320/ and https://makeradvisor.com/tools/esp32-dev-board-wi-fi-bluetooth/
SET UP INSTRUCTIONS: https://RandomNerdTutorials.com/esp32-tft/
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/
#include <SPI.h>
/* Install the "TFT_eSPI" library by Bodmer to interface with the TFT Display - https://github.com/Bodmer/TFT_eSPI
*** IMPORTANT: User_Setup.h available on the internet will probably NOT work with the examples available at Random Nerd Tutorials ***
*** YOU MUST USE THE User_Setup.h FILE PROVIDED IN THE LINK BELOW IN ORDER TO USE THE EXAMPLES FROM RANDOM NERD TUTORIALS ***
FULL INSTRUCTIONS AVAILABLE ON HOW CONFIGURE THE LIBRARY: https://RandomNerdTutorials.com/cyd/ or https://RandomNerdTutorials.com/esp32-tft/ */
#include <TFT_eSPI.h>
// Install the "XPT2046_Touchscreen" library by Paul Stoffregen to use the Touchscreen - https://github.com/PaulStoffregen/XPT2046_Touchscreen
// Note: this library doesn't require further configuration
#include <XPT2046_Touchscreen.h>
// Add External Library
#include <TFT_eWidget.h> // Widget library
// Pin assignment
// Touchscreen pins
#define XPT2046_IRQ 36 // T_IRQ
#define XPT2046_MOSI 32 // T_DIN
#define XPT2046_MISO 39 // T_OUT
#define XPT2046_CLK 25 // T_CLK
#define XPT2046_CS 33 // T_CS
// HW Constants
#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 240
// SW Constants
#define FONT_SIZE 2
// HW Global variables
TFT_eSPI tft = TFT_eSPI();
SPIClass touchscreenSPI = SPIClass(VSPI);
XPT2046_Touchscreen touchscreen(XPT2046_CS, XPT2046_IRQ);
// SW Global variables
GraphWidget gr = GraphWidget(&tft); // Graph widget gr instance with pointer to tft
TraceWidget tr1 = TraceWidget(&gr); // Graph trace 1
TraceWidget tr2 = TraceWidget(&gr); // Graph trace 2
const float gxLow = 0.0;
const float gxHigh = 100.0;
const float gyLow = -512.0;
const float gyHigh = 512.0;
// Touchscreen coordinates: (x, y) and pressure (z)
int x, y, z;
// Add Internal Library
#include <TermoAcquario.h>
void setup() {
Serial.begin(115200);
// Start the SPI for the touchscreen and init the touchscreen
touchscreenSPI.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS);
touchscreen.begin(touchscreenSPI);
// Set the Touchscreen rotation in landscape mode
// Note: in some displays, the touchscreen might be upside down, so you might need to set the rotation to 3: touchscreen.setRotation(3);
touchscreen.setRotation(3);
// // Start the tft display
// tft.init();
// // Set the TFT display rotation in landscape mode
// tft.setRotation(1);
// // Clear the screen before writing to it
// tft.fillScreen(TFT_WHITE);
// tft.setTextColor(TFT_BLACK, TFT_WHITE);
// // Set X and Y coordinates for center of display
// int centerX = SCREEN_WIDTH / 2;
// int centerY = SCREEN_HEIGHT / 2;
// tft.drawCentreString("Hello, world!", centerX, 30, FONT_SIZE);
// tft.drawCentreString("Touch screen to test", centerX, centerY, FONT_SIZE);
tft.begin();
tft.setRotation(3);
tft.fillScreen(TFT_BLACK);
// Graph area is 200 pixels wide, 150 high, dark grey background
gr.createGraph(200, 150, tft.color565(5, 5, 5));
// x scale units is from 0 to 100, y scale units is -50 to 50
gr.setGraphScale(0.0, 100.0, -50.0, 50.0);
// X grid starts at 0 with lines every 10 x-scale units
// Y grid starts at -50 with lines every 25 y-scale units
// blue grid
gr.setGraphGrid(0.0, 10.0, -50.0, 25.0, TFT_BLUE);
// Draw empty graph, top left corner at 40,10 on TFT
gr.drawGraph(40, 10);
// Start a trace with using red and another with green
tr1.startTrace(TFT_RED);
tr2.startTrace(TFT_GREEN);
// Add points on graph to trace 1 using graph scale factors
tr1.addPoint(0.0, 0.0);
tr1.addPoint(100.0, 0.0);
// Add points on graph to trace 2 using graph scale factors
// Points are off graph so the plotted line is clipped to graph area
tr2.addPoint(0.0, -100.0);
tr2.addPoint(100.0, 100.0);
// Get x,y pixel coordinates of any scaled point on graph
// and ring that point.
tft.drawCircle(gr.getPointX(50.0), gr.getPointY(0.0), 5, TFT_MAGENTA);
// Draw the x axis scale
tft.setTextDatum(TC_DATUM); // Top centre text datum
tft.drawNumber(0, gr.getPointX(0.0), gr.getPointY(-50.0) + 3);
tft.drawNumber(50, gr.getPointX(50.0), gr.getPointY(-50.0) + 3);
tft.drawNumber(100, gr.getPointX(100.0), gr.getPointY(-50.0) + 3);
// Draw the y axis scale
tft.setTextDatum(MR_DATUM); // Middle right text datum
tft.drawNumber(-50, gr.getPointX(0.0), gr.getPointY(-50.0));
tft.drawNumber(0, gr.getPointX(0.0), gr.getPointY(0.0));
tft.drawNumber(50, gr.getPointX(0.0), gr.getPointY(50.0));
// Restart traces with new colours
tr1.startTrace(TFT_WHITE);
tr2.startTrace(TFT_YELLOW);
}
void loop() {
static uint32_t plotTime = millis();
static float gx = 0.0, gy = 0.0;
static float delta = 7.0;
// Sample periodically
if (millis() - plotTime >= 100) {
plotTime = millis();
// Add a new point on each trace
tr1.addPoint(gx, gy);
tr2.addPoint(gx, gy/2.0); // half y amplitude
// Create next plot point
gx += 1.0;
gy += delta;
if (gy > 70.0) { delta = -7.0; gy = 70.0; }
if (gy < -70.0) { delta = 7.0; gy = -70.0; }
// If the end of the graph is reached start 2 new traces
if (gx > 100.0) {
gx = 0.0;
gy = 0.0;
// Draw empty graph at 40,10 on display
gr.drawGraph(40, 10);
// Start new trace
tr1.startTrace(TFT_GREEN);
tr2.startTrace(TFT_YELLOW);
}
}
// // Checks if Touchscreen was touched, and prints X, Y and Pressure (Z) info on the TFT display and Serial Monitor
// if (touchscreen.tirqTouched() && touchscreen.touched()) {
// // Get Touchscreen points
// TS_Point p = touchscreen.getPoint();
// // Calibrate Touchscreen points with map function to the correct width and height
// x = map(p.x, 200, 3700, 1, SCREEN_WIDTH);
// y = map(p.y, 240, 3800, 1, SCREEN_HEIGHT);
// z = p.z;
// printTouchToSerial(x, y, z);
// printTouchToDisplay(x, y, z);
// delay(100);
// }
}Loading
ili9341-cap-touch
ili9341-cap-touch