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_reading_TFT.ino (5034B)

      1 /*
      2   This sketch is based on Font Demo 1. It introduces a method for rendering
      3   anti-aliased fonts on an arbitrary background. This is achieved by reading
      4   the pixel color at each point on the screen. The TFT must support reading
      5   the graphics RAM of the screen memory. This sketch has been tested with
      6   ILI9241 and ILI9481 serial and parallel screens. Other screens may or may
      7   not work!
      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 pixelColor, so this line is included:
     12 
     13     tft.setCallback(pixelColor);
     14 
     15   TFT_eSPI will call this function during the rendering of the anti-aliased
     16   font and use it to blend the edges of each character with the screen color.
     17 */
     18 //  The fonts used are in the sketch data folder, press Ctrl+K to view.
     19 
     20 //  Upload the fonts and icons to SPIFFS (must set at least 1M for SPIFFS) using the
     21 //  "Tools"  "ESP8266 (or ESP32) Sketch Data Upload" menu option in the IDE.
     22 //  To add this option follow instructions here for the ESP8266:
     23 //  https://github.com/esp8266/arduino-esp8266fs-plugin
     24 //  or for the ESP32:
     25 //  https://github.com/me-no-dev/arduino-esp32fs-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 SPIFFS, so load the library
     39 #include <FS.h>
     40 
     41 #include <SPI.h>
     42 #include <TFT_eSPI.h>       // Hardware-specific library
     43 
     44 TFT_eSPI tft = TFT_eSPI();
     45 
     46 // Callback function to provide the pixel color at x,y
     47 uint16_t pixelColor(uint16_t x, uint16_t y) { return tft.readPixel(x, y); }
     48 
     49 
     50 void setup(void) {
     51   Serial.begin(115200);
     52 
     53   tft.begin();
     54 
     55   tft.setCallback(pixelColor);  // The callback is only used during font rendering
     56   //tft.setCallback(nullptr);   // Switch off callback (off by default)
     57 
     58   tft.setRotation(1);
     59 
     60   if (!SPIFFS.begin()) {
     61     Serial.println("SPIFFS initialisation failed!");
     62     while (1) yield(); // Stay here twiddling thumbs waiting
     63   }
     64   Serial.println("\r\nSPIFFS available!");
     65 
     66   // ESP32 will crash if any of the fonts are missing, so check
     67   bool font_missing = false;
     68   if (SPIFFS.exists("/NotoSansBold15.vlw") == false) font_missing = true;
     69   if (SPIFFS.exists("/NotoSansBold36.vlw") == false) font_missing = true;
     70 
     71   if (font_missing)
     72   {
     73     Serial.println("\r\nFont missing in SPIFFS, did you upload it?");
     74     while (1) yield();
     75   }
     76   else Serial.println("\r\nFonts found OK.");
     77 }
     78 
     79 
     80 void loop() {
     81 
     82   rainbow_fill(); // Fill the screen with rainbow colours
     83 
     84   // Select a font size commensurate with screen size
     85   if (tft.width()>= 320)
     86     tft.loadFont(AA_FONT_LARGE);
     87   else
     88     tft.loadFont(AA_FONT_SMALL);
     89 
     90   tft.setTextColor(TFT_BLACK, TFT_WHITE); // Background color is ignored if callback is set
     91   tft.setCursor(0, 10); // Set cursor at top left of screen
     92 
     93   uint32_t t = millis();
     94   tft.println(" Ode to a small\n lump of green\n putty I found\n in my armpit\n one midsummer\n morning ");
     95   Serial.println(t = millis()-t);
     96 
     97   tft.unloadFont(); // Remove the font to recover memory used
     98 
     99   delay(2000);
    100 }
    101 
    102 // #########################################################################
    103 // Fill screen with a rainbow pattern
    104 // #########################################################################
    105 byte red = 31;
    106 byte green = 0;
    107 byte blue = 0;
    108 byte state = 0;
    109 unsigned int colour = red << 11; // Colour order is RGB 5+6+5 bits each
    110 
    111 void rainbow_fill()
    112 {
    113   // The colours and state are not initialised so the start colour changes each time the function is called
    114   
    115   for (int i = 319; i >= 0; i--) {
    116     // Draw a vertical line 1 pixel wide in the selected colour
    117     tft.drawFastHLine(0, i, tft.width(), colour); // in this example tft.width() returns the pixel width of the display
    118     // This is a "state machine" that ramps up/down the colour brightnesses in sequence
    119     switch (state) {
    120       case 0:
    121         green ++;
    122         if (green == 64) {
    123           green = 63;
    124           state = 1;
    125         }
    126         break;
    127       case 1:
    128         red--;
    129         if (red == 255) {
    130           red = 0;
    131           state = 2;
    132         }
    133         break;
    134       case 2:
    135         blue ++;
    136         if (blue == 32) {
    137           blue = 31;
    138           state = 3;
    139         }
    140         break;
    141       case 3:
    142         green --;
    143         if (green == 255) {
    144           green = 0;
    145           state = 4;
    146         }
    147         break;
    148       case 4:
    149         red ++;
    150         if (red == 32) {
    151           red = 31;
    152           state = 5;
    153         }
    154         break;
    155       case 5:
    156         blue --;
    157         if (blue == 255) {
    158           blue = 0;
    159           state = 0;
    160         }
    161         break;
    162     }
    163     colour = red << 11 | green << 5 | blue;
    164   }
    165 }