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_1.ino (6292B)
1 /* 2 There are four different methods of plotting anti-aliased fonts to the screen. 3 4 This sketch uses method 1, using tft.print() and tft.println() calls. 5 6 In some cases the sketch shows what can go wrong too, so read the comments! 7 8 The font is rendered WITHOUT a background, but a background colour needs to be 9 set so the anti-aliasing of the character is performed correctly. This is because 10 characters are drawn one by one. 11 12 This method is good for static text that does not change often because changing 13 values may flicker. The text appears at the tft cursor coordinates. 14 15 It is also possible to "print" text directly into a created sprite, for example using 16 spr.println("Hello"); and then push the sprite to the screen. That method is not 17 demonstrated in this sketch. 18 19 */ 20 // The fonts used are in the sketch data folder, press Ctrl+K to view. 21 22 // Upload the fonts and icons to SPIFFS (must set at least 1M for SPIFFS) using the 23 // "Tools" "ESP8266 (or ESP32) Sketch Data Upload" menu option in the IDE. 24 // To add this option follow instructions here for the ESP8266: 25 // https://github.com/esp8266/arduino-esp8266fs-plugin 26 // or for the ESP32: 27 // https://github.com/me-no-dev/arduino-esp32fs-plugin 28 29 // Close the IDE and open again to see the new menu option. 30 31 // A processing sketch to create new fonts can be found in the Tools folder of TFT_eSPI 32 // https://github.com/Bodmer/TFT_eSPI/tree/master/Tools/Create_Smooth_Font/Create_font 33 34 // This sketch uses font files created from the Noto family of fonts: 35 // https://www.google.com/get/noto/ 36 37 #define AA_FONT_SMALL "NotoSansBold15" 38 #define AA_FONT_LARGE "NotoSansBold36" 39 40 // Font files are stored in SPIFFS, so load the library 41 #include <FS.h> 42 43 #include <SPI.h> 44 #include <TFT_eSPI.h> // Hardware-specific library 45 46 TFT_eSPI tft = TFT_eSPI(); 47 48 49 void setup(void) { 50 51 Serial.begin(250000); 52 53 tft.begin(); 54 55 tft.setRotation(0); 56 57 if (!SPIFFS.begin()) { 58 Serial.println("SPIFFS initialisation failed!"); 59 while (1) yield(); // Stay here twiddling thumbs waiting 60 } 61 Serial.println("\r\nSPIFFS available!"); 62 63 // ESP32 will crash if any of the fonts are missing 64 bool font_missing = false; 65 if (SPIFFS.exists("/NotoSansBold15.vlw") == false) font_missing = true; 66 if (SPIFFS.exists("/NotoSansBold36.vlw") == false) font_missing = true; 67 68 if (font_missing) 69 { 70 Serial.println("\r\nFont missing in SPIFFS, did you upload it?"); 71 while(1) yield(); 72 } 73 else Serial.println("\r\nFonts found OK."); 74 } 75 76 77 void loop() { 78 79 tft.fillScreen(TFT_BLACK); 80 81 tft.setTextColor(TFT_WHITE, TFT_BLACK); // Set the font colour AND the background colour 82 // so the anti-aliasing works 83 84 tft.setCursor(0, 0); // Set cursor at top left of screen 85 86 87 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 88 // Small font 89 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 90 91 tft.loadFont(AA_FONT_SMALL); // Must load the font first 92 93 tft.println("Small 15pt font"); // println moves cursor down for a new line 94 95 tft.println(); // New line 96 97 tft.print("ABC"); // print leaves cursor at end of line 98 99 tft.setTextColor(TFT_CYAN, TFT_BLACK); 100 tft.println("1234"); // Added to line after ABC 101 102 tft.setTextColor(TFT_YELLOW, TFT_BLACK); 103 // print stream formatting can be used,see: 104 // https://www.arduino.cc/en/Serial/Print 105 int ivalue = 1234; 106 tft.println(ivalue); // print as an ASCII-encoded decimal 107 tft.println(ivalue, DEC); // print as an ASCII-encoded decimal 108 tft.println(ivalue, HEX); // print as an ASCII-encoded hexadecimal 109 tft.println(ivalue, OCT); // print as an ASCII-encoded octal 110 tft.println(ivalue, BIN); // print as an ASCII-encoded binary 111 112 tft.println(); // New line 113 tft.setTextColor(TFT_MAGENTA, TFT_BLACK); 114 float fvalue = 1.23456; 115 tft.println(fvalue, 0); // no decimal places 116 tft.println(fvalue, 1); // 1 decimal place 117 tft.println(fvalue, 2); // 2 decimal places 118 tft.println(fvalue, 5); // 5 decimal places 119 120 delay(5000); 121 122 // Get ready for the next demo while we have this font loaded 123 tft.fillScreen(TFT_BLACK); 124 tft.setCursor(0, 0); // Set cursor at top left of screen 125 tft.setTextColor(TFT_WHITE, TFT_BLACK); 126 tft.println("Wrong and right ways to"); 127 tft.println("print changing values..."); 128 129 tft.unloadFont(); // Remove the font to recover memory used 130 131 132 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 133 // Large font 134 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 135 136 tft.loadFont(AA_FONT_LARGE); // Load another different font 137 138 //tft.fillScreen(TFT_BLACK); 139 140 // Draw changing numbers - does not work unless a filled rectangle is drawn over the old text 141 for (int i = 0; i <= 20; i++) 142 { 143 tft.setCursor(50, 50); 144 tft.setTextColor(TFT_GREEN, TFT_BLACK); // TFT_BLACK is used for anti-aliasing only 145 // By default background fill is off 146 tft.print(" "); // Overprinting old number with spaces DOES NOT WORK! 147 tft.setCursor(50, 50); 148 tft.print(i / 10.0, 1); 149 150 // Adding a parameter "true" to the setTextColor() function fills character background 151 // This extra parameter is only for smooth fonts! 152 tft.setTextColor(TFT_GREEN, TFT_BLACK, true); 153 tft.setCursor(50, 90); 154 tft.print(i / 10.0, 1); 155 156 delay (200); 157 } 158 159 delay(5000); 160 161 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 162 // Large font text wrapping 163 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 164 165 tft.fillScreen(TFT_BLACK); 166 167 tft.setTextColor(TFT_YELLOW, TFT_BLACK); // Change the font colour and the background colour 168 169 tft.setCursor(0, 0); // Set cursor at top left of screen 170 171 tft.println("Large font!"); 172 173 tft.setTextWrap(true); // Wrap on width 174 tft.setTextColor(TFT_CYAN, TFT_BLACK); 175 tft.println("Long lines wrap to the next line"); 176 177 tft.setTextWrap(false, false); // Wrap on width and height switched off 178 tft.setTextColor(TFT_MAGENTA, TFT_BLACK); 179 tft.println("Unless text wrap is switched off"); 180 181 tft.unloadFont(); // Remove the font to recover memory used 182 183 delay(8000); 184 }