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

Print_Smooth_Font.ino (6070B)

      1 /*
      2   Sketch to demonstrate using the print class with smooth fonts
      3 
      4   Sketch is written for a 240 x 320 display
      5 
      6   Load the font file into SPIFFS first by using the Arduino IDE
      7   Sketch Data Upload menu option. Font files must be stored in the
      8   sketch data folder (Ctrl+k to view).
      9   https://github.com/esp8266/arduino-esp8266fs-plugin
     10   https://github.com/me-no-dev/arduino-esp32fs-plugin
     11 
     12   New font files in the .vlw format can be created using the Processing
     13   sketch in the library Tools folder. The Processing sketch can convert
     14   TrueType fonts in *.ttf or *.otf files.
     15 
     16   Note: SPIFFS does not accept an underscore _ in filenames!
     17 
     18   The library supports 16 bit Unicode characters:
     19   https://en.wikipedia.org/wiki/Unicode_font
     20 
     21   The characters supported are in the in the Basic Multilingual Plane:
     22   https://en.wikipedia.org/wiki/Plane_(Unicode)#Basic_Multilingual_Plane
     23 
     24   Make sure all the display driver and pin connections are correct by
     25   editing the User_Setup.h file in the TFT_eSPI library folder.
     26 
     27   #########################################################################
     28   ###### DON'T FORGET TO UPDATE THE User_Setup.h FILE IN THE LIBRARY ######
     29   #########################################################################
     30 */
     31 
     32 // Font file is stored in SPIFFS
     33 #define FS_NO_GLOBALS
     34 #include <FS.h>
     35 
     36 // Graphics and font library
     37 #include <TFT_eSPI.h>
     38 #include <SPI.h>
     39 
     40 TFT_eSPI tft = TFT_eSPI();  // Invoke library
     41 
     42 // -------------------------------------------------------------------------
     43 // Setup
     44 // -------------------------------------------------------------------------
     45 void setup(void) {
     46   Serial.begin(115200); // Used for messages
     47 
     48   tft.init();
     49   tft.setRotation(1);
     50 
     51   if (!SPIFFS.begin()) {
     52     Serial.println("SPIFFS initialisation failed!");
     53     while (1) yield(); // Stay here twiddling thumbs waiting
     54   }
     55   Serial.println("\r\nInitialisation done.");
     56 
     57   listFiles(); // Lists the files so you can see what is in the SPIFFS
     58 
     59 }
     60 
     61 // -------------------------------------------------------------------------
     62 // Main loop
     63 // -------------------------------------------------------------------------
     64 void loop() {
     65   // Wrap test at right and bottom of screen
     66   tft.setTextWrap(true, true);
     67 
     68   // Name of font file (library adds leading / and .vlw)
     69   String fileName = "Final-Frontier-28";
     70 
     71   // Font and background colour, background colour is used for anti-alias blending
     72   tft.setTextColor(TFT_WHITE, TFT_BLACK);
     73 
     74   // Load the font
     75   tft.loadFont(fileName);
     76 
     77   // Display all characters of the font
     78   tft.showFont(2000);
     79 
     80   // Set "cursor" at top left corner of display (0,0)
     81   // (cursor will move to next line automatically during printing with 'tft.println'
     82   //  or stay on the line is there is room for the text with tft.print)
     83   tft.setCursor(0, 0);
     84 
     85   // Set the font colour to be white with a black background, set text size multiplier to 1
     86   tft.setTextColor(TFT_WHITE, TFT_BLACK);
     87 
     88   // We can now plot text on screen using the "print" class
     89   tft.println("Hello World!");
     90 
     91   // Set the font colour to be yellow
     92   tft.setTextColor(TFT_YELLOW, TFT_BLACK);
     93   tft.println(1234.56);
     94 
     95   // Set the font colour to be red
     96   tft.setTextColor(TFT_RED, TFT_BLACK);
     97   tft.println((uint32_t)3735928559, HEX); // Should print DEADBEEF
     98 
     99   // Set the font colour to be green with black background
    100   tft.setTextColor(TFT_GREEN, TFT_BLACK);
    101   tft.println("Anti-aliased font!");
    102   tft.println("");
    103 
    104   // Test some print formatting functions
    105   float fnumber = 123.45;
    106 
    107   // Set the font colour to be blue
    108   tft.setTextColor(TFT_BLUE, TFT_BLACK);
    109   tft.print("Float = ");       tft.println(fnumber);           // Print floating point number
    110   tft.print("Binary = ");      tft.println((int)fnumber, BIN); // Print as integer value in binary
    111   tft.print("Hexadecimal = "); tft.println((int)fnumber, HEX); // Print as integer number in Hexadecimal
    112 
    113   // Unload the font to recover used RAM
    114   tft.unloadFont();
    115 
    116   delay(10000);
    117 }
    118 
    119 
    120 // -------------------------------------------------------------------------
    121 // List files in ESP8266 or ESP32 SPIFFS memory
    122 // -------------------------------------------------------------------------
    123 void listFiles(void) {
    124   Serial.println();
    125   Serial.println("SPIFFS files found:");
    126 
    127 #ifdef ESP32
    128   listDir(SPIFFS, "/", true);
    129 #else
    130   fs::Dir dir = SPIFFS.openDir("/"); // Root directory
    131   String  line = "=====================================";
    132 
    133   Serial.println(line);
    134   Serial.println("  File name               Size");
    135   Serial.println(line);
    136 
    137   while (dir.next()) {
    138     String fileName = dir.fileName();
    139     Serial.print(fileName);
    140     int spaces = 25 - fileName.length(); // Tabulate nicely
    141     if (spaces < 0) spaces = 1;
    142     while (spaces--) Serial.print(" ");
    143     fs::File f = dir.openFile("r");
    144     Serial.print(f.size()); Serial.println(" bytes");
    145     yield();
    146   }
    147 
    148   Serial.println(line);
    149 #endif
    150   Serial.println();
    151   delay(1000);
    152 }
    153 
    154 #ifdef ESP32
    155 void listDir(fs::FS &fs, const char * dirname, uint8_t levels) {
    156   Serial.printf("Listing directory: %s\n", dirname);
    157 
    158   fs::File root = fs.open(dirname);
    159   if (!root) {
    160     Serial.println("Failed to open directory");
    161     return;
    162   }
    163   if (!root.isDirectory()) {
    164     Serial.println("Not a directory");
    165     return;
    166   }
    167 
    168   fs::File file = root.openNextFile();
    169   while (file) {
    170 
    171     if (file.isDirectory()) {
    172       Serial.print("DIR : ");
    173       String fileName = file.name();
    174       Serial.print(fileName);
    175       if (levels) {
    176         listDir(fs, file.name(), levels - 1);
    177       }
    178     } else {
    179       String fileName = file.name();
    180       Serial.print("  " + fileName);
    181       int spaces = 32 - fileName.length(); // Tabulate nicely
    182       if (spaces < 1) spaces = 1;
    183       while (spaces--) Serial.print(" ");
    184       String fileSize = (String) file.size();
    185       spaces = 8 - fileSize.length(); // Tabulate nicely
    186       if (spaces < 1) spaces = 1;
    187       while (spaces--) Serial.print(" ");
    188       Serial.println(fileSize + " bytes");
    189     }
    190 
    191     file = root.openNextFile();
    192   }
    193 }
    194 #endif
    195 // -------------------------------------------------------------------------