Adafruit_mfGFX custom font need help


I used Dot Factory v0.1.4 to generate the code for 0123456789 Microsoft Sans Serif 16pts following @peekay123 pdf guide on but I was not able to print to the 2.4"spi tft screen. Below is my test code:


   void test(void)
  tft.setTextColor(ILI9341_BLACK, ILI9341_WHITE);

    tft.setFont(MSS_16); // Microsoft Sans Serif 16pts
    tft.setTextColor(ILI9341_RED, ILI9341_WHITE);
    tft.print("1"); // <---Not printing to tft display

    tft.print("2"); //<-- works

fonts.cpp mod:

const uint8_t microsoftSansSerif_16ptBitmaps[] =
	0x00, 0x90,
	0x1E, 0x00, 0x3F, 0x00, 0x61, 0x80, 0x61, 0x80, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xE1, 0xC0, 0x61, 0x80, 0x3F, 0x00, 0x1E, 0x00,
	0x0C, 0x0C, 0x1C, 0xFC, 0xEC, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C,
	0x3F, 0x00, 0x7F, 0x80, 0xE1, 0xC0, 0xC0, 0xC0, 0x00, 0xC0, 0x00, 0xC0, 0x01, 0x80, 0x01, 0x80, 0x07, 0x00, 0x0C, 0x00, 0x18, 0x00, 0x30, 0x00, 0x60, 0x00, 0xFF, 0xC0, 0xFF, 0xC0,
	0x3E, 0x00, 0x7F, 0x00, 0xE3, 0x80, 0xC1, 0x80, 0x01, 0x80, 0x03, 0x80, 0x0F, 0x00, 0x0F, 0x80, 0x01, 0x80, 0x00, 0xC0, 0x00, 0xC0, 0xC0, 0xC0, 0xE1, 0xC0, 0x7F, 0x80, 0x3E, 0x00,
	0x01, 0x80, 0x03, 0x80, 0x07, 0x80, 0x07, 0x80, 0x0D, 0x80, 0x19, 0x80, 0x31, 0x80, 0x61, 0x80, 0xC1, 0x80, 0xFF, 0xE0, 0xFF, 0xE0, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80,
	0x3F, 0x80, 0x3F, 0x80, 0x30, 0x00, 0x30, 0x00, 0x60, 0x00, 0x7F, 0x00, 0x7F, 0x80, 0x61, 0xC0, 0x00, 0xC0, 0x00, 0xC0, 0x00, 0xC0, 0xC0, 0xC0, 0xE1, 0x80, 0x7F, 0x80, 0x3E, 0x00,
	0x1F, 0x00, 0x3F, 0x80, 0x71, 0xC0, 0x60, 0xC0, 0xC0, 0x00, 0xDF, 0x00, 0xFF, 0x80, 0xE1, 0x80, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0x60, 0xC0, 0x71, 0x80, 0x3F, 0x80, 0x1E, 0x00,
	0xFF, 0xC0, 0xFF, 0xC0, 0x00, 0xC0, 0x01, 0x80, 0x01, 0x80, 0x03, 0x00, 0x03, 0x00, 0x06, 0x00, 0x06, 0x00, 0x0C, 0x00, 0x0C, 0x00, 0x0C, 0x00, 0x18, 0x00, 0x18, 0x00, 0x30, 0x00,
	0x1E, 0x00, 0x3F, 0x00, 0x61, 0x80, 0x61, 0x80, 0x61, 0x80, 0x33, 0x00, 0x1E, 0x00, 0x3F, 0x00, 0x73, 0x80, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xE1, 0xC0, 0x7F, 0x80, 0x3F, 0x00,
	0x1E, 0x00, 0x7F, 0x00, 0x63, 0x80, 0xC1, 0x80, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0x61, 0xC0, 0x7F, 0xC0, 0x3E, 0xC0, 0x00, 0xC0, 0xC1, 0x80, 0xE3, 0x80, 0x7F, 0x00, 0x3E, 0x00,

const FontDescriptor microsoftSansSerif_16ptDescriptors[] =
	{10, 15, 0},
	{6, 15, 30},
	{10, 15, 45},
	{10, 15, 75},
	{11, 15, 105},
	{10, 15, 135},
	{10, 15, 165},
	{10, 15, 195},
	{10, 15, 225},
	{10, 15, 255}

fonts.h mod:

#define MSS_16	6

extern const uint8_t microsoftSansSerif_16ptBitmaps[];
extern const FontDescriptor microsoftSansSerif_16ptDescriptors[];

Adafruit_mfGFX.cpp mod:

case TEST:
      fontData = testBitmaps;
	  fontDesc = testDescriptors;
      fontKern = 1;
    case MSS_16:
          Serial.println("IHERE");  //test info
          fontData = microsoftSansSerif_16ptBitmaps;
    	    fontDesc = microsoftSansSerif_16ptDescriptors;
          fontKern = 1;
          Serial.println("hereEnd"); //test info
      font = ARIAL_8;
      fontData = arial_8ptBitmaps;
	  fontDesc = arial_8ptDescriptors;
      fontKern = 1;

The custom font is being call and entering the case statement of Adafruit_mfGFX.cpp but I can’t figure out why its not displaying into the display. Any help will be grateful.

Thanks in advance.

