Skip to content

Commit 8225398

Browse files
committed
Merge multi-font
Merge Multi Font to Main
2 parents 1f81e8e + 0c68ac7 commit 8225398

File tree

4 files changed

+85
-20
lines changed

4 files changed

+85
-20
lines changed

TFT_eSPIDisplay.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,35 @@ TFT_eSPI *TFT_eSPIDisplay::_tft = nullptr;
2424
bool TFT_eSPIDisplay::_tftInitialised = false;
2525

2626
TFT_eSPIDisplay::TFT_eSPIDisplay(uint8_t rotation, uint8_t textSize, uint16_t textColour, uint16_t backgroundColour) {
27+
TFT_eSPIDisplay::TFT_eSPIDisplay(uint8_t rotation, const GFXfont *textFont, uint16_t textColour,
28+
uint16_t backgroundColour) {
2729
_rotation = rotation;
2830
_textSize = textSize;
31+
_textSize = 1; // default text size to save amending display routine.
2932
_textColour = textColour;
3033
_backgroundColour = backgroundColour;
3134
if (_tft == nullptr) {
3235
_tft = new TFT_eSPI();
3336
}
3437
_gfxFont = TEXT_FONT;
38+
_gfxFont = textFont;
3539
}
3640

3741
TFT_eSPIDisplay::TFT_eSPIDisplay(uint8_t rotation, uint8_t textSize, uint16_t textColour, uint16_t backgroundColour,
3842
int csPin) {
43+
TFT_eSPIDisplay::TFT_eSPIDisplay(uint8_t rotation, const GFXfont *textFont, uint16_t textColour,
44+
uint16_t backgroundColour, int csPin) {
3945
_rotation = rotation;
4046
_textSize = textSize;
47+
_textSize = 1; // default text size to save amending display routine.
4148
_textColour = textColour;
4249
_backgroundColour = backgroundColour;
4350
_csPin = csPin;
4451
if (_tft == nullptr) {
4552
_tft = new TFT_eSPI();
4653
}
4754
_gfxFont = TEXT_FONT;
55+
_gfxFont = textFont;
4856
}
4957

5058
void TFT_eSPIDisplay::begin() {
@@ -91,6 +99,24 @@ void TFT_eSPIDisplay::displayScreen(Screen *screen) {
9199
_needsRedraw = false;
92100
}
93101

102+
void TFT_eSPIDisplay::displayRow(uint8_t row, const char *text, bool underlined, uint8_t column) {
103+
if (text == nullptr) {
104+
return;
105+
}
106+
_tft->setFreeFont(_gfxFont);
107+
int32_t x = 0;
108+
int32_t y = 0;
109+
_getRowPosition(column, row, x, y);
110+
LOG(LogLevel::LOG_DEBUG, "TFT_eSPIDisplay::displayRow[%d](%d, %s, %d, %d) at X=%d|Y=%d", _displayId, row, text,
111+
underlined, column, x, y);
112+
_tft->setTextColor(_textColour);
113+
LOG(LogLevel::LOG_DEBUG, "setTextColour(0x%04X)", _textColour);
114+
if (column == 0) {
115+
clearRow(row);
116+
}
117+
_tft->drawString(text, x, y);
118+
}
119+
94120
void TFT_eSPIDisplay::clearRow(uint8_t row) {
95121
LOG(LogLevel::LOG_DEBUG, "TFT_eSPIDisplay::clearRow[%d](%d)", _displayId, row);
96122
_tft->setFreeFont(_gfxFont);
@@ -102,6 +128,7 @@ void TFT_eSPIDisplay::clearRow(uint8_t row) {
102128

103129
void TFT_eSPIDisplay::displayStartupInfo(const char *version) {
104130
LOG(LogLevel::LOG_DEBUG, "TFT_eSPIDisplay::displayStartupInfo[%d](%s)", _displayId, version);
131+
_tft->setRotation(_rotation);
105132
_tft->setFreeFont(_gfxFont);
106133
_tft->fillScreen(0xFFFF);
107134
int32_t x = 0;
@@ -169,14 +196,18 @@ TFT_eSPI *TFT_eSPIDisplay::getTFT_eSPIInstance() {
169196
bool TFT_eSPIDisplay::tftInitialised() { return _tftInitialised; }
170197

171198
TFT_eSPIDisplay *TFT_eSPIDisplay::create(uint8_t rotation, uint8_t textSize, uint16_t textColour,
199+
TFT_eSPIDisplay *TFT_eSPIDisplay::create(uint8_t rotation, const GFXfont *textFont, uint16_t textColour,
172200
uint16_t backgroundColour) {
173201
TFT_eSPIDisplay *newDisplay = new TFT_eSPIDisplay(rotation, textSize, textColour, backgroundColour);
202+
TFT_eSPIDisplay *newDisplay = new TFT_eSPIDisplay(rotation, textFont, textColour, backgroundColour);
174203
return newDisplay;
175204
}
176205

177206
TFT_eSPIDisplay *TFT_eSPIDisplay::create(uint8_t rotation, uint8_t textSize, uint16_t textColour,
207+
TFT_eSPIDisplay *TFT_eSPIDisplay::create(uint8_t rotation, const GFXfont *textFont, uint16_t textColour,
178208
uint16_t backgroundColour, int csPin) {
179209
TFT_eSPIDisplay *newDisplay = new TFT_eSPIDisplay(rotation, textSize, textColour, backgroundColour, csPin);
210+
TFT_eSPIDisplay *newDisplay = new TFT_eSPIDisplay(rotation, textFont, textColour, backgroundColour, csPin);
180211
return newDisplay;
181212
}
182213

TFT_eSPIDisplay.h

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,32 @@
3535
#define FONT_TFT_ESPI_MEDIUM_SANS &FreeSans12pt7b
3636
#define FONT_TFT_ESPI_LARGE_SANS &FreeSans18pt7b
3737
#define FONT_TFT_ESPI_XLARGE_SANS &FreeSans24pt7b
38+
#define FONT_TFT_ESPI_MEDIUM_BOLD &FreeMonoBold12pt7b
39+
#define FONT_TFT_ESPI_SERIF_BOLD_ITALIC &FreeSerifBoldItalic12pt7b
3840

3941
// If not overridden by myConfig.h, set the font
4042
#ifndef TEXT_FONT
41-
#define TEXT_FONT FONT_TFT_ESPI_MEDIUM
42-
#endif // TEXT_FONT
43+
#define TEXT_FONT FONT_TFT_ESPI_MEDIUM_BOLD
44+
#endif
4345

46+
// #define LOCAL_FONT FONT_TFT
4447
/// @brief Display class for TFT_eSPI based displays
4548
class TFT_eSPIDisplay : public DisplayInterface {
4649
public:
4750
/// @brief Constructor for a TFT_eSPIDisplay instance
4851
/// @param rotation Rotation of the display, 0 - 3, refer to TFT_eSPI documentation for details
49-
/// @param textSize Multiplier for the text size, refer to TFT_eSPI documentation for details
52+
/// @param textFont The identiy of the font for this display.
5053
/// @param textColour Default 16bit text colour, refer to TFT_eSPI documentation for details
5154
/// @param backgroundColour Default 16bit background colour, refer to TFT_eSPI documentation for details
52-
TFT_eSPIDisplay(uint8_t rotation, uint8_t textSize, uint16_t textColour, uint16_t backgroundColour);
55+
TFT_eSPIDisplay(uint8_t rotation, const GFXfont *textFont, uint16_t textColour, uint16_t backgroundColour);
5356

5457
/// @brief Alternate constructor for a TFT_eSPIDisplay instance to specify the CS pin to allow for two displays
5558
/// @param rotation Rotation of the display, 0 - 3, refer to TFT_eSPI documentation for details
56-
/// @param textSize Multiplier for the text size, refer to TFT_eSPI documentation for details
59+
/// @param textFont The identiy of the font for this display.
5760
/// @param textColour Default 16bit text colour, refer to TFT_eSPI documentation for details
5861
/// @param backgroundColour Default 16bit background colour, refer to TFT_eSPI documentation for details
5962
/// @param csPin Pin this display's chip select (CS) pin is connected to to enable manual display switching
60-
TFT_eSPIDisplay(uint8_t rotation, uint8_t textSize, uint16_t textColour, uint16_t backgroundColour, int csPin);
63+
TFT_eSPIDisplay(uint8_t rotation, const GFXfont *textFont, uint16_t textColour, uint16_t backgroundColour, int csPin);
6164

6265
/// @brief Perform any initial once off setup or configuration here and call only once
6366
void begin() override;
@@ -96,21 +99,22 @@ class TFT_eSPIDisplay : public DisplayInterface {
9699

97100
/// @brief Static method to enable the compiler to generate create commands from myDevices.h entries
98101
/// @param rotation rotation Rotation of the display, 0 - 3, refer to TFT_eSPI documentation for details
99-
/// @param textSize Multiplier for the text size, refer to TFT_eSPI documentation for details
102+
/// @param textFont The identiy of the font for this display.
100103
/// @param textColour Default 16bit text colour, refer to TFT_eSPI documentation for details
101104
/// @param backgroundColour Default 16bit background colour, refer to TFT_eSPI documentation for details
102105
/// @return Pointer to a new TFT_eSPIDisplay instance
103-
static TFT_eSPIDisplay *create(uint8_t rotation, uint8_t textSize, uint16_t textColour, uint16_t backgroundColour);
106+
static TFT_eSPIDisplay *create(uint8_t rotation, const GFXfont *textFont, uint16_t textColour,
107+
uint16_t backgroundColour);
104108

105109
/// @brief Alternate static method to enable the compiler to generate create commands from myDevices.h entries
106110
/// @param rotation rotation Rotation of the display, 0 - 3, refer to TFT_eSPI documentation for details
107-
/// @param textSize Multiplier for the text size, refer to TFT_eSPI documentation for details
111+
/// @param textFont The identiy of the font for this display.
108112
/// @param textColour Default 16bit text colour, refer to TFT_eSPI documentation for details
109113
/// @param backgroundColour Default 16bit background colour, refer to TFT_eSPI documentation for details
110114
/// @param csPin Pin this display's chip select (CS) pin is connected to to enable manual display switching
111115
/// @return Pointer to a new TFT_eSPIDisplay instance
112-
static TFT_eSPIDisplay *create(uint8_t rotation, uint8_t textSize, uint16_t textColour, uint16_t backgroundColour,
113-
int csPin);
116+
static TFT_eSPIDisplay *create(uint8_t rotation, const GFXfont *textFont, uint16_t textColour,
117+
uint16_t backgroundColour, int csPin);
114118

115119
/// @brief Destructor for the TFT_eSPIDisplay
116120
~TFT_eSPIDisplay() override;

myConfig.example.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,11 @@
5151
* @brief Option to force a touch screen to enter calibration mode even if the current calibration is valid
5252
*/
5353
// #define FORCE_CALIBRATION
54+
55+
/**
56+
* @brief Option provide an alternative option from the GFXFF FreeFonts that is not yet defined in the display type header file
57+
* e.g. set the text font to Free Sans Bold Oblique 18pt high. - uncomment the line below
58+
* which will replace the default definition or replace it with another.
59+
*/
60+
61+
// #define TEXT_FONT &FreeSansBoldOblique18pt7b

myDevices.example.h

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,45 @@
3131
/**
3232
* Display devices using the TFT_eSPI display library
3333
*
34-
* USER_DISPLAY(TFT_eSPIDisplay, rotation, text size, text colour, background colour, *chip select pin)
34+
* USER_DISPLAY(TFT_eSPIDisplay, rotation, text font, text colour, background colour, *chip select pin)
3535
*
36-
* Example for a single display with rotation set to 1, text size 1, white text colour, and black background colour
37-
* Omit the final chip select pin parameter for a single display
38-
* USER_DISPLAY(TFT_eSPIDisplay, 1, 1, TFT_WHITE, TFT_BLACK)
36+
* Example for a single display with rotation set to 1, text font FreeMono12pt7b, white text colour, and black background colour
37+
* Omit the final chip select pin parameter for a single display.
38+
* TEXT_FONT is a default font provided in the display header file and is set to FONT_TFT_ESPI_MEDIUM i.e. FreeMono12pt7b.
39+
* If a defferent font is requires substitute the font define for TEXT_FONT in the example below.
40+
* USER_DISPLAY(TFT_eSPIDisplay, 1, TEXT_FONT, TFT_WHITE, TFT_BLACK)
3941
*
4042
* Example for two displays as above, but specifying the chip select pin to manually switch the displays
4143
* This is necessary as the TFT_eSPI library doesn't directly support multiple displays
42-
* USER_DISPLAY(TFT_eSPIDisplay, 1, 1, TFT_WHITE, TFT_BLACK, 15)
43-
* USER_DISPLAY(TFT_eSPIDisplay, 1, 1, TFT_WHITE, TFT_BLACK, 16)
44+
* USER_DISPLAY(TFT_eSPIDisplay, 1, TEXT_FONT, TFT_WHITE, TFT_BLACK, 15)
45+
* USER_DISPLAY(TFT_eSPIDisplay, 1, TEXT_FONT, TFT_WHITE, TFT_BLACK, 16)
46+
*
47+
* The available fonts that are defined for use are listed at the top of the appropriate display header file
48+
* In the case of TFT_eSPI screens that is TFT_eSPIDisplay.h. These are a selection of fonts that have been define from the
49+
* library GFXFF FreeFonts.
50+
*
51+
* If you wish to use a font that is not defined in the header file, it is possible to add an elternative (see config.example.h)
4452
*
4553
* Uncomment and edit one or more lines below as required for your setup.
4654
*/
4755

48-
// USER_DISPLAY(TFT_eSPIDisplay, 1, 1, TFT_WHITE, TFT_BLACK)
49-
// USER_DISPLAY(TFT_eSPIDisplay, 1, 1, TFT_WHITE, TFT_BLACK, 15)
50-
// USER_DISPLAY(TFT_eSPIDisplay, 1, 1, TFT_WHITE, TFT_BLACK, 16)
56+
// USER_DISPLAY(TFT_eSPIDisplay, 1, TEXT_FONT, TFT_WHITE, TFT_BLACK)
57+
// USER_DISPLAY(TFT_eSPIDisplay, 1, TEXT_FONT, TFT_WHITE, TFT_BLACK, 15)
58+
// USER_DISPLAY(TFT_eSPIDisplay, 1, 1TEXT_FONT, TFT_WHITE, TFT_BLACK, 16)
59+
60+
/**
61+
* If using multiple displays on a processor that can support this, it is possible to set a specific font for each screen.
62+
* The following three lines give an example that has been tested with three SPI displays on an ESP32. Each display is
63+
* configured with a different font from the list available.
64+
* Note that each screen has been given a different orientation. This works to a point but has a limitation imposed by the
65+
* library. Orientations 180 degrees opposed work OK, but an orientation 90 degrees to the first will not have the background
66+
* rendered correctly i.e the height and width attributes do not adjust.
67+
* If using mixed orientations its best to use 0 & 2 or 1 & 3.
68+
*
69+
* USER_DISPLAY(TFT_eSPIDisplay, 1, FONT_TFT_ESPI_SMALL, TFT_WHITE, TFT_BLACK, 12)
70+
* USER_DISPLAY(TFT_eSPIDisplay, 2, FONT_TFT_ESPI_SERIF_BOLD_ITALIC, TFT_WHITE, TFT_BLUE, 13)
71+
* USER_DISPLAY(TFT_eSPIDisplay, 3, FONT_TFT_ESPI_LARGE, TFT_WHITE, TFT_RED, 14)
72+
*/
5173

5274
/**
5375
* Display devices using the MCUFRIEND_kbv display library

0 commit comments

Comments
 (0)