Go Down

Topic: text overwrite on TFT Touch screen applicatio (Read 5693 times) previous topic - next topic


I am trying to display successive numbers on an Adafruit TFT touchscreen display. As I increment the number it overwrites itself on the screen, eventually obscuring the entire number.
Is there a way to clear the text before I write another number? I am using the ILI9341 library.

The application is a touch screen tuner for a programmable oscillator. Touch sceen instead of a rotary encoder.


Dec 06, 2015, 08:34 am Last Edit: Dec 06, 2015, 08:35 am by david_prentice
Use tft.setTextColor(WHITE, BLACK) to set the background colour.   If you use the setTextColor(WHITE) method,    the foreground is WHITE but it does not draw the background colour.

Obviously 65536 different colours are available.    Common colours might have a #define e.g. YELLOW,  BLUE, ....



Use tft.setTextColor(WHITE, BLACK) to set the background colour.   If you use the setTextColor(WHITE) method,    the foreground is WHITE but it does not draw the background colour.
@david_prentice: I'm not the original poster, but I just wanted to say thanks for this. I've been writing my text/values in white, then drawing a black rectangle over the white text, then rewriting the updated text/values in white again. It works, but clearly the method you mentioned is better and more efficient. It's still early in the morning, but I guarantee this will be the best thing that I learn today.



I did not click either.    It was ages before I realised the significance of the different methods:
Code: [Select]

   void setTextColor(uint16_t c);
   void setTextColor(uint16_t c, uint16_t bg);

But the classic Duh was when I realised that
Code: [Select]

   print(double d, int decimals=2);

would let you specify different numbers of decimals.
after all,  you normally just say print(floatvalue) without specifying the second argument i.e. decimals.

C++ has some nice features.



Dec 07, 2015, 08:03 pm Last Edit: Dec 07, 2015, 08:11 pm by TFTLCDCyg
The best practice is to convert the number in a character chain, for example:

Code: [Select]
#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);

char TX[20];
int lectV;

void setup() {
  tft.setRotation(1); tft.fillScreen(ILI9341_BLACK);
  tft.setTextColor(ILI9341_YELLOW, ILI9341_BLACK); tft.setTextSize(2);
  tft.setCursor(0, 5); tft.print("Number");

  sprintf(TX,"Volume = %4d ml", lectV);
  tft.setCursor(10, 50);  tft.print(TX);

void loop() {
  lectV = random(9000); 
  sprintf(TX,"Volume = %4d ml", lectV);
  tft.setCursor(10, 50);  tft.print(TX);


If you look in the "Adafruit_GFX.cpp" you will see the following.

Code: [Select]

void Adafruit_GFX::setTextColor(uint16_t c) {
 // For 'transparent' background, we'll set the bg
 // to the same as fg instead of using a flag
 textcolor = textbgcolor = c;

void Adafruit_GFX::setTextColor(uint16_t c, uint16_t b) {
 textcolor   = c;
 textbgcolor = b;

If you only want to Draw the Character with a transparent background meaning you only write the character
setTextColor in the prescribed pixels changing none of the surrounding pixels in the character block defined by x, y, setTextSize.

Code: [Select]

TFT.setTextColor(GREEN); //With Transparent Text Background not updating surrounding pixels.

If you want to Draw the Character with your background color, replace app pixels in the character block defined by x, y, setTextSize with your chosen text color and your background to be the same as your chosen background. This will overwrite all pixels with old character that are no longer used with the background color all at the same time preventing mess of pixels, and character flashing.

TFT.setTextColor.setTextColor(GREEN, BLACK); //With Text Background Color updating surrounding pixels.


Oct 07, 2019, 04:45 pm Last Edit: Oct 07, 2019, 04:49 pm by avandalen
I have made a library extension to overwrite text and numbers correct:

Download the library and examples at GitHub: ILI9341-overwrite-text

Go Up