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 (4187B)

      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 has 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 xolor 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_reading_TFT" 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 LittleFS (must set at least 1M for LittleFS) using the
     25 //  "Tools"  "ESP8266 LittleFS Data Upload" menu option in the IDE.
     26 //  To add this option follow instructions here for the ESP8266:
     27 //  https://github.com/earlephilhower/arduino-esp8266littlefs-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 Flash FS
     41 #include <FS.h>
     42 #include <LittleFS.h>
     43 #define FlashFS LittleFS
     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 (!LittleFS.begin()) {
     82     Serial.println("Flash FS initialisation failed!");
     83     while (1) yield(); // Stay here twiddling thumbs waiting
     84   }
     85   Serial.println("\n\Flash FS available!");
     86 
     87   bool font_missing = false;
     88   if (LittleFS.exists("/NotoSansBold15.vlw") == false) font_missing = true;
     89   if (LittleFS.exists("/NotoSansBold36.vlw")    == false) font_missing = true;
     90 
     91   if (font_missing)
     92   {
     93     Serial.println("\nFont missing in Flash FS, did you upload it?");
     94     while (1) yield();
     95   }
     96   else Serial.println("\nFonts found OK.");
     97 }
     98 
     99 
    100 void loop() {
    101 
    102   // Select a font size commensurate with screen size
    103   if (tft.width()>= 320)
    104     tft.loadFont(AA_FONT_LARGE, LittleFS);
    105   else
    106     tft.loadFont(AA_FONT_SMALL, LittleFS);
    107 
    108   fillGradient(); // Put here after selecting the font so fontHeight() is already set
    109 
    110   tft.setTextColor(TFT_WHITE); // Background color is ignored in gradient area
    111   tft.setCursor(0, 10); // Set cursor at top left of screen
    112 
    113   uint32_t t = millis();
    114   tft.println(" Ode to a small\n lump of green\n putty I found\n in my armpit\n one midsummer\n morning ");
    115   Serial.println(t = millis()-t);
    116 
    117   tft.unloadFont(); // Remove the font to recover memory used
    118 
    119   delay(2000);
    120 }