Dibujar rectángulo en pantalla TFT procesador ILI9488

Hola estimados, buscando en las librerías mcufriend y en internet no logro dar con información respecto de como dibujar un rectángulo en esta pantalla.
Escribo texto perfectamente, pero a la hora de realizar el borrado de una zona, quiero hacerlo con un rectángulo del mismo color del fondo de pantalla para realizar la escritura de un nuevo dato en ese lugar.

Escribo lo siguiente:

tft.setCursor(1, 2 + pasovert * 2);
tft.fillScreen(WHITE);
tft.setTextColor(GREEN);
tft.setTextSize(1);
tft.print(" SECTOR 1 = ARMADO ");

Donde dice ARMADO, tengo que borrar y volver a escribir sin borronear :slight_smile: , en caso de recibir un nuevo dato. Por ejemplo ACTIVADO , FALLA o la condición que sea se indique a través del dato recibido.

Quieres "borrar" el texto previo y escribir en su lugar el texto que se obtenga de la nueva condición

Prueba con esto:

tft.setCursor(1, 2 + pasovert * 2);
tft.fillScreen(WHITE);
tft.setTextColor(GREEN, WHITE);
tft.setTextSize(1);
tft.print(" SECTOR 1 =  ARMADO ");

En forma general:

if(condicion A==1){
    tft.setTextColor(GREEN, WHITE);
    tft.setTextSize(1);
    tft.print(" SECTOR 1 =  ARMADO ");
}

if(condicion A==2){
    tft.setTextColor(RED, WHITE);
    tft.setTextSize(1);
    tft.print(" SECTOR 1 =  PELIGRO ");
}

if(condicion A==3){
    tft.setTextColor(YELLOW, WHITE);
    tft.setTextSize(1);
    tft.print(" SECTOR 1 =  TE FALTAN CABLES... ");
}

if(condicion A==0){
    tft.setTextColor(BLACK, WHITE);
    tft.setTextSize(1);
    tft.print(" SECTOR 1 =  DURMIENDO...");
}

En todos los textos nuevos debes usar como fondo del texto, el color de fondo general, eso es lo que significa esta linea:

    tft.setTextColor(Color del texto, Color del fondo);

Hola TFTLCDCyg, muchas gracias por tu respuesta, probé con lo que dices para que escriba según. la condición entrante, pero sigue escribiendo sobre lo que estaba anteriormente. El color de fondo de texto lo puse como dices. Al compilar me da todo ok pero te envío una foto de como queda en pantalla.

Gracias por tu ayuda

Sube el código que usaste.

No tengo la pantalla que tienes, pero las librerías de adafruit tienen la misma base de configuración. Hice las pruebas con una pantalla ILI9341 de 2.4" y la librería mas básica: Adafruit_ILI9340, que tiene instrucciones idénticas de control a la librería que usas para tu pantalla..

Este es el sketch que usé, solo cambia el encabezado y el llamado de colores para que se adapte a la librería de tu pantalla:

#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9340.h"
#define _cs 10
#define _dc 9
#define _rst 8
Adafruit_ILI9340 tft = Adafruit_ILI9340(_cs, _dc, _rst);

void setup() {
  Serial.begin(9600);
  Serial.println("ILI9341 BREAK OUT 24-TOUCH"); 
 
  tft.begin();
  tft.setRotation(3);
  tft.fillScreen(ILI9340_BLACK);
  MenuPrincipal();
}

void loop(){}

void MenuPrincipal()
{
  while(1)
{
  tft.setTextColor(ILI9340_GREEN, ILI9340_BLACK);
  tft.setCursor(0, 0);
  tft.setTextSize(5);
  tft.print("Armado      ");
  tft.setCursor(0, 100);
  tft.setTextSize(3);
  tft.print("Sistema    ");
  delay(1000);
  
  tft.setTextSize(5);
  tft.setTextColor(ILI9340_RED, ILI9340_BLACK);
  tft.setCursor(0, 0);
  tft.print("En linea      ");
  delay(1000);

  tft.setTextColor(ILI9340_BLUE, ILI9340_BLACK);
  tft.setCursor(0, 0);
  tft.print("En espera    ");
  delay(1000);
}
}

