acidportal- 😈 Worlds smallest Evil Portal on a LilyGo T-QT |
git clone git://git.acid.vegas/acidportal.git |
Log | Files | Refs | Archive | README | LICENSE |
Free_Font_Demo.ino (9327B)
1 /* 2 This example draws fonts (as used by the Adafruit_GFX library) onto the 3 screen. These fonts are called the GFX Free Fonts (GFXFF) in this library. 4 5 Other True Type fonts could be converted using the utility within the 6 "fontconvert" folder inside the library. This converted has also been 7 copied from the Adafruit_GFX library. 8 9 Since these fonts are a recent addition Adafruit do not have a tutorial 10 available yet on how to use the utility. Linux users will no doubt 11 figure it out! In the meantime there are 48 font files to use in sizes 12 from 9 point to 24 point, and in normal, bold, and italic or oblique 13 styles. 14 15 This example sketch uses both the print class and drawString() functions 16 to plot text to the screen. 17 18 Make sure LOAD_GFXFF is defined in the User_Setup.h file within the 19 TFT_eSPI library folder. 20 21 ######################################################################### 22 ###### DON'T FORGET TO UPDATE THE User_Setup.h FILE IN THE LIBRARY ###### 23 ######################################################################### 24 */ 25 26 #include <TFT_eSPI.h> // Hardware-specific library 27 #include <SPI.h> 28 29 #include "Free_Fonts.h" // Include the header file attached to this sketch 30 31 TFT_eSPI tft = TFT_eSPI(); // Invoke custom library with default width and height 32 33 unsigned long drawTime = 0; 34 35 void setup(void) { 36 37 tft.begin(); 38 39 tft.setRotation(1); 40 41 } 42 43 void loop() { 44 45 int xpos = 0; 46 int ypos = 40; 47 48 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 49 // Select different fonts to draw on screen using the print class 50 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 51 52 tft.fillScreen(TFT_NAVY); // Clear screen to navy background 53 54 header("Draw free fonts using print class"); 55 56 // For comaptibility with Adafruit_GFX library the text background is not plotted when using the print class 57 // even if we specify it. 58 tft.setTextColor(TFT_YELLOW, TFT_BLACK); 59 tft.setCursor(xpos, ypos); // Set cursor near top left corner of screen 60 61 tft.setTextFont(GLCD); // Select the orginal small GLCD font by using NULL or GLCD 62 tft.println(); // Move cursor down a line 63 tft.print("Original GLCD font"); // Print the font name onto the TFT screen 64 tft.println(); 65 tft.println(); 66 67 tft.setFreeFont(FSB9); // Select Free Serif 9 point font, could use: 68 // tft.setFreeFont(&FreeSerif9pt7b); 69 tft.println(); // Free fonts plot with the baseline (imaginary line the letter A would sit on) 70 // as the datum, so we must move the cursor down a line from the 0,0 position 71 tft.print("Serif Bold 9pt"); // Print the font name onto the TFT screen 72 73 tft.setFreeFont(FSB12); // Select Free Serif 12 point font 74 tft.println(); // Move cursor down a line 75 tft.print("Serif Bold 12pt"); // Print the font name onto the TFT screen 76 77 tft.setFreeFont(FSB18); // Select Free Serif 12 point font 78 tft.println(); // Move cursor down a line 79 tft.print("Serif Bold 18pt"); // Print the font name onto the TFT screen 80 81 tft.setFreeFont(FSB24); // Select Free Serif 24 point font 82 tft.println(); // Move cursor down a line 83 tft.print("Serif Bold 24pt"); // Print the font name onto the TFT screen 84 85 86 delay(4000); 87 88 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 89 // Now use drawString() so we can set background colours and the datum 90 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 91 92 tft.fillScreen(TFT_BLACK); 93 94 header("Draw with background using drawString()"); 95 96 tft.setTextColor(TFT_WHITE, TFT_BLACK); 97 98 tft.setTextDatum(TC_DATUM); // Centre text on x,y position 99 100 xpos = tft.width() / 2; // Half the screen width 101 ypos = 50; 102 103 tft.setFreeFont(FSB9); // Select the font 104 tft.drawString("Serif Bold 9pt", xpos, ypos, GFXFF); // Draw the text string in the selected GFX free font 105 ypos += tft.fontHeight(GFXFF); // Get the font height and move ypos down 106 107 tft.setFreeFont(FSB12); 108 tft.drawString("Serif Bold 12pt", xpos, ypos, GFXFF); 109 ypos += tft.fontHeight(GFXFF); 110 111 tft.setFreeFont(FSB18); 112 tft.drawString("Serif Bold 18pt", xpos, ypos, GFXFF); 113 ypos += tft.fontHeight(GFXFF); 114 115 tft.setFreeFont(FSB24); 116 tft.drawString("Serif Bold 24pt", xpos, ypos, GFXFF); 117 ypos += tft.fontHeight(GFXFF); 118 119 // Set text padding to 120 pixels wide area to over-write old values on screen 120 tft.setTextPadding(120); 121 for (int i = 0; i <= 20; i++) { 122 tft.drawFloat(i / 10.0, 1, xpos, ypos, GFXFF); 123 delay (200); 124 } 125 126 delay(4000); 127 128 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 129 // Same again but with colours that show bounding boxes 130 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 131 132 133 tft.fillScreen(TFT_DARKGREY); 134 135 header("Show background filled bounding boxes"); 136 137 tft.setTextColor(TFT_YELLOW, TFT_BLACK); 138 139 tft.setTextDatum(TC_DATUM); // Centre text on x,y position 140 141 xpos = tft.width() / 2; // Half the screen width 142 ypos = 50; 143 144 tft.setFreeFont(FSB9); // Select the font 145 tft.drawString("Serif Bold 9pt", xpos, ypos, GFXFF); // Draw the text string in the selected GFX free font 146 ypos += tft.fontHeight(GFXFF); // Get the font height and move ypos down 147 148 tft.setFreeFont(FSB12); 149 tft.drawString("Serif Bold 12pt", xpos, ypos, GFXFF); 150 ypos += tft.fontHeight(GFXFF); 151 152 tft.setFreeFont(FSB18); 153 tft.drawString("Serif Bold 18pt", xpos, ypos, GFXFF); 154 ypos += tft.fontHeight(GFXFF); 155 156 tft.setFreeFont(FSBI24); 157 tft.drawString("Serif Bold Italic 24pt", xpos, ypos, GFXFF); 158 ypos += tft.fontHeight(GFXFF); 159 160 // Set text padding to 120 pixels wide area to over-write old values on screen 161 tft.setTextPadding(120); 162 for (int i = 0; i <= 20; i++) { 163 tft.drawFloat(i / 10.0, 1, xpos, ypos, GFXFF); 164 delay (200); 165 } 166 167 delay(4000); 168 169 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 170 // Now show setting the 12 datum positions works with free fonts 171 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 172 173 // Numbers, floats and strings can be drawn relative to a datum 174 tft.fillScreen(TFT_BLACK); 175 header("Draw text relative to a datum"); 176 177 tft.setTextColor(TFT_DARKGREY, TFT_BLACK); 178 tft.setFreeFont(FSS9); 179 180 tft.setTextDatum(TL_DATUM); 181 tft.drawString("[Top left}", 20, 60, GFXFF); 182 drawDatum(20,60); 183 184 tft.setTextDatum(TC_DATUM); 185 tft.drawString("[Top centre]", 240, 60, GFXFF); 186 drawDatum(240,60); 187 188 tft.setTextDatum(TR_DATUM); 189 tft.drawString("[Top right]", 460, 60, GFXFF); 190 drawDatum(460,60); 191 192 tft.setTextDatum(ML_DATUM); 193 tft.drawString("[Middle left]", 20, 140, GFXFF); 194 drawDatum(20,140); 195 196 tft.setTextDatum(MC_DATUM); 197 tft.drawString("[Middle centre]", 240, 140, GFXFF); 198 drawDatum(240,140); 199 200 tft.setTextDatum(MR_DATUM); 201 tft.drawString("[Middle right]", 460, 140, GFXFF); 202 drawDatum(460,140); 203 204 tft.setTextDatum(BL_DATUM); 205 tft.drawString("[Bottom Left]", 20, 220, GFXFF); 206 drawDatum(20,220); 207 208 tft.setTextDatum(BC_DATUM); 209 tft.drawString("[Bottom centre]", 240, 220, GFXFF); 210 drawDatum(240,220); 211 212 tft.setTextDatum(BR_DATUM); 213 tft.drawString("[Bottom right]", 460, 220, GFXFF); 214 drawDatum(460,220); 215 216 tft.setTextDatum(L_BASELINE); 217 tft.drawString("[Left baseline]", 20, 300, GFXFF); 218 drawDatum(20,300); 219 220 tft.setTextDatum(C_BASELINE); 221 tft.drawString("[Centre baseline]", 240, 300, GFXFF); 222 drawDatum(240,300); 223 224 tft.setTextDatum(R_BASELINE); 225 tft.drawString("[Right baseline]", 460, 300, GFXFF); 226 drawDatum(460,300); 227 228 //while(1); 229 delay(8000); 230 231 } 232 233 // Print the header for a display screen 234 void header(const char *string) 235 { 236 tft.setTextSize(1); 237 tft.setTextColor(TFT_MAGENTA, TFT_BLUE); 238 tft.fillRect(0, 0, 480, 30, TFT_BLUE); 239 tft.setTextDatum(TC_DATUM); 240 tft.drawString(string, 239, 2, 4); // Font 4 for fast drawing with background 241 } 242 243 // Draw a + mark centred on x,y 244 void drawDatum(int x, int y) 245 { 246 tft.drawLine(x - 5, y, x + 5, y, TFT_GREEN); 247 tft.drawLine(x, y - 5, x, y + 5, TFT_GREEN); 248 } 249 250 251 // There follows a crude way of flagging that this example sketch needs fonts which 252 // have not been enbabled in the User_Setup.h file inside the TFT_HX8357 library. 253 // 254 // These lines produce errors during compile time if settings in User_Setup are not correct 255 // 256 // The error will be "does not name a type" but ignore this and read the text between '' 257 // it will indicate which font or feature needs to be enabled 258 // 259 // Either delete all the following lines if you do not want warnings, or change the lines 260 // to suit your sketch modifications. 261 262 #ifndef LOAD_GLCD 263 //ERROR_Please_enable_LOAD_GLCD_in_User_Setup 264 #endif 265 266 #ifndef LOAD_FONT2 267 //ERROR_Please_enable_LOAD_FONT2_in_User_Setup! 268 #endif 269 270 #ifndef LOAD_FONT4 271 //ERROR_Please_enable_LOAD_FONT4_in_User_Setup! 272 #endif 273 274 #ifndef LOAD_FONT6 275 //ERROR_Please_enable_LOAD_FONT6_in_User_Setup! 276 #endif 277 278 #ifndef LOAD_FONT7 279 //ERROR_Please_enable_LOAD_FONT7_in_User_Setup! 280 #endif 281 282 #ifndef LOAD_FONT8 283 //ERROR_Please_enable_LOAD_FONT8_in_User_Setup! 284 #endif 285 286 #ifndef LOAD_GFXFF 287 ERROR_Please_enable_LOAD_GFXFF_in_User_Setup! 288 #endif 289