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 |
Smooth_font_gradient.ino (4255B)
1 /* 2 This sketch is based on Font Demo 1. It introduces a method for rendering 3 anti-aliased fonts on a graded background. This is achieved by telling the 4 TFT_eSPI library the pixel color at each point on the screen. In this sketch 5 a graded background is drawn, the color of each pixel can therefore be 6 determined. The TFT does not need to support reading of the graphics memory. 7 The sketch could be adapted so only part of the screen is gas a color gradient. 8 9 The TFT_eSPI library must be given the name of the function in the sketch 10 that will return the pixel color at a position x,y on the TFT. In this 11 sketch that function is called gradientColor, so this line is included: 12 13 tft.setCallback(gradientColor); 14 15 TFT_eSPI will call this function during the rendering of the anti-aliased 16 font to blend the edges of each character with the returned color. 17 18 If the TFT supports reading the screen RAM then the returned value can be 19 tft.readPixel(x,y) and anti-aliased text can the be drawn over any screen 20 image. See "Smooth_font_over_image" example. 21 */ 22 // The fonts used are in the sketch data folder, press Ctrl+K to view. 23 24 // Upload the fonts and icons to SPIFFS (must set at least 1M for SPIFFS) using the 25 // "Tools" "ESP8266 (or ESP32) Sketch Data Upload" menu option in the IDE. 26 // To add this option follow instructions here for the ESP8266: 27 // https://github.com/esp8266/arduino-esp8266fs-plugin 28 // or for the ESP32: 29 // https://github.com/me-no-dev/arduino-esp32fs-plugin 30 31 // Close the IDE and open again to see the new menu option. 32 33 // A processing sketch to create new fonts can be found in the Tools folder of TFT_eSPI 34 // https://github.com/Bodmer/TFT_eSPI/tree/master/Tools/Create_Smooth_Font/Create_font 35 36 // This sketch uses font files created from the Noto family of fonts: 37 // https://www.google.com/get/noto/ 38 39 #define AA_FONT_SMALL "NotoSansBold15" 40 #define AA_FONT_LARGE "NotoSansBold36" 41 42 // Font files are stored in SPIFFS, so load the library 43 #include <FS.h> 44 45 #include <SPI.h> 46 #include <TFT_eSPI.h> // Hardware-specific library 47 48 TFT_eSPI tft = TFT_eSPI(); 49 50 #define TOP_COLOR TFT_RED 51 #define BOTTOM_COLOR TFT_BLACK 52 53 #define GRADIENT_HEIGHT (9 + tft.fontHeight() * 5) // Gradient over 5 lines 54 #define OUTSIDE_GRADIENT TFT_BLUE 55 56 uint16_t gradientColor(uint16_t x, uint16_t y) 57 { 58 if (y > GRADIENT_HEIGHT) return OUTSIDE_GRADIENT; // Outside gradient area 59 uint8_t alpha = (255 * y) / GRADIENT_HEIGHT; // alpha is a value in the range 0-255 60 return tft.alphaBlend(alpha, BOTTOM_COLOR, TOP_COLOR); 61 } 62 63 void fillGradient() { 64 uint16_t w = tft.width(); 65 for (uint16_t y = 0; y < tft.height(); ++y) { 66 uint16_t color = gradientColor(0, y); // x not used here 67 tft.drawFastHLine(0, y, w, color); 68 } 69 } 70 71 void setup(void) { 72 Serial.begin(115200); 73 74 tft.begin(); 75 76 tft.setCallback(gradientColor); // Switch on color callback for anti-aliased fonts 77 //tft.setCallback(nullptr); // Switch off callback (off by default) 78 79 tft.setRotation(1); 80 81 if (!SPIFFS.begin()) { 82 Serial.println("SPIFFS initialisation failed!"); 83 while (1) yield(); // Stay here twiddling thumbs waiting 84 } 85 Serial.println("\r\nSPIFFS available!"); 86 87 // ESP32 will crash if any of the fonts are missing 88 bool font_missing = false; 89 if (SPIFFS.exists("/NotoSansBold15.vlw") == false) font_missing = true; 90 if (SPIFFS.exists("/NotoSansBold36.vlw") == false) font_missing = true; 91 92 if (font_missing) 93 { 94 Serial.println("\r\nFont missing in SPIFFS, did you upload it?"); 95 while (1) yield(); 96 } 97 else Serial.println("\r\nFonts found OK."); 98 } 99 100 101 void loop() { 102 103 // Select a font size commensurate with screen size 104 if (tft.width()>= 320) 105 tft.loadFont(AA_FONT_LARGE); 106 else 107 tft.loadFont(AA_FONT_SMALL); 108 109 fillGradient(); // Put here after selecting the font so fontHeight() is already set 110 111 tft.setTextColor(TFT_WHITE); // Background color is ignored in gradient area 112 tft.setCursor(0, 10); // Set cursor at top left of screen 113 114 uint32_t t = millis(); 115 tft.println(" Ode to a small\n lump of green\n putty I found\n in my armpit\n one midsummer\n morning "); 116 Serial.println(t = millis()-t); 117 118 tft.unloadFont(); // Remove the font to recover memory used 119 120 delay(2000); 121 }