Para variar sin usar el void loop()

Para evitar "letras sobrantes", lo ideal es que insertes espacios antes y después en cada linea de texto

tft.print("Armado      ");
tft.print("En linea      ");
tft.print("En espera    ");

El truco consiste en disponer de lineas de texto de la misma longitud y que no tengas que borrar todo lo que mandas a la pantalla.

Hola TFT LCDCyg, muchas gracias por tu atención. El código que utilizo es este:

#include <Adafruit_GFX.h>
#if defined(_GFXFONT_H_)           //are we using the new library?
#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeSerifBold18pt7b.h>
#define ADJ_BASELINE 11            //new fonts setCursor to bottom of letter
#else
#define ADJ_BASELINE 0             //legacy setCursor to top of letter
#endif
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF
#define GREY    0x7BEF
#define Pin13LED         13
#include <stdio.h>

int byteReceived;
int valorAnterior = 0;
int consultaRem;
int byteSend;
int buzzPresion;
int disparo = 0;
int ca;
int valorAnteriorca = 0;
#define RS485Transmit    HIGH
#define RS485Receive     LOW
#define SSerialTxControl 22   //RS485 Direction control

uint16_t ID;
uint8_t hh, mm, ss; //containers for current time

uint8_t conv2d(const char* p)
{
  uint8_t v = 0;
  if ('0' <= *p && *p <= '9') v = *p - '0';
  return 10 * v + *++p - '0';
}

void setup(void)
{
  pinMode(48, OUTPUT);
  Serial.begin(9600);
  Serial1.begin(9600);
  pinMode(SSerialTxControl, OUTPUT);
  digitalWrite(SSerialTxControl, RS485Receive);  // Init Transceiver
  tft.reset();
  ID = tft.readID();
  Serial.print("TFT ID = 0x");
  Serial.println(ID, HEX);
  //    if (ID == 0x00D3) ID = 0x9481; // write-only shield
  if (ID == 0x00D3) ID = 0x9486; // write-only shield
  tft.begin(ID);
  tft.setRotation(1);
  tft.fillScreen(WHITE);
  //#if defined(_GFXFONT_H_)
  //    tft.setFont(&FreeSans9pt7b);
  tft.setFont(&FreeSerifBold18pt7b);

  /*#endif
      hh = conv2d(__TIME__);
      mm = conv2d(__TIME__ + 3);
      ss = conv2d(__TIME__ + 16);*/

}

