Skip to content

Commit

Permalink
Merge multi-font
Browse files Browse the repository at this point in the history
Merge Multi Font to Main
  • Loading branch information
ColinHMurdoch committed Jan 13, 2025
2 parents 1f81e8e + 0c68ac7 commit 8225398
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 20 deletions.
31 changes: 31 additions & 0 deletions TFT_eSPIDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,35 @@ TFT_eSPI *TFT_eSPIDisplay::_tft = nullptr;
bool TFT_eSPIDisplay::_tftInitialised = false;

TFT_eSPIDisplay::TFT_eSPIDisplay(uint8_t rotation, uint8_t textSize, uint16_t textColour, uint16_t backgroundColour) {
TFT_eSPIDisplay::TFT_eSPIDisplay(uint8_t rotation, const GFXfont *textFont, uint16_t textColour,
uint16_t backgroundColour) {
_rotation = rotation;
_textSize = textSize;
_textSize = 1; // default text size to save amending display routine.
_textColour = textColour;
_backgroundColour = backgroundColour;
if (_tft == nullptr) {
_tft = new TFT_eSPI();
}
_gfxFont = TEXT_FONT;
_gfxFont = textFont;
}

TFT_eSPIDisplay::TFT_eSPIDisplay(uint8_t rotation, uint8_t textSize, uint16_t textColour, uint16_t backgroundColour,
int csPin) {
TFT_eSPIDisplay::TFT_eSPIDisplay(uint8_t rotation, const GFXfont *textFont, uint16_t textColour,
uint16_t backgroundColour, int csPin) {
_rotation = rotation;
_textSize = textSize;
_textSize = 1; // default text size to save amending display routine.
_textColour = textColour;
_backgroundColour = backgroundColour;
_csPin = csPin;
if (_tft == nullptr) {
_tft = new TFT_eSPI();
}
_gfxFont = TEXT_FONT;
_gfxFont = textFont;
}

void TFT_eSPIDisplay::begin() {
Expand Down Expand Up @@ -91,6 +99,24 @@ void TFT_eSPIDisplay::displayScreen(Screen *screen) {
_needsRedraw = false;
}

void TFT_eSPIDisplay::displayRow(uint8_t row, const char *text, bool underlined, uint8_t column) {
if (text == nullptr) {
return;
}
_tft->setFreeFont(_gfxFont);
int32_t x = 0;
int32_t y = 0;
_getRowPosition(column, row, x, y);
LOG(LogLevel::LOG_DEBUG, "TFT_eSPIDisplay::displayRow[%d](%d, %s, %d, %d) at X=%d|Y=%d", _displayId, row, text,
underlined, column, x, y);
_tft->setTextColor(_textColour);
LOG(LogLevel::LOG_DEBUG, "setTextColour(0x%04X)", _textColour);
if (column == 0) {
clearRow(row);
}
_tft->drawString(text, x, y);
}

void TFT_eSPIDisplay::clearRow(uint8_t row) {
LOG(LogLevel::LOG_DEBUG, "TFT_eSPIDisplay::clearRow[%d](%d)", _displayId, row);
_tft->setFreeFont(_gfxFont);
Expand All @@ -102,6 +128,7 @@ void TFT_eSPIDisplay::clearRow(uint8_t row) {

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

TFT_eSPIDisplay *TFT_eSPIDisplay::create(uint8_t rotation, uint8_t textSize, uint16_t textColour,
TFT_eSPIDisplay *TFT_eSPIDisplay::create(uint8_t rotation, const GFXfont *textFont, uint16_t textColour,
uint16_t backgroundColour) {
TFT_eSPIDisplay *newDisplay = new TFT_eSPIDisplay(rotation, textSize, textColour, backgroundColour);
TFT_eSPIDisplay *newDisplay = new TFT_eSPIDisplay(rotation, textFont, textColour, backgroundColour);
return newDisplay;
}

TFT_eSPIDisplay *TFT_eSPIDisplay::create(uint8_t rotation, uint8_t textSize, uint16_t textColour,
TFT_eSPIDisplay *TFT_eSPIDisplay::create(uint8_t rotation, const GFXfont *textFont, uint16_t textColour,
uint16_t backgroundColour, int csPin) {
TFT_eSPIDisplay *newDisplay = new TFT_eSPIDisplay(rotation, textSize, textColour, backgroundColour, csPin);
TFT_eSPIDisplay *newDisplay = new TFT_eSPIDisplay(rotation, textFont, textColour, backgroundColour, csPin);
return newDisplay;
}

Expand Down
26 changes: 15 additions & 11 deletions TFT_eSPIDisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,32 @@
#define FONT_TFT_ESPI_MEDIUM_SANS &FreeSans12pt7b
#define FONT_TFT_ESPI_LARGE_SANS &FreeSans18pt7b
#define FONT_TFT_ESPI_XLARGE_SANS &FreeSans24pt7b
#define FONT_TFT_ESPI_MEDIUM_BOLD &FreeMonoBold12pt7b
#define FONT_TFT_ESPI_SERIF_BOLD_ITALIC &FreeSerifBoldItalic12pt7b

// If not overridden by myConfig.h, set the font
#ifndef TEXT_FONT
#define TEXT_FONT FONT_TFT_ESPI_MEDIUM
#endif // TEXT_FONT
#define TEXT_FONT FONT_TFT_ESPI_MEDIUM_BOLD
#endif

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

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

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

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

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

/// @brief Destructor for the TFT_eSPIDisplay
~TFT_eSPIDisplay() override;
Expand Down
8 changes: 8 additions & 0 deletions myConfig.example.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,11 @@
* @brief Option to force a touch screen to enter calibration mode even if the current calibration is valid
*/
// #define FORCE_CALIBRATION

/**
* @brief Option provide an alternative option from the GFXFF FreeFonts that is not yet defined in the display type header file
* e.g. set the text font to Free Sans Bold Oblique 18pt high. - uncomment the line below
* which will replace the default definition or replace it with another.
*/

// #define TEXT_FONT &FreeSansBoldOblique18pt7b
40 changes: 31 additions & 9 deletions myDevices.example.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,45 @@
/**
* Display devices using the TFT_eSPI display library
*
* USER_DISPLAY(TFT_eSPIDisplay, rotation, text size, text colour, background colour, *chip select pin)
* USER_DISPLAY(TFT_eSPIDisplay, rotation, text font, text colour, background colour, *chip select pin)
*
* Example for a single display with rotation set to 1, text size 1, white text colour, and black background colour
* Omit the final chip select pin parameter for a single display
* USER_DISPLAY(TFT_eSPIDisplay, 1, 1, TFT_WHITE, TFT_BLACK)
* Example for a single display with rotation set to 1, text font FreeMono12pt7b, white text colour, and black background colour
* Omit the final chip select pin parameter for a single display.
* TEXT_FONT is a default font provided in the display header file and is set to FONT_TFT_ESPI_MEDIUM i.e. FreeMono12pt7b.
* If a defferent font is requires substitute the font define for TEXT_FONT in the example below.
* USER_DISPLAY(TFT_eSPIDisplay, 1, TEXT_FONT, TFT_WHITE, TFT_BLACK)
*
* Example for two displays as above, but specifying the chip select pin to manually switch the displays
* This is necessary as the TFT_eSPI library doesn't directly support multiple displays
* USER_DISPLAY(TFT_eSPIDisplay, 1, 1, TFT_WHITE, TFT_BLACK, 15)
* USER_DISPLAY(TFT_eSPIDisplay, 1, 1, TFT_WHITE, TFT_BLACK, 16)
* USER_DISPLAY(TFT_eSPIDisplay, 1, TEXT_FONT, TFT_WHITE, TFT_BLACK, 15)
* USER_DISPLAY(TFT_eSPIDisplay, 1, TEXT_FONT, TFT_WHITE, TFT_BLACK, 16)
*
* The available fonts that are defined for use are listed at the top of the appropriate display header file
* In the case of TFT_eSPI screens that is TFT_eSPIDisplay.h. These are a selection of fonts that have been define from the
* library GFXFF FreeFonts.
*
* 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)
*
* Uncomment and edit one or more lines below as required for your setup.
*/

// USER_DISPLAY(TFT_eSPIDisplay, 1, 1, TFT_WHITE, TFT_BLACK)
// USER_DISPLAY(TFT_eSPIDisplay, 1, 1, TFT_WHITE, TFT_BLACK, 15)
// USER_DISPLAY(TFT_eSPIDisplay, 1, 1, TFT_WHITE, TFT_BLACK, 16)
// USER_DISPLAY(TFT_eSPIDisplay, 1, TEXT_FONT, TFT_WHITE, TFT_BLACK)
// USER_DISPLAY(TFT_eSPIDisplay, 1, TEXT_FONT, TFT_WHITE, TFT_BLACK, 15)
// USER_DISPLAY(TFT_eSPIDisplay, 1, 1TEXT_FONT, TFT_WHITE, TFT_BLACK, 16)

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

/**
* Display devices using the MCUFRIEND_kbv display library
Expand Down

0 comments on commit 8225398

Please sign in to comment.