When I am working on TFT_LCD 3.5 ,I need to include fonts libraries to change font style.
Can I combine all this font styles libraries in the Adafruit library in one file and include it in the code and how?
This is the code:
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
//-----------font_library-------------------
#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeSans12pt7b.h>
#include <Fonts/FreeSerif12pt7b.h>
#include <FreeDefaultFonts.h>
//-------------color-----------------------
#define BLACK 0x0000
#define RED 0xF800
#define GREEN 0x07E0
#define WHITE 0xFFFF
#define GREY 0x8410
//-----------------------------------------
void showmsgXY(int x, int y, int sz, const GFXfont *f, const char *msg)
{
tft.setFont(f);
tft.setCursor(x, y);
tft.setTextColor(0x780F, 0xffff);
tft.setTextSize(sz);
tft.print(msg);
}
void setup() {
// put your setup code here, to run once:
uint16_t ID = tft.readID();
tft.begin(ID);
tft.setRotation(0);
}
void loop() {
// put your main code here, to run repeatedly:
tft.fillScreen(GREY);
showmsgXY(100, 100, 2, &FreeSmallFont, "kinda");
showmsgXY(100, 150, 2, &FreeSans9pt7b, "kinda");
showmsgXY(100, 200, 2, &FreeSans12pt7b, "kinda");
showmsgXY(100, 250, 2, &FreeSerif12pt7b, "kinda");
showmsgXY(80, 330, 1, &FreeSevenSegNumFont, "1808");
while(1);
}
You can create your own custom header file e.g.
“my_fonts.h”
#include <Fonts/FreeSans9pt7b.h> //many Free Fonts come with Adafruit_GFX
#include <Fonts/FreeSans12pt7b.h>
#include <Fonts/FreeSerif12pt7b.h>
#include <FreeDefaultFonts.h> //not part of Adafruit_GFX
You copy your “my_fonts.h” to a tab in each sketch
And your “sketch.ino” contains:
#include "my_fonts.h"
There is no harm in including Fonts that you don’t use.
The Linker will see the object code created from:
And omit the FreeSans9pt7b code because it is not referenced in your project.
There are many things you can do with #include, #define, #if, …
They work fine with C++, C, S, H files
They don’t always work with .INO files i.e. the Arduino INO mangler may get confused.
I advise you to use bog-standard .INO sketches e.g.