|
| 1 | +/** |
| 2 | + * Shows how to use tcUnicodeHelper with TFT_eSPI library. TcUnicode library can render both AdafruitGFX and tcUnicode |
| 3 | + * fonts. The text passed to it is normally in UTF-8 format (modern C++ standard). |
| 4 | + * |
| 5 | + * The Unicode fonts are of type TcUnicode and can be generated by TcMenuDesigner: https://github.com/TcMenu/tcMenu/releases |
| 6 | + * |
| 7 | + * This works by providing a `FontHandler` that is responsible for drawing onto your display, taking over the task |
| 8 | + * of drawing characters from the graphics library. |
| 9 | + * |
| 10 | + * With TFT_eSPI you'll need to configure the display that you want to use yourself first, and then compile and upload |
| 11 | + * this sketch. Probably best to try and upload an example from the library first. |
| 12 | + */ |
| 13 | + |
| 14 | +// before including the graphics library, always include GFXfont from our code. TFTeSPI has an incompatible |
| 15 | +// version of the include. |
| 16 | +#include "UnicodeFontDefs.h" |
| 17 | + |
| 18 | +// Include the graphics library |
| 19 | +#include <TFT_eSPI.h> |
| 20 | +// and then include an Adafruit font, and a tcUnicode font |
| 21 | +#include <Fonts/RobotoMedium18.h> |
| 22 | +#include "Fonts/OpenSansCyrillicLatin18.h" |
| 23 | +// and lastly the fontHandler |
| 24 | +#include "tcUnicodeTFT_eSPI.h" |
| 25 | + |
| 26 | +// |
| 27 | +// For TFTeSPI, we simply create our graphics component `tft` and then we create a `tftPipeline` that draws |
| 28 | +// text onto it. Lastly, we create the `fontHandler` that we use to draw Unicode text onto the display. |
| 29 | +// |
| 30 | +TFT_eSPI tft; |
| 31 | +TftSpiTextPlotPipeline tftPipeline(&tft); |
| 32 | +UnicodeFontHandler fontHandler(&tftPipeline, ENCMODE_UTF8); |
| 33 | + |
| 34 | +// some text to print |
| 35 | +const char* helloText = "Hello World!"; |
| 36 | + |
| 37 | +void setup() { |
| 38 | + SPI.begin(); |
| 39 | + Serial.begin(115200); |
| 40 | + |
| 41 | + tft.begin(); |
| 42 | + tft.fillScreen(ILI9341_BLACK); |
| 43 | + |
| 44 | + // |
| 45 | + // Now get the size of the Adafruit_GFX font that we are using, same as above basically. |
| 46 | + // |
| 47 | + int baselineAda; |
| 48 | + fontHandler.setFont(&RobotoMedium18); |
| 49 | + Coord textSize = fontHandler.textExtents(helloText, &baselineAda); |
| 50 | + int yAdaFontHeight = (int)textSize.y - baselineAda; |
| 51 | + |
| 52 | + Serial.print("Adafruit font height: "); Serial.println(yAdaFontHeight); |
| 53 | + Serial.print("Text size: "); Serial.print(textSize.x); Serial.print(", "); Serial.println(textSize.y); |
| 54 | + |
| 55 | + fontHandler.setDrawColor(ILI9341_YELLOW); |
| 56 | + fontHandler.setFont(&RobotoMedium18); |
| 57 | + fontHandler.setCursor(Coord((tft.width() - textSize.x) / 2, yAdaFontHeight + 10)); |
| 58 | + fontHandler.print(helloText); |
| 59 | + |
| 60 | + Serial.print("Writing text at "); Serial.print((tft.width() - textSize.x) / 2); Serial.print(","); |
| 61 | + Serial.println(yAdaFontHeight + 10); |
| 62 | + |
| 63 | +} |
| 64 | + |
| 65 | +void loop() { |
| 66 | + delay(1000); |
| 67 | + |
| 68 | + tft.fillRect(0, 80, tft.width(), 30, ILI9341_BLACK); |
| 69 | + fontHandler.setDrawColor(ILI9341_WHITE); |
| 70 | + fontHandler.setFont(OpenSansCyrillicLatin18); |
| 71 | + fontHandler.setCursor(Coord(0, 100)); |
| 72 | + fontHandler.print(millis() / 1000); |
| 73 | +} |
| 74 | + |
| 75 | + |
0 commit comments