Adafruit GFX "buffer" for drawing text

Hi, I have a 480x320 16-bit display which I'm driving with the TFT_HX8357 library (Based on Adafruit GFX). I'm dawing a fixed-width custom font using the code below, and need to be able to refresh parts of the display when the lines change.

tft.fillRect(x_offset, y_coordinate_for_line(10) - 18, 480 - x_offset, 21, TFT_BLACK);
tft.setCursor(x_offset, y_coordinate_for_line(10) + y_offset);
tft.print(cdu10);

However, fillRect causes that line to briefly flicker to black. Is there any way i can fill and draw text in one pass, and then transfer the data to the screen?

The official GFX way to handle this is with getTextBounds() to determine the area that you want to "rub out". This will be smaller (and faster) than rubbing out the whole line.

If you rub out and redraw one letter at a time you will notice less flicker.

However, you are using Bodmer's TFT_HX8357 library. He does it all for you.
i.e. he has methods to select rub-out mode, different fonts, ...

As always. Run all of the library examples before you attempt to write your own sketches.

If you go for a regular Adafruit_GFX library you have to do the work yourself e.g. with getTextBounds()

Are you referring to the drawString() function? That overwrites the previous string without flickering, however I'm unable to use certain codepoints in the string, like ±¡©® and ¶. This is how the data comes in from the software on the PC-side and i need them. If i try to draw these using drawString() instead of print(), the display corrupts.

If i use print() with the same string, it draws fine. If i remove the special charactes and use drawString() it also draws fine, but as i said i need them.

No, I am talking about the examples at https://github.com/Bodmer/TFT_HX8357 e.g.

Free_Font_Demo
HX8357_Char_times
HX8357_Padding_demo
TFT_Print_Test

Run the examples. See what they do. Inspect the sketch code.
Study https://github.com/Bodmer/TFT_HX8357/blob/master/TFT_HX8357.h

No, I am not going to dig out a compatible ILI9486 before you have done these simple steps first.

I did not write the library. I would have to remind myself too. I suspect that these 6-yr old methods work exactly like the modern TFT_eSPI library. i.e. Bodmer wrote perfectly good code in the first place.

David.

Yes, drawString() is exactly what they use to clear the background. Free_Font_Demo says "Draw with background using drawString()".

I was able to narrow down the corruption to encoding issues, but now the problem is drawString() doesn't overwrite the previous line any more, just draws on top of it.

#include "TFT_Setup.h"
#include <TFT_HX8357.h>
#include "CDU_Font.h"

// Invoke library
TFT_HX8357 tft = TFT_HX8357();

#define text_height 21
#define text_width 18

// Offsets for slightly randomizing text position to prevent burn-in.
int x_offset = 13; // Max 27
int y_offset = 0; // Max 6

// Text lines to display
char* lines[] = {
  " WAYPT         0   D5/B1 ",
  "                         ",
  " 1      SP    INIT POSIT ",
  "                    UNK  ",
  "EL:23      ******** DTOT ",
  " NO CR                   ",
  " N34 35.367           ?1 ",
  "             WND ***/*** ",
  " E036 00.680         L/L ",
  "[                ]   P1/2 "
};

void setup() {
  // Slightly randomize position on every boot to avoid burn-in.
  randomSeed(analogRead(0));
  x_offset = random(15) + 6; // Range 6-21
  y_offset = random(6);
  
  tft.begin();
  tft.setRotation(1);

  tft.fillScreen(TFT_BLACK);
  tft.setTextColor(TFT_GREEN, TFT_BLACK);
  tft.setFreeFont(&CDU);
  tft.setTextSize(3);
  tft.setTextWrap(false);

  drawCDULine(1);
  drawCDULine(2);
  drawCDULine(3);
  drawCDULine(4);
  drawCDULine(5);
  drawCDULine(6);
  drawCDULine(7);
  drawCDULine(8);
  drawCDULine(9);
  drawCDULine(10);
}

int toggle = 0;

void loop() {
  if (toggle == 0) {
    setCDUText(10, "[               ]   P1/2 ");
    toggle = 1;
  } else {
    setCDUText(10, "[#              ]   P1/2 ");
    toggle = 0;
  }
  
  delay(900);
}

// Set and draw CDU Text
void setCDUText(int line, char* text) {
  lines[line - 1] = text;
  drawCDULine(line);
}

// Draw existing CDU Text
void drawCDULine(int line) {
  /*tft.fillRect(x_offset, (y_coordinate_for_line(line) - text_height) + y_offset, 462 - x_offset, text_height, TFT_BLACK);
  tft.setCursor(x_offset, y_coordinate_for_line(line) + y_offset);
  tft.print(lines[line - 1]);*/

  tft.drawString(lines[line - 1], x_offset, y_coordinate_for_line(line) + y_offset, 1);
}

int y_coordinate_for_line(int line) {
  //return (32 * line) - 8;
  return (32 * line) - 27;
}

Nevermind, i missed setTextPadding, and it causes the text to flicker yet again.

Please run the examples first. Quote example "by name" when you have a question.

If and when you have run examples I will dig out a suitable shield and show you what to do.

David.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.