acid-drop- Hacking the planet from a LilyGo T-Deck using custom firmware |
git clone git://git.acid.vegas/acid-drop.git |
Log | Files | Refs | Archive | README | LICENSE |
Free_Font_Demo.ino (10583B)
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 --------------------------- NOTE ---------------------------------------- 22 The free font encoding format does not lend itself easily to plotting 23 the background without flicker. For values that changes on screen it is 24 better to use Fonts 1- 8 which are encoded specifically for rapid 25 drawing with background. 26 ------------------------------------------------------------------------- 27 28 >>>>>>>>>>>>>>>>>>>>>>>>>>> WARNING <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 29 30 As supplied with the default settings the sketch has 11 fonts loaded, 31 i.e. GLCD (Font 1), Font 2, Font 4, Font 6, Font 7, Font 8 and five Free Fonts, 32 even though they are not all used in the sketch. 33 34 Disable fonts you do not need in User_Setup.h in the library folder. 35 36 ######################################################################### 37 ###### DON'T FORGET TO UPDATE THE User_Setup.h FILE IN THE LIBRARY ###### 38 ######################################################################### 39 */ 40 41 #include "SPI.h" 42 #include "TFT_eSPI.h" 43 44 #include "Free_Fonts.h" // Include the header file attached to this sketch 45 46 // Use hardware SPI 47 TFT_eSPI tft = TFT_eSPI(); 48 49 unsigned long drawTime = 0; 50 51 void setup(void) { 52 53 tft.begin(); 54 55 tft.setRotation(1); 56 57 } 58 59 void loop() { 60 61 int xpos = 0; 62 int ypos = 40; 63 64 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 65 // Select different fonts to draw on screen using the print class 66 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 67 68 header("Using print() method", TFT_NAVY); 69 70 // For comaptibility with Adafruit_GFX library the text background is not plotted when using the print class 71 // even if we specify it. 72 tft.setTextColor(TFT_YELLOW); 73 tft.setCursor(xpos, ypos); // Set cursor near top left corner of screen 74 75 tft.setFreeFont(TT1); // Select the orginal small TomThumb font 76 tft.println(); // Move cursor down a line 77 tft.print("The really tiny TomThumb font"); // Print the font name onto the TFT screen 78 tft.println(); 79 tft.println(); 80 81 tft.setFreeFont(FSB9); // Select Free Serif 9 point font, could use: 82 // tft.setFreeFont(&FreeSerif9pt7b); 83 tft.println(); // Free fonts plot with the baseline (imaginary line the letter A would sit on) 84 // as the datum, so we must move the cursor down a line from the 0,0 position 85 tft.print("Serif Bold 9pt"); // Print the font name onto the TFT screen 86 87 tft.setFreeFont(FSB12); // Select Free Serif 12 point font 88 tft.println(); // Move cursor down a line 89 tft.print("Serif Bold 12pt"); // Print the font name onto the TFT screen 90 91 tft.setFreeFont(FSB18); // Select Free Serif 12 point font 92 tft.println(); // Move cursor down a line 93 tft.print("Serif Bold 18pt"); // Print the font name onto the TFT screen 94 95 tft.setFreeFont(FSB24); // Select Free Serif 24 point font 96 tft.println(); // Move cursor down a line 97 tft.print("Serif Bold 24pt"); // Print the font name onto the TFT screen 98 99 100 delay(4000); 101 102 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 103 // Now use drawString() so we can set font background colours and the datum 104 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 105 106 header("Using drawString()", TFT_BLACK); 107 108 tft.setTextColor(TFT_WHITE, TFT_BLACK); 109 110 tft.setTextDatum(TC_DATUM); // Centre text on x,y position 111 112 xpos = tft.width() / 2; // Half the screen width 113 ypos = 50; 114 115 tft.setFreeFont(FSB9); // Select the font 116 tft.drawString("Serif Bold 9pt", xpos, ypos, GFXFF); // Draw the text string in the selected GFX free font 117 ypos += tft.fontHeight(GFXFF); // Get the font height and move ypos down 118 119 tft.setFreeFont(FSB12); 120 tft.drawString("Serif Bold 12pt", xpos, ypos, GFXFF); 121 ypos += tft.fontHeight(GFXFF); 122 123 tft.setFreeFont(FSB18); 124 tft.drawString("Serif Bold 18pt", xpos, ypos, GFXFF); 125 ypos += tft.fontHeight(GFXFF); 126 127 tft.setFreeFont(FSB24); 128 tft.drawString("Serif Bold 24pt", xpos, ypos, GFXFF); 129 ypos += tft.fontHeight(GFXFF); 130 131 // Set text padding to 100 pixels wide area to over-write old values on screen 132 tft.setTextPadding(100); 133 for (int i = 0; i <= 20; i++) { 134 tft.drawFloat(i / 10.0, 1, xpos, ypos, GFXFF); 135 delay (200); 136 } 137 138 delay(4000); 139 140 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 141 // Same again but with colours that show bounding boxes 142 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 143 144 145 header("With background", TFT_DARKGREY); 146 147 tft.setTextColor(TFT_YELLOW, TFT_BLACK); 148 149 tft.setTextDatum(TC_DATUM); // Centre text on x,y position 150 151 xpos = tft.width() / 2; // Half the screen width 152 ypos = 50; 153 154 tft.setFreeFont(FSB9); // Select the font 155 tft.drawString("Serif Bold 9pt", xpos, ypos, GFXFF); // Draw the text string in the selected GFX free font 156 ypos += tft.fontHeight(GFXFF); // Get the font height and move ypos down 157 158 tft.setFreeFont(FSB12); 159 tft.drawString("Serif Bold 12pt", xpos, ypos, GFXFF); 160 ypos += tft.fontHeight(GFXFF); 161 162 tft.setFreeFont(FSB18); 163 tft.drawString("Serif Bold 18pt", xpos, ypos, GFXFF); 164 ypos += tft.fontHeight(GFXFF); 165 166 tft.setFreeFont(FSBI24); 167 tft.drawString("Bold Italic 24pt", xpos, ypos, GFXFF); 168 ypos += tft.fontHeight(GFXFF); 169 170 // Set text padding to 100 pixels wide area to over-write old values on screen 171 tft.setTextPadding(100); 172 for (int i = 0; i <= 20; i++) { 173 tft.drawFloat(i / 10.0, 1, xpos, ypos, GFXFF); 174 delay (200); 175 } 176 177 delay(4000); 178 179 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 180 // Now show setting the 12 datum positions works with free fonts 181 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 182 183 // Numbers, floats and strings can be drawn relative to a datum 184 header("Text with a datum", TFT_BLACK); 185 tft.setTextColor(TFT_DARKGREY, TFT_BLACK); 186 tft.setFreeFont(FSS12); 187 tft.setTextDatum(TL_DATUM); 188 tft.drawString("[Top left]", 160, 120, GFXFF); 189 drawDatumMarker(160,120); 190 delay(1000); 191 192 tft.fillRect(0, 80, 320, 80, TFT_BLACK); 193 tft.setTextDatum(TC_DATUM); 194 tft.drawString("[Top centre]", 160, 120, GFXFF); 195 drawDatumMarker(160,120); 196 delay(1000); 197 198 tft.fillRect(0, 80, 320, 80, TFT_BLACK); 199 tft.setTextDatum(TR_DATUM); 200 tft.drawString("[Top right]", 160, 120, GFXFF); 201 drawDatumMarker(160,120); 202 delay(1000); 203 204 tft.fillRect(0, 80, 320, 80, TFT_BLACK); 205 tft.setTextDatum(ML_DATUM); 206 tft.drawString("[Middle left]", 160, 120, GFXFF); 207 drawDatumMarker(160,120); 208 delay(1000); 209 210 tft.fillRect(0, 80, 320, 80, TFT_BLACK); 211 tft.setTextDatum(MC_DATUM); 212 tft.drawString("[Middle centre]", 160, 120, GFXFF); 213 drawDatumMarker(160,120); 214 delay(1000); 215 216 tft.fillRect(0, 80, 320, 80, TFT_BLACK); 217 tft.setTextDatum(MR_DATUM); 218 tft.drawString("[Middle right]", 160, 120, GFXFF); 219 drawDatumMarker(160,120); 220 delay(1000); 221 222 tft.fillRect(0, 80, 320, 80, TFT_BLACK); 223 tft.setTextDatum(BL_DATUM); 224 tft.drawString("[Bottom left]", 160, 120, GFXFF); 225 drawDatumMarker(160,120); 226 delay(1000); 227 228 tft.fillRect(0, 80, 320, 80, TFT_BLACK); 229 tft.setTextDatum(BC_DATUM); 230 tft.drawString("[Bottom centre]", 160, 120, GFXFF); 231 drawDatumMarker(160,120); 232 delay(1000); 233 234 tft.fillRect(0, 80, 320, 80, TFT_BLACK); 235 tft.setTextDatum(BR_DATUM); 236 tft.drawString("[Bottom right]", 160, 120, GFXFF); 237 drawDatumMarker(160,120); 238 delay(1000); 239 240 tft.fillRect(0, 80, 320, 80, TFT_BLACK); 241 tft.setTextDatum(L_BASELINE); 242 tft.drawString("[Left baseline]", 160, 120, GFXFF); 243 drawDatumMarker(160,120); 244 delay(1000); 245 246 tft.fillRect(0, 80, 320, 80, TFT_BLACK); 247 tft.setTextDatum(C_BASELINE); 248 tft.drawString("[Centre baseline]", 160, 120, GFXFF); 249 drawDatumMarker(160,120); 250 delay(1000); 251 252 tft.fillRect(0, 80, 320, 80, TFT_BLACK); 253 tft.setTextDatum(R_BASELINE); 254 tft.drawString("[Right baseline]", 160, 120, GFXFF); 255 drawDatumMarker(160,120); 256 delay(1000); 257 258 //while(1); 259 delay(8000); 260 261 } 262 263 // Print the header for a display screen 264 void header(const char *string, uint16_t color) 265 { 266 tft.fillScreen(color); 267 tft.setTextSize(1); 268 tft.setTextColor(TFT_MAGENTA, TFT_BLUE); 269 tft.fillRect(0, 0, 320, 30, TFT_BLUE); 270 tft.setTextDatum(TC_DATUM); 271 tft.drawString(string, 160, 2, 4); // Font 4 for fast drawing with background 272 } 273 274 // Draw a + mark centred on x,y 275 void drawDatumMarker(int x, int y) 276 { 277 tft.drawLine(x - 5, y, x + 5, y, TFT_GREEN); 278 tft.drawLine(x, y - 5, x, y + 5, TFT_GREEN); 279 } 280 281 282 // There follows a crude way of flagging that this example sketch needs fonts which 283 // have not been enbabled in the User_Setup.h file inside the TFT_HX8357 library. 284 // 285 // These lines produce errors during compile time if settings in User_Setup are not correct 286 // 287 // The error will be "does not name a type" but ignore this and read the text between '' 288 // it will indicate which font or feature needs to be enabled 289 // 290 // Either delete all the following lines if you do not want warnings, or change the lines 291 // to suit your sketch modifications. 292 293 #ifndef LOAD_GLCD 294 //ERROR_Please_enable_LOAD_GLCD_in_User_Setup 295 #endif 296 297 #ifndef LOAD_FONT2 298 //ERROR_Please_enable_LOAD_FONT2_in_User_Setup! 299 #endif 300 301 #ifndef LOAD_FONT4 302 //ERROR_Please_enable_LOAD_FONT4_in_User_Setup! 303 #endif 304 305 #ifndef LOAD_FONT6 306 //ERROR_Please_enable_LOAD_FONT6_in_User_Setup! 307 #endif 308 309 #ifndef LOAD_FONT7 310 //ERROR_Please_enable_LOAD_FONT7_in_User_Setup! 311 #endif 312 313 #ifndef LOAD_FONT8 314 //ERROR_Please_enable_LOAD_FONT8_in_User_Setup! 315 #endif 316 317 #ifndef LOAD_GFXFF 318 ERROR_Please_enable_LOAD_GFXFF_in_User_Setup! 319 #endif 320