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_3.ino (7994B)
1 /* 2 There are four different methods of plotting anti-aliased fonts to the screen. 3 4 This sketch uses method 3, the font characters are first plotted in a Sprite, then the 5 Sprite is pushed to the screen. This method is very flexible and the Sprite can be 6 created, deleted, resized as needed. To render anti-aliased fonts well the Sprite 7 needs to be 16 bit. The fonts will render in 1 bit per pixel sprites but there 8 will then be no anti-aliasing. Using 1 bit per pixel Sprites is however useful 9 to use the extended Unicode range in fonts on mono displays like ePaper. 10 11 A single Sprite can be re-used for plotting different values and graphics to 12 different positions on the screen. This makes this method a very powerful display tool, 13 for example round buttons can be created, making use of transparent colour plotting. 14 15 */ 16 // The fonts used are in the sketch data folder, press Ctrl+K to view. 17 18 // Upload the fonts and icons to SPIFFS (must set at least 1M for SPIFFS) using the 19 // "Tools" "ESP8266 (or ESP32) Sketch Data Upload" menu option in the IDE. 20 // To add this option follow instructions here for the ESP8266: 21 // https://github.com/esp8266/arduino-esp8266fs-plugin 22 // or for the ESP32: 23 // https://github.com/me-no-dev/arduino-esp32fs-plugin 24 25 // Close the IDE and open again to see the new menu option. 26 27 // A processing sketch to create new fonts can be found in the Tools folder of TFT_eSPI 28 // https://github.com/Bodmer/TFT_eSPI/tree/master/Tools/Create_Smooth_Font/Create_font 29 30 // This sketch uses font files created from the Noto family of fonts: 31 // https://www.google.com/get/noto/ 32 33 #define AA_FONT_SMALL "NotoSansBold15" 34 #define AA_FONT_LARGE "NotoSansBold36" 35 #define AA_FONT_MONO "NotoSansMonoSCB20" // NotoSansMono-SemiCondensedBold 20pt 36 // Font files are stored in SPIFFS, so load the library 37 #include <FS.h> 38 39 #include <SPI.h> 40 #include <TFT_eSPI.h> // Hardware-specific library 41 42 TFT_eSPI tft = TFT_eSPI(); 43 TFT_eSprite spr = TFT_eSprite(&tft); // Sprite class needs to be invoked 44 45 void setup(void) { 46 47 Serial.begin(250000); 48 49 tft.begin(); 50 51 tft.setRotation(1); 52 53 spr.setColorDepth(16); // 16 bit colour needed to show anti aliased fonts 54 55 if (!SPIFFS.begin()) { 56 Serial.println("SPIFFS initialisation failed!"); 57 while (1) yield(); // Stay here twiddling thumbs waiting 58 } 59 Serial.println("\r\nSPIFFS available!"); 60 61 // ESP32 will crash if any of the fonts are missing 62 bool font_missing = false; 63 if (SPIFFS.exists("/NotoSansBold15.vlw") == false) font_missing = true; 64 if (SPIFFS.exists("/NotoSansBold36.vlw") == false) font_missing = true; 65 if (SPIFFS.exists("/NotoSansMonoSCB20.vlw") == false) font_missing = true; 66 67 if (font_missing) 68 { 69 Serial.println("\r\nFont missing in SPIFFS, did you upload it?"); 70 while(1) yield(); 71 } 72 else Serial.println("\r\nFonts found OK."); 73 } 74 75 void loop() { 76 77 tft.fillScreen(TFT_DARKGREY); 78 79 int xpos = tft.width() / 2; // Half the screen width 80 int ypos = 50; 81 82 83 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 84 // Small font 85 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 86 87 spr.loadFont(AA_FONT_SMALL); // Must load the font first into the sprite class 88 89 spr.createSprite(100, 50); // Create a sprite 100 pixels wide and 50 high 90 91 spr.fillSprite(TFT_BLUE); 92 93 spr.drawRect(0, 0, 100, 50, TFT_WHITE); // Draw sprite border outline (so we see extent) 94 95 spr.setTextColor(TFT_YELLOW, TFT_DARKGREY); // Set the sprite font colour and the background colour 96 97 spr.setTextDatum(MC_DATUM); // Middle Centre datum 98 99 spr.drawString("15pt font", 50, 25 ); // Coords of middle of 100 x 50 Sprite 100 101 spr.pushSprite(10, 10); // Push to TFT screen coord 10, 10 102 103 spr.pushSprite(10, 70, TFT_BLUE); // Push to TFT screen, TFT_BLUE is transparent 104 105 spr.unloadFont(); // Remove the font from sprite class to recover memory used 106 107 delay(4000); 108 109 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 110 // Large font 111 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 112 113 tft.fillScreen(TFT_BLACK); 114 115 // Beware: Sprites are a different "class" to TFT, so different fonts can be loaded 116 // in the tft and sprite instances, so load the font in the class instance you use! 117 // In this example this means the spr. instance. 118 119 spr.loadFont(AA_FONT_LARGE); // Load another different font into the sprite instance 120 121 // 100 x 50 sprite was created above and still exists... 122 123 spr.fillSprite(TFT_GREEN); 124 125 spr.setTextColor(TFT_BLACK, TFT_GREEN); // Set the font colour and the background colour 126 127 spr.setTextDatum(MC_DATUM); // Middle Centre datum 128 129 spr.drawString("Fits", 50, 25); // Make sure text fits in the Sprite! 130 spr.pushSprite(10, 10); // Push to TFT screen coord 10, 10 131 132 spr.fillSprite(TFT_RED); 133 spr.setTextColor(TFT_WHITE, TFT_RED); // Set the font colour and the background colour 134 135 spr.drawString("Too big", 50, 25); // Text is too big to all fit in the Sprite! 136 spr.pushSprite(10, 70); // Push to TFT screen coord 10, 70 137 138 // Draw changing numbers - no flicker using this plot method! 139 140 // >>>> Note: it is best to use drawNumber() and drawFloat() for numeric values <<<< 141 // >>>> this reduces digit position movement when the value changes <<<< 142 // >>>> drawNumber() and drawFloat() functions behave like drawString() and are <<<< 143 // >>>> supported by setTextDatum() and setTextPadding() <<<< 144 145 spr.setTextDatum(TC_DATUM); // Top Centre datum 146 147 spr.setTextColor(TFT_WHITE, TFT_BLUE); // Set the font colour and the background colour 148 149 for (int i = 0; i <= 200; i++) { 150 spr.fillSprite(TFT_BLUE); 151 spr.drawFloat(i / 100.0, 2, 50, 10); // draw with 2 decimal places at 50,10 in sprite 152 spr.pushSprite(10, 130); // Push to TFT screen coord 10, 130 153 delay (20); 154 } 155 156 spr.unloadFont(); // Remove the font to recover memory used 157 158 spr.deleteSprite(); // Recover memory 159 160 delay(1000); 161 162 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 163 // Mono spaced font 164 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 165 166 spr.loadFont(AA_FONT_MONO); // Mono spaced fonts have fixed inter-character gaps to 167 // aid formatting 168 int bnum = 1; 169 170 // Example of drawing buttons 171 for (int j = 0; j < 4; j++) 172 { 173 for (int k = 0; k < 4; k++) 174 { 175 int x = 120 + k * 45; 176 int y = 40 + j * 30; 177 button(x, y, bnum++); 178 } 179 } 180 181 for (int i = 0; i < 100; i++) 182 { 183 button(120, 160, i); 184 delay(50); 185 } 186 187 spr.unloadFont(); 188 189 delay(8000); 190 } 191 192 // ######################################################################### 193 // Draw a number in a rounded rectangle with some transparent pixels 194 // Load the font before calling 195 // ######################################################################### 196 void button(int x, int y, int num ) 197 { 198 199 // Size of sprite 200 #define IWIDTH 40 201 #define IHEIGHT 25 202 203 // Create a 16 bit sprite 40 pixels wide, 25 high (2000 bytes of RAM needed) 204 spr.setColorDepth(16); 205 spr.createSprite(IWIDTH, IHEIGHT); 206 207 // Fill it with black (this will be the transparent colour this time) 208 spr.fillSprite(TFT_BLACK); 209 210 // Draw a background for the numbers 211 spr.fillRoundRect( 0, 0, IWIDTH, IHEIGHT, 8, TFT_RED); 212 spr.drawRoundRect( 0, 0, IWIDTH, IHEIGHT, 8, TFT_WHITE); 213 214 // Set the font parameters 215 216 // Set text coordinate datum to middle centre 217 spr.setTextDatum(MC_DATUM); 218 219 // Set the font colour and the background colour 220 spr.setTextColor(TFT_WHITE, TFT_RED); 221 222 // Draw the number 223 spr.drawNumber(num, IWIDTH/2, 1 + IHEIGHT/2); 224 225 // Push sprite to TFT screen CGRAM at coordinate x,y (top left corner) 226 // All black pixels will not be drawn hence will show as "transparent" 227 spr.pushSprite(x, y, TFT_BLACK); 228 229 // Delete sprite to free up the RAM 230 spr.deleteSprite(); 231 }