void loop(void)
{
  int16_t x, y, dx, dy, idx;
  uint16_t w, h, len, mask, pasovert = 48;
  uint16_t colors[8] = { BLACK, WHITE, YELLOW, CYAN, GREEN, MAGENTA, RED, BLUE };
  uint16_t height, width;
  width = tft.width();
  height = tft.height();
  //Serial.flush();

  //------------------------------------------------------------------
  

 


  //------------------------------------------------------------------

 

  digitalWrite(SSerialTxControl, RS485Receive);  // Disable RS485 Transmit


   if (Serial1.available())  //Look for data from other Arduino
  {
    byteReceived = Serial1.read();    // Read received byte
   
     if (byteReceived == 65) {


      tft.setCursor(1, 2 + pasovert * 2);
      //tft.fillScreen(WHITE);
      tft.setTextColor(GREEN, WHITE);
      tft.setTextSize(1);
      
      tft.print(" Area 1 =  ARMADO ");
      delay(500);
     

     

    }

    if (byteReceived == 67) {



      
      tft.setCursor(1, 2 + pasovert * 2);
  
      tft.setTextColor(RED, WHITE);
      tft.setTextSize(1);
      tft.print("Area 1 = FALLA PRESION");
      delay(500);

    
    }

   

    if (byteReceived == 69) {

     
      tft.setCursor(1, 2 + pasovert * 2);
      tft.setTextColor(RED, WHITE);
      tft.setTextSize(1);
      tft.print(" AREA 1 =  FALLA ZONA ");
      delay(500);
     
    }

Con esto le digo en quie linea tiene que escribir

tft.setCursor(1, 2 + pasovert * 2);

y si para la misma linea cambia la información, sobreescribe.

Un colega me pasó este código de un reloj que borra dibujando un rectángulo. Tal vez lo pueda adaptar a mi código, escribiendo un rectángulo blanco, ya que el fondo es blanco, y así producir un borrado.

// Si fuese SPI seria mucho mas rapida, en este caso l ainterfase es paralelo 8 bits
#include <Adafruit_GFX.h>
#include <Adafruit_TFTLCD.h> // Hardware-specific library
#if defined(_GFXFONT_H_)           //are we using the new library?

#include <Fonts/FreeSerifBold12pt7b.h>
#define ADJ_BASELINE 14            //new fonts setCursor to bottom of letter
#else
#define ADJ_BASELINE 0             //legacy setCursor to top of letter
#endif
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
uint8_t YP = A1;  // must be an analog pin, use "An" notation!
uint8_t XM = A2;  // must be an analog pin, use "An" notation!
uint8_t YM = 7;   // can be a digital pin
uint8_t XP = 6;   // can be a digital pin

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF
#define GREY    0x7BEF

#include <stdio.h>

   uint16_t ID;
   uint8_t hh, mm, ss; //containers for current time
   uint16_t ancho_pantalla, alto_pantalla;
   int16_t posx = 20, posy = 80, x,y,x1,y1, tamanotexto = 1;
   uint16_t w,h;
   
 uint8_t conv2d(const char* p)
{
    uint8_t v = 0;
    if ('0' <= *p && *p <= '9') v = *p - '0';
    return 10 * v + *++p - '0';
}

  
void setup() {
  // put your setup code here, to run once:
 Serial.begin(9600);
    tft.reset();
    ID = tft.readID();
    Serial.print("Pantalla TFT ID = 0x");
    Serial.println(ID, HEX);
    //    if (ID == 0x00D3) ID = 0x9481; // write-only shield
    if (ID == 0x00D3) ID = 0x9488; // write-only shield
    tft.begin(ID);
    tft.setRotation(1);
    tft.setTextSize(tamanotexto);
    tft.fillScreen(BLACK);
#if defined(_GFXFONT_H_)
    tft.setFont(&FreeSerifBold12pt7b);
#endif
    ancho_pantalla = tft.width();
    alto_pantalla = tft.height();
    Serial.print("El ancho horizontal en pixels es ");
    Serial.println(ancho_pantalla);
    Serial.print("La altura vertical en pixels es ");
    Serial.println(alto_pantalla);

    // Toma la hora de las variables del compilador - Se borra al desconectar el arduino
    hh = conv2d(__TIME__);
    mm = conv2d(__TIME__ + 3);
    ss = conv2d(__TIME__ + 16);
}

void loop() {
  // put your main code here, to run repeatedly:
while (1) {
  for (tamanotexto = 1; tamanotexto < 5; tamanotexto++){  tft.setTextSize(tamanotexto);
        if (++ss > 59) {
            ss = 0;
            mm++;
            if (mm > 59) {
                mm = 0;
                hh++;
                if (hh > 23) hh = 0;
            }
        }
        char buf[20];

     sprintf(buf, "%02d:%02d:%02d", hh, mm, ss); // almacena la hora en buf con formato de dos digitos
 
  tft.setCursor(posx,posy);
   tft.setTextColor(RED);
  tft.print(buf);
    
  delay(1000);
tft.getTextBounds(buf, x, y, &x1, &y1, &w, &h);
tft.drawRect(posx, posy-(ADJ_BASELINE+2)*tamanotexto, w+tamanotexto*2, h+tamanotexto, YELLOW); // Solo para ver el area a borrar, se puede eliminar
tft.fillRect(posx, posy-(ADJ_BASELINE+2)*tamanotexto, w+tamanotexto*2, h+tamanotexto, BLACK); // Borra el area 
  }
} // Fin de while
} // Fin de loop

Gracias de antemano