@sheng, the problem is in the first two bytes of const uint8_t microsoftSansSerif_16ptBitmaps[]. These bytes define the start and end characters you have defined. Since you only defined β€œ0123456789”, the start byte should be 0x30 (ascii β€œ0”) and the end byte should be 0x39 (ascii β€œ9”). Change the values and give it another shot. :wink:


@peekay123, Thank You! :smile: It works finally!

To not make the same mistake again, if I want β€œ.0123456789A”, the start byte should be 0x2E ( ascii β€œ.”) and the end byte should be 0x41 (ascii β€œA”), is this correct assumption?

@sheng, the values need to be contiguous so you would need to have ALL characters between the β€œ.” and the β€œA”. In order words, you need β€œ./0123456789:;<=>?@A”. :wink:

1 Like

@peekay123, Thanks, I got it now. :smile:

1 Like

I ran into another issue with the custom font overlap if update in the same location. Only happen when one number is bigger then the other such as 1 vs 0. Please see picture below:

Test code:

void setup()


  Serial.println("Adafruit 2.4\" SPI TFT Test!");

  // Set the TFT and touch screen to landscape orientation


  tft.setTextColor(ILI9341_BLACK, ILI9341_WHITE);

  tft.setTextColor(ILI9341_RED, ILI9341_WHITE);

  tft.setFont(MSS_72); // microsoftSansSerif_72pt
  tft.print("31");  // problem 1 is smaller then 0 will over lap when rewrite


I think its the way I am clearing the screen. I try fill screen but create a blink effect which does not look good. Any advice will be great.

Thanks in advance.

@sheng, I suggest that instead of filling the entire screen every time, only fill the largest area that will be used by your text using a filled rectangle. Also, try print a number of spaces after each number to see if that helps, in your case tft.print("31 "). :smile:

@peekay123, sorry not to get back to you sooner, I was away for few days. Back to the problem at hand, I try tft.print("31 "), the result is 310 on the screen. It look like, with the space, it padded it with a 0. I will try the fill rectangle next and see if I get a solution.

In the Adafruit_ILI9341 library, is there any command to put the screen in sleep mode or dim the LED? I am using the 2.4" spi tft touch screen, when I turn off the light, the screen shine so bright.

Thanks in advance.

@sheng, the reason you get a β€œ0” instead of a space is because that is the first defined char! You should define the space as your char, then the rest of the numbers.

Which 2.4" SPI TFT touch screen are you using (model)? If it is the Adafruit model, you may be able to control the display brightness via the β€œLite” input of the display board. You could connect a PWM-capable digital pin from the Photon to this pin and control the brigthness by controlling the PWM duty cycle. :smiley:

@peekay123, I just tested the fill rectangle, it does work but have a little blink effect. Oh yeah, I did not change the space with the custom font. Do I have to use dot factory to
custom " !"#$%&’()*+,-./0123456789" to get the space? Currently, I only have number to save memory. lol, I have upgraded to P1 but pin is limited again. Just with the lcd and touch, it took up 10 pin already.

I am using

It does not look like it have a β€œLite” input. All I can think of was to control the LED pin but it will be either on or off. :frowning:

@sheng, creating the space will require those extra characters so it may be easier to draw the rectange. Are you using software or hardware SPI with the display?

As for brightness, I have one of those displays so I will look at it later to see what could be done.

@peekay123, I am using hardware SPI. For the touch, I used Utouch library with four pins. I tied the LED to 3V, I can free up RST if I need a control pin, right now its tied to A0.

Strange, I just used dot factory to convert the " !"#$%&’()*+,-./0123456789", modified the start and end char as 0x20, 0x39, tft.print(β€œ1. 31 1”); show up as 1.311 but no space.

Great, thanks for all the help! :smile:

@sheng, the β€œA” is not in order so it won’t print correctly. The way you have things, the β€œA” is where the β€œ:” should be. So either you keep what you have and print β€œ:” for an A to appear or include all the characters between the β€œ9” and the β€œA”, meaning β€œ:;<=>?@”.

@peekay123, I think I am missing something. I just need β€œspace,0123456789”. In dot factory, just to test I include " !"#$%&’()*+,-./0123456789:;<=>?@A" font. Space before !. I did a print out on all the char every char print except Space. I am using 0x20, 0x41 as the start and end char. What am I doing wrong?

@peekay123, I think i found a solution to dim the lcd screen. Is there any way to use one of the pin to output various voltage from the P1?

@sheng, the space is only two pixels wide I believe so it won’t show well. I still think your best bet is the rectangle fill.

As for the P1 driving the LED for the backlight, it cannot provide enough current. You would need to drive a transistor which in turn drives the backlight LEDs.

@peekay123, that make sense, I thought I was doing something wrong. No wonder the space does not show up. Rectangle fill it is then.

I was afraid that you would said that regarding to the LED. Is that mean I need to modify Adafruit_ILI9341.cpp since β€œAdafruit_ILI9341::Adafruit_ILI9341(uint8_t cs, uint8_t dc, uint8_t rst)” take three pins? I am trying to use the RST pin to drive the transistor. Is there an easy way around this or just comment out the rst code would do? Thanks

Can you not assign a wider bit pattern for a space then?

@ScruffR, you are correct! The width can be set to be full char width. :smiley:

What command or mod do you need to do to set it to full char width?