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_4.ino (5357B)

      1 /*
      2   There are four different methods of plotting anti-aliased fonts to the screen.
      3 
      4   This sketch uses method 4, printing "String" or character array types only to screen,
      5   via a Sprite. The Sprite must NOT have been created already. The printToSprite()
      6   function automatically creates a sprite of a minimal size to contain the String,
      7   then plots to screen at the "tft" cursor position. Printing via a sprite draws the
      8   text faster on the screen. This method minimises flicker but uses RAM for the Sprite,
      9   the Sprite is automatically deleted after plotting to the TFT.
     10 
     11   Number and float types must be converted to strings to use printToSprite() e.g.:
     12   spr.printToSprite( (String) number );
     13   spr.printToSprite( (String) (number * 55 / 1.23) );  // Put calculations within brackets
     14 
     15   The key advantage of this method is that you do not need to calculate the size of sprite
     16   needed to contain the text, the library does that for you. The library also fills the
     17   the sprite with text background colour for you.
     18 
     19   printToSprite() has a second purpose, if the sprite has been created already the String
     20   will be printed into the Sprite at the "sprite" cursor position, which is
     21   different to the "tft" cursor position. In this case the Sprite is not deleted and
     22   you must use pushSprite() to plot on the screen. This method is not used in this sketch.
     23   because in general it is better to use drawString() in an already created sprite.
     24   printToSprite() will NOT move the tft cursor.
     25 
     26 */
     27 //  The fonts used are in the sketch data folder, press Ctrl+K to view.
     28 
     29 //  Upload the fonts and icons to SPIFFS (must set at least 1M for SPIFFS) using the
     30 //  "Tools"  "ESP8266 (or ESP32) Sketch Data Upload" menu option in the IDE.
     31 //  To add this option follow instructions here for the ESP8266:
     32 //  https://github.com/esp8266/arduino-esp8266fs-plugin
     33 //  or for the ESP32:
     34 //  https://github.com/me-no-dev/arduino-esp32fs-plugin
     35 
     36 //  Close the IDE and open again to see the new menu option.
     37 
     38 // A processing sketch to create new fonts can be found in the Tools folder of TFT_eSPI
     39 // https://github.com/Bodmer/TFT_eSPI/tree/master/Tools/Create_Smooth_Font/Create_font
     40 
     41 // This sketch uses font files created from the Noto family of fonts:
     42 // https://www.google.com/get/noto/
     43 
     44 #define AA_FONT_SMALL "NotoSansBold15"
     45 #define AA_FONT_LARGE "NotoSansBold36"
     46 
     47 // Font files are stored in SPIFFS, so load the library
     48 #include <FS.h>
     49 
     50 #include <SPI.h>
     51 #include <TFT_eSPI.h>       // Hardware-specific library
     52 
     53 TFT_eSPI    tft = TFT_eSPI();
     54 TFT_eSprite spr = TFT_eSprite(&tft); // Sprite class needs to be invoked
     55 
     56 void setup(void) {
     57 
     58   Serial.begin(250000);
     59 
     60   tft.begin();
     61 
     62   tft.setRotation(1);
     63 
     64   spr.setColorDepth(16); // 16 bit colour needed to show anti-aliased fonts
     65 
     66   if (!SPIFFS.begin()) {
     67     Serial.println("SPIFFS initialisation failed!");
     68     while (1) yield(); // Stay here twiddling thumbs waiting
     69   }
     70   Serial.println("\r\nSPIFFS available!");
     71 
     72   // ESP32 will crash if any of the fonts are missing
     73   bool font_missing = false;
     74   if (SPIFFS.exists("/NotoSansBold15.vlw")    == false) font_missing = true;
     75   if (SPIFFS.exists("/NotoSansBold36.vlw")    == false) font_missing = true;
     76 
     77   if (font_missing)
     78   {
     79     Serial.println("\r\nFont missing in SPIFFS, did you upload it?");
     80     while(1) yield();
     81   }
     82   else Serial.println("\r\nFonts found OK.");
     83 }
     84 
     85 void loop() {
     86 
     87   tft.fillScreen(TFT_BLACK);
     88 
     89   tft.setTextColor(TFT_WHITE, TFT_BLACK); // Set the font colour and the background colour
     90 
     91   tft.setTextDatum(TC_DATUM); // Top Centre datum
     92 
     93   int xpos = tft.width() / 2; // Half the screen width
     94   int ypos = 50;
     95 
     96 
     97   // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
     98   // Small font
     99   // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    100 
    101   spr.loadFont(AA_FONT_SMALL); // Must load the font first into the sprite class
    102   
    103   spr.setTextColor(TFT_YELLOW, TFT_BLACK); // Set the sprite font colour and the background colour
    104 
    105   tft.setCursor(xpos - 50, ypos);          // Set the tft cursor position, yes tft position!
    106   spr.printToSprite("Small 15pt font");    // Prints to tft cursor position, tft cursor NOT moved
    107 
    108   ypos += spr.fontHeight();   // Get the font height and move ypos down
    109 
    110   spr.unloadFont(); // Remove the font from sprite class to recover memory used
    111 
    112   delay(4000);
    113 
    114   // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    115   // Large font
    116   // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    117 
    118   tft.fillScreen(TFT_BLACK);
    119 
    120   spr.loadFont(AA_FONT_LARGE); // Load another different font
    121 
    122   spr.setTextColor(TFT_WHITE, TFT_BLUE); // Set the font colour and the background colour
    123 
    124   tft.setCursor(xpos - 90, ypos);  // Set the tft cursor position
    125   spr.printToSprite("36pt font");  // Text is rendered via a minimally sized sprite
    126 
    127   ypos += spr.fontHeight();  // Get the font height and move ypos down
    128 
    129   // Draw changing numbers - no flicker using this plot method!
    130   for (int i = 0; i <= 200; i++) {
    131     tft.setCursor(10, 10);
    132     // Number is converted to String type by (String) (number)
    133     spr.printToSprite(" " + (String) (i / 100.0) + " "); // Space padding helps over-write old numbers
    134     delay (20);
    135   }
    136 
    137   spr.unloadFont(); // Remove the font to recover memory used
    138 
    139   delay(8000);
    140 }