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 (6223B)
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 LittleFS (must set at least 1M for LittleFS) using the 23 // "Tools" "ESP8266 LittleFS Data Upload" menu option in the IDE. 24 // To add this option follow instructions here for the ESP8266: 25 // https://github.com/earlephilhower/arduino-esp8266littlefs-plugin 26 27 // Close the IDE and open again to see the new menu option. 28 29 // A processing sketch to create new fonts can be found in the Tools folder of TFT_eSPI 30 // https://github.com/Bodmer/TFT_eSPI/tree/master/Tools/Create_Smooth_Font/Create_font 31 32 // This sketch uses font files created from the Noto family of fonts: 33 // https://www.google.com/get/noto/ 34 35 #define AA_FONT_SMALL "NotoSansBold15" 36 #define AA_FONT_LARGE "NotoSansBold36" 37 38 // Font files are stored in Flash FS 39 #include <FS.h> 40 #include <LittleFS.h> 41 #define FlashFS LittleFS 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 (!LittleFS.begin()) { 58 Serial.println("Flash FS initialisation failed!"); 59 while (1) yield(); // Stay here twiddling thumbs waiting 60 } 61 Serial.println("\n\Flash FS available!"); 62 63 bool font_missing = false; 64 if (LittleFS.exists("/NotoSansBold15.vlw") == false) font_missing = true; 65 if (LittleFS.exists("/NotoSansBold36.vlw") == false) font_missing = true; 66 67 if (font_missing) 68 { 69 Serial.println("\nFont missing in Flash FS, did you upload it?"); 70 while(1) yield(); 71 } 72 else Serial.println("\nFonts found OK."); 73 } 74 75 76 void loop() { 77 78 tft.fillScreen(TFT_BLACK); 79 80 tft.setTextColor(TFT_WHITE, TFT_BLACK); // Set the font colour AND the background colour 81 // so the anti-aliasing works 82 83 tft.setCursor(0, 0); // Set cursor at top left of screen 84 85 86 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 87 // Small font 88 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 89 90 tft.loadFont(AA_FONT_SMALL, LittleFS); // Must load the font first 91 92 tft.println("Small 15pt font"); // println moves cursor down for a new line 93 94 tft.println(); // New line 95 96 tft.print("ABC"); // print leaves cursor at end of line 97 98 tft.setTextColor(TFT_CYAN, TFT_BLACK); 99 tft.println("1234"); // Added to line after ABC 100 101 tft.setTextColor(TFT_YELLOW, TFT_BLACK); 102 // print stream formatting can be used,see: 103 // https://www.arduino.cc/en/Serial/Print 104 int ivalue = 1234; 105 tft.println(ivalue); // print as an ASCII-encoded decimal 106 tft.println(ivalue, DEC); // print as an ASCII-encoded decimal 107 tft.println(ivalue, HEX); // print as an ASCII-encoded hexadecimal 108 tft.println(ivalue, OCT); // print as an ASCII-encoded octal 109 tft.println(ivalue, BIN); // print as an ASCII-encoded binary 110 111 tft.println(); // New line 112 tft.setTextColor(TFT_MAGENTA, TFT_BLACK); 113 float fvalue = 1.23456; 114 tft.println(fvalue, 0); // no decimal places 115 tft.println(fvalue, 1); // 1 decimal place 116 tft.println(fvalue, 2); // 2 decimal places 117 tft.println(fvalue, 5); // 5 decimal places 118 119 delay(5000); 120 121 // Get ready for the next demo while we have this font loaded 122 tft.fillScreen(TFT_BLACK); 123 tft.setCursor(0, 0); // Set cursor at top left of screen 124 tft.setTextColor(TFT_WHITE, TFT_BLACK); 125 tft.println("Wrong and right ways to"); 126 tft.println("print changing values..."); 127 128 tft.unloadFont(); // Remove the font to recover memory used 129 130 131 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 132 // Large font 133 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 134 135 tft.loadFont(AA_FONT_LARGE, LittleFS); // Load another different font 136 137 //tft.fillScreen(TFT_BLACK); 138 139 // Draw changing numbers - does not work unless a filled rectangle is drawn over the old text 140 for (int i = 0; i <= 20; i++) 141 { 142 tft.setCursor(50, 50); 143 tft.setTextColor(TFT_GREEN, TFT_BLACK); // TFT_BLACK is used for anti-aliasing only 144 // By default background fill is off 145 tft.print(" "); // Overprinting old number with spaces DOES NOT WORK! 146 tft.setCursor(50, 50); 147 tft.print(i / 10.0, 1); 148 149 // Adding a parameter "true" to the setTextColor() function fills character background 150 // This extra parameter is only for smooth fonts! 151 tft.setTextColor(TFT_GREEN, TFT_BLACK, true); 152 tft.setCursor(50, 90); 153 tft.print(i / 10.0, 1); 154 155 delay (200); 156 } 157 158 delay(5000); 159 160 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 161 // Large font text wrapping 162 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 163 164 tft.fillScreen(TFT_BLACK); 165 166 tft.setTextColor(TFT_YELLOW, TFT_BLACK); // Change the font colour and the background colour 167 168 tft.setCursor(0, 0); // Set cursor at top left of screen 169 170 tft.println("Large font!"); 171 172 tft.setTextWrap(true); // Wrap on width 173 tft.setTextColor(TFT_CYAN, TFT_BLACK); 174 tft.println("Long lines wrap to the next line"); 175 176 tft.setTextWrap(false, false); // Wrap on width and height switched off 177 tft.setTextColor(TFT_MAGENTA, TFT_BLACK); 178 tft.println("Unless text wrap is switched off"); 179 180 tft.unloadFont(); // Remove the font to recover memory used 181 182 delay(8000); 183 }