Skip to content

Commit d6d5d0f

Browse files
committed
updated examples
1 parent d514bec commit d6d5d0f

3 files changed

Lines changed: 134 additions & 12 deletions

File tree

examples/adafruitGfxUnicode/adafruitGfxUnicode.ino

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
11
/**
2-
* Shows how to write unicode characters with any Adafruit_GFX compatible library. The unicode fonts are of type
3-
* TcUnicode and can be generated by TcMenuDesigner: https://github.com/TcMenu/tcMenu/releases
2+
* Shows how to use tcUnicodeHelper with Adafruit based libraries. TcUnicode library can render both AdafruitGFX
3+
* and tcUnicode fonts.
4+
*
5+
* The Unicode fonts are of type TcUnicode and can be generated by TcMenuDesigner: https://github.com/TcMenu/tcMenu/releases
46
*
57
* This works by providing a `FontHandler` that is responsible for drawing onto your display, taking over the task
68
* of drawing characters from the graphics library.
79
*/
10+
811
#include <Adafruit_GFX.h>
9-
#include "tcUnicodeAdaGFX.h"
1012
#include <Adafruit_ILI9341.h>
1113
#include <SPI.h>
1214
#include <Wire.h>
1315
#include <Fonts/RobotoMedium18.h>
1416
#include "Fonts/OpenSansCyrillicLatin18.h"
1517

1618
#define TFT_CS 22 // Chip select control pin
17-
#define TFT_DC 17 // Data Command control pin
18-
#define TFT_RST 16 // Reset pin (could connect to Arduino RESET pin)
19+
#define TFT_DC 21 // Data Command control pin
20+
#define TFT_RST -1 // Reset pin (could connect to Arduino RESET pin)
1921

2022
//
21-
// Here we create an adafruit display, but the font handler works equally well with U8G2, and TFT_eSPI displays.
22-
// It also works with TcMenu drawable interface and the designer can generate suitable themes.
23+
// For Adafruit, we simply create our adafruit graphics component `tft` and then we create a `tftPipeline` that draws
24+
// text onto it. Lastly, we create the `fontHandler` that we use to draw Unicode text onto the display.
2325
//
26+
#include "tcUnicodeAdaGFX.h"
2427
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
2528
AdafruitTextPlotPipeline tftPipeline(&tft);
26-
27-
//
28-
// Create an object that can draw fonts onto the display, this shows adafruit but
29-
// if using U8G2 or TFT_eSPI you'd simply pass the pointer to your display object instead.
30-
//
3129
UnicodeFontHandler fontHandler(&tftPipeline, ENCMODE_UTF8);
3230

3331
int yAdaFontSize = 0;
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+

examples/helloU8G2/helloU8G2.ino

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Shows how to use tcUnicodeHelper with U8G2 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+
11+
#include <HardwareSerial.h>
12+
#include <SPI.h>
13+
#include <Wire.h>
14+
#include <U8g2lib.h>
15+
#include "Fonts/OpenSansRegular7pt.h"
16+
#include "tcUnicodeU8G2.h"
17+
18+
// For U8G2 based displays, simply create the display as usual, and then create a pipeline and handler.
19+
U8G2_SH1106_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, 5, 4, U8X8_PIN_NONE);
20+
U8g2TextPlotPipeline u8g2Pipeline(&u8g2);
21+
UnicodeFontHandler fontHandler(&u8g2Pipeline, ENCMODE_UTF8);
22+
23+
void setup() {
24+
u8g2.begin();
25+
int baseline;
26+
fontHandler.setFont(OpenSansRegular7pt);
27+
Coord textSize = fontHandler.textExtents("Abcdef", &baseline);
28+
int yFontHeight = (int)textSize.y - baseline;
29+
Serial.print("Font height is "); Serial.println(yFontHeight);
30+
31+
fontHandler.setDrawColor(1);
32+
fontHandler.setFont(OpenSansRegular7pt);
33+
fontHandler.setCursor(Coord(0, 10));
34+
fontHandler.print("Hello world");
35+
u8g2.sendBuffer();
36+
}
37+
38+
void loop() {
39+
delay(1000);
40+
41+
u8g2.setColorIndex(0);
42+
u8g2.drawBox(0, 10, 100, 20);
43+
44+
fontHandler.setDrawColor(1);
45+
fontHandler.setFont(OpenSansRegular7pt);
46+
fontHandler.setCursor(Coord(0, 20));
47+
fontHandler.print(millis() / 1000);
48+
u8g2.sendBuffer();
49+
}

0 commit comments

Comments
 (0)