Slow draw fonts

Hi,

I am using a Mega2560 with a Mcufriend 3.5" tft display. I am working on a project that I would like to flash/blink a line of text. This works ok with the built in font but not so well with the 7 segment font that is supplied with the mcufriend library where there is a very noticeable left to right draw that is not acceptable in this application. I have written a small sketch to test this but the result is still the same, so...as the built-in font is almost like a 7 segment font is it possible to modify it to look like a seven segment font? or is there a faster way of doing this?

#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;       // hard-wired for UNO shields anyway.
#include <TouchScreen.h>
#include <FreeDefaultFonts.h>


const int white =  0xFFFF;
const int green =  0x07E0;

int state;

void setup() {
 
 uint16_t id;
 
 tft.reset();
 id = tft.readID();
 tft.begin(id);
 tft.setRotation(1);//landscape
 tft.fillScreen(white);

}

void loop() {

 
  
    tft.setFont(&FreeSevenSegNumFont);
    tft.setTextSize(1);
    
    if (state==LOW) {state=HIGH;} else {state=LOW;}
    
    if (state==LOW) {tft.setCursor(20,76);
                     tft.setTextColor(white);
                     tft.print(2850);
                     delay(300);}

    if (state==HIGH) {tft.setCursor(20,76);
                       tft.setTextColor(green);
                       tft.print(2850);
                       delay(300);}

    tft.setFont(NULL);

    tft.setTextSize(1);

    if (state==LOW) {tft.setCursor(20,100);
                     tft.setTextColor(white);
                     tft.print(2850);
                     delay(300);}

    if (state==HIGH) {tft.setCursor(20,100);
                       tft.setTextColor(green);
                       tft.print(2850);
                       delay(300);}
}

what happens if you don't mess around with switching font in the loop()?

if you put this in setup()

    tft.setFont(&FreeSevenSegNumFont);
    tft.setTextSize(1);

and don't do the part

    tft.setFont(NULL);
    tft.setTextSize(1);

Same result, the switching fonts was there just so I could see the difference between the two fonts, actually the built-in font should be size 6 but it doesn't make a lot of difference to the draw speed

what about this?

#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;       // hard-wired for UNO shields anyway.
#include <TouchScreen.h>
#include <FreeDefaultFonts.h>

const int white =  0xFFFF;
const int green =  0x07E0;

boolean displayText = true;

int16_t  x, y, x1, y1;
uint16_t w, h;

void setup() {

  uint16_t id;

  tft.reset();
  id = tft.readID();
  tft.begin(id);
  tft.setRotation(1);//landscape
  tft.fillScreen(white);
  tft.setFont(&FreeSevenSegNumFont);
  tft.setTextSize(1);
  tft.setTextColor(green);
  x = 20;
  y = 76;

  tft.getTextBounds("2850", x, y, &x1, &y1, &w, &h);
}

void loop() {

  if (displayText) {
    tft.setCursor(x, y);
    tft.print("2850");
  } else {
    tft.fillRect(x1, y1, w, h, white);
  }
  delay(300);

  displayText = !displayText;

}

any faster? (totally untested, typed here)

I have to say that I wish I could write a piece of code like that without making any mistakes at all! Sadly I can't and that way of doing it doesn't make any difference unfortunately. I've used that technique of filling a rectangle but I tend to use it where I know the characters will change between flashes, usually works well. I think this problem is because of the way the built-in fonts are different from the add-on fonts, only I'm not sure what the difference is. when you look at those two fonts they are not a world apart so why should one take a lot longer to draw that the other?

I think this problem is because of the way the built-in fonts are different from the add-on fonts

I don't think the issue is with the way the two different kinds of fonts are designed. The built-in fonts most likely live in PROGMEM, while the add-on fonts live in SRAM. The code is optimized for displaying PROGMEM data.

PaulS:
I don't think the issue is with the way the two different kinds of fonts are designed. The built-in fonts most likely live in PROGMEM, while the add-on fonts live in SRAM. The code is optimized for displaying PROGMEM data.

No, the new fonts are also in PROGMEM, see FreeDefaultFonts.h

 const uint8_t FreeSmallFontBitmaps[] PROGMEM = {
// 0x0 [0x20 '.']

// 1x8 [0x21 '!']
0xFD,
// 4x3 [0x22 '"']
0x5A,0xA0,
// 6x8 [0x23 '#']
0x28,0xAF,0xCA,0x53,0xF5,0x14,
...

In order to deal with more complex font, MCUFRIEND_kbv does way more when drawing a char / pixel than the original adafruit library... there is a cost there probably to that flexibility

there is a cost there probably to that flexibility

I think "probably" should be "definitely".

PaulS:
I think "probably" should be "definitely".

probably :slight_smile:

so is there a work around for this? is it possible to create a seven segment font that displays as fast as the built-in font?

MCUFRIEND_kbv inherits all the methods from the Adafruit_GFX class and Print class.

you can see how the 'Classic' built-in font is handled in drawChar().

if the font is not set, then it uses optimized code

    if(!gfxFont) { // 'Classic' built-in font
   ...

The library knows the size of the bitmaps and thus has an optimized code for getting the right pixels out to the screen.

Now If you have set another font, it will do more generic maths to adjust to character size.

--> you could modify the library to suit your font size and optimize the maths

That's along the lines I was thinking but as I'm new to the Ardunio and C I wasn't quite sure what direction I should be looking, Thanks.