//Using KurtE's Async DMA ILI9341 library. 240Mhz high speed test.
//Enabled Touch Screen
#include "SPI.h"
#include <ILI9341_t3n.h>
#include <XPT2046_Touchscreen.h>
#define F_BUS 120000000
#define ILI9341_SPICLOCK 60000000
#define TFT_DC 9
#define TFT_CS 10
#define TFT_RST 8 // 255 = unused, connect to 3.3V
#define TFT_MOSI 11
#define TFT_SCLK 13
#define TFT_MISO 12
#define CS_PIN 6 //Touch CS
#define TIRQ_PIN 3 //Touch Interupt pin
float WattHours = 100.000;
//uint16_t tft_frame_buffer[ILI9341_TFTWIDTH * ILI9341_TFTHEIGHT];
DMAMEM uint16_t ili9341_frame_buffer[ILI9341_TFTWIDTH*ILI9341_TFTHEIGHT];
ILI9341_t3n tft = ILI9341_t3n(TFT_CS, TFT_DC, TFT_RST, TFT_MOSI, TFT_SCLK, TFT_MISO, &SPIN);
XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN); // Param 2 - Touch IRQ Pin - interrupt enabled polling
void setup() {
tft.begin();
ts.begin();
tft.setFrameBuffer(ili9341_frame_buffer);
tft.useFrameBuffer(true);
tft.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_YELLOW);
tft.setTextSize(2);
tft.println("Waiting for Arduino Serial Monitor...");
tft.updateScreenAsync();
Serial.begin(9600);
while (!Serial) ; // wait for Arduino Serial Monitor
Serial.println("ILI9341 Test!");
Serial.print("F_CPU ");
Serial.print(F_CPU);
Serial.println(" MHZ");
Serial.print("F_BUS ");
Serial.print(F_BUS);
Serial.println(" MHZ");
Serial.print("SPICLOCK ");
Serial.print(ILI9341_SPICLOCK);
Serial.println(" MHZ");
tft.waitUpdateAsyncComplete();
tft.println("Connected!");
tft.updateScreenAsync();
delay(5000);
}
elapsedMillis drawtimer;
elapsedMillis TouchTimer;
elapsedMicros drawtime;
elapsedMicros touchtime;
uint32_t drawtimelast;
uint32_t touchtimelast;
byte debugmenu = 0;
uint16_t posx = 0;
uint16_t posy = 0;
byte touchme = 0;
void loop(void) {
if(debugmenu == 0){
tft.waitUpdateAsyncComplete() ;
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setTextSize(3); //Upper Left Side
tft.setCursor(3, 3);
tft.print("Q1");
tft.setCursor(3, 27);
tft.print("Q2");
tft.setCursor(3, 51);
tft.print("Q3");
tft.setCursor(3, 75);
tft.print("Q4");
tft.setCursor(3, 99);
tft.print("Q5");
tft.setTextSize(4); //Upper Right Side
tft.setCursor(295, 3);
tft.print("V");
tft.setCursor(295, 36);
tft.print("W");
tft.setCursor(295, 69);
tft.print("A");
tft.setTextSize(2); //Lower Right Side
tft.setCursor(3, 125);
tft.print("DAC0");
tft.setCursor(110, 125);
tft.print("%");
tft.setCursor(143, 125);
tft.print("DAC1");
tft.setCursor(251, 125);
tft.print("%");
tft.setCursor(3, 145);
tft.print("FAN");
tft.setCursor(110, 145);
tft.print("%");
tft.setCursor(143, 145);
tft.print("FAN");
tft.setCursor(251, 145);
tft.print("RPM");
tft.setTextSize(3);
tft.setCursor(165, 180);
tft.print("WH");
tft.setCursor(39, 210);
tft.print("H");
tft.setCursor(103, 210);
tft.print("M");
tft.setCursor(167, 210);
tft.print("S");
tft.setTextSize(2);
tft.setCursor(224, 180);
tft.print("T");
tft.setCursor(224, 200);
tft.print("X");
tft.setCursor(224, 220);
tft.print("Y");
tft.updateScreenAsync();
TouchTimer = 0;
debugmenu = 1;
drawtimer = 0;
}
if(drawtimer > 100 && debugmenu == 1){
drawtimer = 0; //reset drawtimer
// drawtime = 0;
//drawtime = 0;
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setTextSize(3); //Upper Left Side
tft.setCursor(50, 3);
tft.print("4.095"); //Q1
tft.setCursor(50, 27);
tft.print("4.095"); //Q2
tft.setCursor(50, 51);
tft.print("4.095"); //Q3
tft.setCursor(50, 75);
tft.print("4.095"); //Q4
tft.setCursor(50, 99);
tft.print("4.095"); //Q5
tft.setTextSize(4); //Upper Right Side
tft.setCursor(175, 3);
tft.print("81.92"); //V
tft.setCursor(150, 36);
tft.print("300.00"); //W
tft.setCursor(175, 69);
tft.print("20.48"); //A
tft.setTextSize(2); //Lower Right Side
tft.setCursor(63, 125);
tft.print("100"); //DAC0
tft.setCursor(203, 125);
tft.print("100"); //DAC1
tft.setCursor(63, 145);
tft.print("100"); //FAN
tft.setCursor(203, 145);
tft.print("9999"); //RPM
drawtime = 0;
printFloat6Char2Dec(3, 180, 3, WattHours);
drawtimelast = drawtime;
/*
drawtime = 0;
tft.setTextColor(ILI9341_WHITE);
tft.fillRect(3, 180, 162, 24, ILI9341_BLACK);
tft.setTextSize(3);
if(WattHours < 10){
tft.setCursor(75, 180); }
else if(WattHours < 100){
tft.setCursor(57, 180); }
else if (WattHours < 1000){
tft.setCursor(39, 180); }
else if (WattHours < 10000){
tft.setCursor(21, 180); }
else {tft.setCursor(3, 180); }
tft.print(WattHours, 3);
drawtimelast = drawtime;
*/
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setCursor(3, 210);
tft.print("24");
tft.setCursor(67, 210);
tft.print("60");
tft.setCursor(131, 210);
tft.print("60");
tft.setTextSize(2);
tft.setCursor(240, 180);
tft.print(drawtimelast);
tft.setCursor(240, 200);
tft.print(posx);
tft.print(" ");
tft.setCursor(240, 220);
tft.print(posy);
tft.print(" ");
tft.updateScreenAsync();
// drawtimelast = drawtime;
WattHours = WattHours + 0.1;
touchme = 0;
}
if (ts.touched() && drawtimer > 80 && drawtimer < 90 && touchme != 1) {
touchtime = 0;
tft.waitUpdateAsyncComplete();
TS_Point p = ts.getPoint();
posx = p.x;
posy = p.y;
touchtimelast = touchtime;
Serial.print("Pressure = ");
Serial.print(p.z);
Serial.print(", x = ");
Serial.print(p.x);
Serial.print(", y = ");
Serial.print(p.y);
Serial.print(", time = ");
Serial.print(touchtimelast);
Serial.println("uS");
TouchTimer = 0;
touchme = 1;
}
}
void printFloat5Char2Dec(uint16_t posx, uint16_t posy, uint8_t Textsize, float value){
tft.setTextColor(ILI9341_WHITE);
tft.fillRect(posx, posy, (Textsize*6*6), (Textsize*8), ILI9341_BLACK);
tft.setTextSize(Textsize);
if(value < 10){
tft.setCursor(Textsize * 12 + posx, posy); }
else if(value < 100){
tft.setCursor(Textsize * 6 + posx, posy); }
else {tft.setCursor(posx, posy); }
tft.print(value, 2);
}
void printFloat6Char2Dec(uint16_t posx, uint16_t posy, uint8_t Textsize, float value){
tft.setTextColor(ILI9341_WHITE);
tft.fillRect(posx, posy, (Textsize*6*7), (Textsize*8), ILI9341_BLACK);
tft.setTextSize(Textsize);
if(value < 10){
tft.setCursor(Textsize * 18 + posx, posy); }
else if(value < 100){
tft.setCursor(Textsize * 12 + posx, posy); }
else if (value < 1000){
tft.setCursor(Textsize * 6 + posx, posy); }
else {tft.setCursor(posx, posy); }
tft.print(value, 2);
}