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 |
Font_Demo_2.ino (7165B)
1 /* 2 There are four different methods of plotting anti-aliased fonts to the screen. 3 4 This sketch uses method 2, using graphics calls plotting direct to the TFT: 5 tft.drawString(string, x, y); 6 tft.drawNumber(integer, x, y); 7 tft.drawFloat(float, dp, x, y); // dp = number of decimal places 8 9 setTextDatum() and setTextPadding() functions work with those draw functions. 10 11 This method is good for static text that does not change often because changing 12 values may flicker. 13 14 */ 15 // The fonts used are in the sketch data folder, press Ctrl+K to view. 16 17 // Upload the fonts and icons to SPIFFS (must set at least 1M for SPIFFS) using the 18 // "Tools" "ESP8266 (or ESP32) Sketch Data Upload" menu option in the IDE. 19 // To add this option follow instructions here for the ESP8266: 20 // https://github.com/esp8266/arduino-esp8266fs-plugin 21 // or for the ESP32: 22 // https://github.com/me-no-dev/arduino-esp32fs-plugin 23 24 // Close the IDE and open again to see the new menu option. 25 26 // A processing sketch to create new fonts can be found in the Tools folder of TFT_eSPI 27 // https://github.com/Bodmer/TFT_eSPI/tree/master/Tools/Create_Smooth_Font/Create_font 28 29 // This sketch uses font files created from the Noto family of fonts: 30 // https://www.google.com/get/noto/ 31 32 #define AA_FONT_SMALL "NotoSansBold15" 33 #define AA_FONT_LARGE "NotoSansBold36" 34 35 // Font files are stored in SPIFFS, so load the library 36 #include <FS.h> 37 38 #include <SPI.h> 39 #include <TFT_eSPI.h> // Hardware-specific library 40 41 TFT_eSPI tft = TFT_eSPI(); 42 43 void setup(void) { 44 45 Serial.begin(250000); 46 47 tft.begin(); 48 49 tft.setRotation(1); 50 51 if (!SPIFFS.begin()) { 52 Serial.println("SPIFFS initialisation failed!"); 53 while (1) yield(); // Stay here twiddling thumbs waiting 54 } 55 Serial.println("\r\nSPIFFS available!"); 56 57 // ESP32 will crash if any of the fonts are missing 58 bool font_missing = false; 59 if (SPIFFS.exists("/NotoSansBold15.vlw") == false) font_missing = true; 60 if (SPIFFS.exists("/NotoSansBold36.vlw") == false) font_missing = true; 61 62 if (font_missing) 63 { 64 Serial.println("\r\nFont missing in SPIFFS, did you upload it?"); 65 while(1) yield(); 66 } 67 else Serial.println("\r\nFonts found OK."); 68 } 69 70 void loop() { 71 72 tft.fillScreen(TFT_BLACK); 73 74 tft.setTextColor(TFT_WHITE, TFT_BLACK); // Set the font colour and the background colour 75 76 tft.setTextDatum(TC_DATUM); // Top Centre datum 77 78 int xpos = tft.width() / 2; // Half the screen width 79 int ypos = 10; 80 81 82 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 83 // Small font 84 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 85 86 tft.loadFont(AA_FONT_SMALL); // Must load the font first 87 88 tft.drawString("Small 15pt font", xpos, ypos); 89 90 ypos += tft.fontHeight(); // Get the font height and move ypos down 91 92 tft.setTextColor(TFT_GREEN, TFT_BLACK); 93 94 // If the string does not fit the screen width, then the next character will wrap to a new line 95 tft.drawString("Ode To A Small Lump Of Green Putty I Found In My Armpit One Midsummer Morning", xpos, ypos); 96 97 tft.setTextColor(TFT_GREEN, TFT_BLUE); // Background colour does not match the screen background! 98 tft.drawString("Anti-aliasing causes odd looking shadow effects if the text and screen background colours are not the same!", xpos, ypos + 60); 99 100 tft.unloadFont(); // Remove the font to recover memory used 101 102 delay(5000); 103 104 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 105 // Large font 106 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 107 108 tft.loadFont(AA_FONT_LARGE); // Load another different font 109 110 tft.fillScreen(TFT_BLACK); 111 112 // The "true" parameter forces background drawing for smooth fonts 113 tft.setTextColor(TFT_GREEN, TFT_BLUE, true); // Change the font colour and the background colour 114 115 tft.drawString("36pt font", xpos, ypos); 116 117 ypos += tft.fontHeight(); // Get the font height and move ypos down 118 119 // Set text padding to 100 pixels wide area to over-write old values on screen 120 tft.setTextPadding(100); 121 122 // Draw changing numbers - likely to flicker using this plot method! 123 for (int i = 0; i <= 20; i++) { 124 tft.drawFloat(i / 10.0, 1, xpos, ypos); 125 delay (200); 126 } 127 128 // Turn off text padding by setting value to 0 129 tft.setTextPadding(0); 130 131 tft.unloadFont(); // Remove the font to recover memory used 132 133 delay(5000); 134 135 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 136 // Setting the 12 datum positions works with free fonts 137 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 138 139 // Integer numbers, floats and strings can be drawn relative to a x,y datum, e.g.: 140 // tft.drawNumber( 123, x, y); 141 // tft.drawFloat( 1.23, dp, x, y); // Where dp is number of decimal places to show 142 // tft.drawString( "Abc", x, y); 143 144 tft.fillScreen(TFT_BLACK); 145 146 tft.setTextColor(TFT_DARKGREY, TFT_BLACK, false); 147 148 // Use middle of screen as datum 149 xpos = tft.width() /2; 150 ypos = tft.height()/2; 151 152 tft.loadFont(AA_FONT_SMALL); 153 tft.setTextDatum(TL_DATUM); 154 tft.drawString("[Top left]", xpos, ypos); 155 drawDatumMarker(xpos, ypos); 156 delay(1000); 157 158 tft.fillScreen(TFT_BLACK); 159 tft.setTextDatum(TC_DATUM); 160 tft.drawString("[Top centre]", xpos, ypos); 161 drawDatumMarker(xpos, ypos); 162 delay(1000); 163 164 tft.fillScreen(TFT_BLACK); 165 tft.setTextDatum(TR_DATUM); 166 tft.drawString("[Top right]", xpos, ypos); 167 drawDatumMarker(xpos, ypos); 168 delay(1000); 169 170 tft.fillScreen(TFT_BLACK); 171 tft.setTextDatum(ML_DATUM); 172 tft.drawString("[Middle left]", xpos, ypos); 173 drawDatumMarker(xpos, ypos); 174 delay(1000); 175 176 tft.fillScreen(TFT_BLACK); 177 tft.setTextDatum(MC_DATUM); 178 tft.drawString("[Middle centre]", xpos, ypos); 179 drawDatumMarker(xpos, ypos); 180 delay(1000); 181 182 tft.fillScreen(TFT_BLACK); 183 tft.setTextDatum(MR_DATUM); 184 tft.drawString("[Middle right]", xpos, ypos); 185 drawDatumMarker(xpos, ypos); 186 delay(1000); 187 188 tft.fillScreen(TFT_BLACK); 189 tft.setTextDatum(BL_DATUM); 190 tft.drawString("[Bottom left]", xpos, ypos); 191 drawDatumMarker(xpos, ypos); 192 delay(1000); 193 194 tft.fillScreen(TFT_BLACK); 195 tft.setTextDatum(BC_DATUM); 196 tft.drawString("[Bottom centre]", xpos, ypos); 197 drawDatumMarker(xpos, ypos); 198 delay(1000); 199 200 tft.fillScreen(TFT_BLACK); 201 tft.setTextDatum(BR_DATUM); 202 tft.drawString("[Bottom right]", xpos, ypos); 203 drawDatumMarker(xpos, ypos); 204 delay(1000); 205 206 tft.fillScreen(TFT_BLACK); 207 tft.setTextDatum(L_BASELINE); 208 tft.drawString("[Left baseline]", xpos, ypos); 209 drawDatumMarker(xpos, ypos); 210 delay(1000); 211 212 tft.fillScreen(TFT_BLACK); 213 tft.setTextDatum(C_BASELINE); 214 tft.drawString("[Centre baseline]", xpos, ypos); 215 drawDatumMarker(xpos, ypos); 216 delay(1000); 217 218 tft.fillScreen(TFT_BLACK); 219 tft.setTextDatum(R_BASELINE); 220 tft.drawString("[Right baseline]", xpos, ypos); 221 drawDatumMarker(xpos, ypos); 222 delay(1000); 223 224 tft.unloadFont(); // Remove the font to recover memory used 225 226 delay(4000); 227 228 } 229 230 // Draw a + mark centred on x,y 231 void drawDatumMarker(int x, int y) 232 { 233 tft.drawLine(x - 5, y, x + 5, y, TFT_GREEN); 234 tft.drawLine(x, y - 5, x, y + 5, TFT_GREEN); 235 }