/*
Example how to write and overwrite text on a TFT display
(by ec2021, 2022-08-15)
*/
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define TFT_DC 9
#define TFT_CS 10
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// This is a structure for text output
struct output_struct {
String text = {};
String old_text = {};
uint16_t CursorX = 0;
uint16_t CursorY = 0;
uint16_t TextColor = ILI9341_GREEN;
uint16_t TextSize = 1;
uint16_t BackGroundColor = ILI9341_BLACK;
void Print(String txt);
void set(uint16_t x, uint16_t y, uint16_t size, uint16_t color, uint16_t bgcolor );
};
// Function to set the relevant data
// x = X position for output
// y = Y position for output
// size = Text Size
// color = Text Color
// bgcolor = Background Color
void output_struct::set(uint16_t x, uint16_t y, uint16_t size, uint16_t color, uint16_t bgcolor ){
CursorX = x;
CursorY = y;
TextSize = size;
TextColor = color;
BackGroundColor = bgcolor;
}
// Function to print a (new) text
// Erases the old text by printing it with background color as text color
// and writes the new one
void output_struct::Print(String txt) {
tft.setTextSize(TextSize);
tft.setCursor(CursorX, CursorY);
tft.setTextColor(BackGroundColor);
tft.println(old_text);
tft.setCursor(CursorX, CursorY);
tft.setTextColor(TextColor);
tft.println(txt);
old_text = txt;
}
output_struct out1;
output_struct out2;
output_struct out3;
const int maxNo = 20;
int no = 0;
String o1;
String o2;
String o3 = "ABCDEFGHIJK";
String leer = " ";
void setup() {
tft.begin();
out1.set(26,120,3,ILI9341_RED, ILI9341_BLACK);
out1.Print("Text Demo");
out2.set(20,160,2,ILI9341_GREEN, ILI9341_BLACK);
out2.Print("Erasing Text");
out3.set(20,200,3,ILI9341_BLUE, ILI9341_BLACK);
out3.Print(o3);
delay(1000);
}
void loop() {
o1 = "Count up ";
o1.concat(no);
out1.Print(o1);
o2 = "Count Down ";
o2.concat(maxNo-no);
out2.Print(o2);
no++;
o3 = o3.substring(1)+o3[0];
out3.Print(o3);
if (no > maxNo) no = 0;
delay(1000);
}