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_2_Array.ino (6399B)

      1 /*
      2   This sketch is the same as the Font_Demo_2 example, except the fonts in this
      3   example are in a FLASH (program memory) array. This means that processors
      4   such as the STM32 series that are not supported by a SPIFFS library can use
      5   smooth (anti-aliased) fonts.
      6 */
      7 
      8 /*
      9   There are four different methods of plotting anti-aliased fonts to the screen.
     10 
     11   This sketch uses method 2, using graphics calls plotting direct to the TFT:
     12     tft.drawString(string, x, y);
     13     tft.drawNumber(integer, x, y);
     14     tft.drawFloat(float, dp, x, y); // dp = number of decimal places
     15 
     16   setTextDatum() and setTextPadding() functions work with those draw functions.
     17 
     18   This method is good for static text that does not change often because changing
     19   values may flicker.
     20   
     21 */
     22 
     23 // A processing sketch to create new fonts can be found in the Tools folder of TFT_eSPI
     24 // https://github.com/Bodmer/TFT_eSPI/tree/master/Tools/Create_Smooth_Font/Create_font
     25 
     26 // This sketch uses font files created from the Noto family of fonts:
     27 // https://www.google.com/get/noto/
     28 
     29 #include "NotoSansBold15.h"
     30 #include "NotoSansBold36.h"
     31 
     32 // The font names are arrays references, thus must NOT be in quotes ""
     33 #define AA_FONT_SMALL NotoSansBold15
     34 #define AA_FONT_LARGE NotoSansBold36
     35 
     36 #include <SPI.h>
     37 #include <TFT_eSPI.h>       // Hardware-specific library
     38 
     39 TFT_eSPI tft = TFT_eSPI();
     40 
     41 void setup(void) {
     42 
     43   Serial.begin(250000);
     44 
     45   tft.begin();
     46 
     47   tft.setRotation(1);
     48 }
     49 
     50 void loop() {
     51 
     52   tft.fillScreen(TFT_BLACK);
     53 
     54   tft.setTextColor(TFT_WHITE, TFT_BLACK); // Set the font colour and the background colour
     55 
     56   tft.setTextDatum(TC_DATUM); // Top Centre datum
     57 
     58   int xpos = tft.width() / 2; // Half the screen width
     59   int ypos = 10;
     60 
     61 
     62   // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
     63   // Small font
     64   // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
     65 
     66   tft.loadFont(AA_FONT_SMALL); // Must load the font first
     67 
     68   tft.drawString("Small 15pt font", xpos, ypos);
     69 
     70   ypos += tft.fontHeight();   // Get the font height and move ypos down
     71 
     72   tft.setTextColor(TFT_GREEN, TFT_BLACK);
     73 
     74   // If the string does not fit the screen width, then the next character will wrap to a new line
     75   tft.drawString("Ode To A Small Lump Of Green Putty I Found In My Armpit One Midsummer Morning", xpos, ypos);
     76 
     77   tft.setTextColor(TFT_GREEN, TFT_BLUE); // Background colour does not match the screen background!
     78   tft.drawString("Anti-aliasing causes odd looking shadow effects if the text and screen background colours are not the same!", xpos, ypos + 60);
     79 
     80   tft.unloadFont(); // Remove the font to recover memory used
     81 
     82   delay(5000);
     83 
     84   // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
     85   // Large font
     86   // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
     87 
     88   tft.loadFont(AA_FONT_LARGE); // Load another different font
     89 
     90   tft.fillScreen(TFT_BLACK);
     91 
     92   // The "true" parameter forces background drawing for smooth fonts
     93   tft.setTextColor(TFT_GREEN, TFT_BLUE, true); // Change the font colour and the background colour
     94 
     95   tft.drawString("36pt font", xpos, ypos);
     96 
     97   ypos += tft.fontHeight();  // Get the font height and move ypos down
     98 
     99   // Set text padding to 100 pixels wide area to over-write old values on screen
    100   tft.setTextPadding(100);
    101 
    102   // Draw changing numbers - likely to flicker using this plot method!
    103   for (int i = 0; i <= 99; i++) {
    104     tft.drawFloat(i / 10.0, 1, xpos, ypos);
    105     delay (200);
    106   }
    107 
    108   // Turn off text padding by setting value to 0
    109   tft.setTextPadding(0);
    110 
    111   tft.unloadFont(); // Remove the font to recover memory used
    112 
    113   delay(5000);
    114 
    115   // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    116   // Setting the 12 datum positions works with free fonts
    117   // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    118 
    119   // Integer numbers, floats and strings can be drawn relative to a x,y datum, e.g.:
    120   // tft.drawNumber( 123, x, y);
    121   // tft.drawFloat( 1.23, dp, x, y); // Where dp is number of decimal places to show
    122   // tft.drawString( "Abc", x, y);
    123 
    124   tft.fillScreen(TFT_BLACK);
    125 
    126   tft.setTextColor(TFT_DARKGREY, TFT_BLACK, false);
    127 
    128   // Use middle of screen as datum
    129   xpos = tft.width() /2;
    130   ypos = tft.height()/2;
    131 
    132   tft.loadFont(AA_FONT_SMALL);
    133   tft.setTextDatum(TL_DATUM);
    134   tft.drawString("[Top left]", xpos, ypos);
    135   drawDatumMarker(xpos, ypos);
    136   delay(1000);
    137 
    138   tft.fillScreen(TFT_BLACK);
    139   tft.setTextDatum(TC_DATUM);
    140   tft.drawString("[Top centre]", xpos, ypos);
    141   drawDatumMarker(xpos, ypos);
    142   delay(1000);
    143 
    144   tft.fillScreen(TFT_BLACK);
    145   tft.setTextDatum(TR_DATUM);
    146   tft.drawString("[Top right]", xpos, ypos);
    147   drawDatumMarker(xpos, ypos);
    148   delay(1000);
    149 
    150   tft.fillScreen(TFT_BLACK);
    151   tft.setTextDatum(ML_DATUM);
    152   tft.drawString("[Middle left]", xpos, ypos);
    153   drawDatumMarker(xpos, ypos);
    154   delay(1000);
    155 
    156   tft.fillScreen(TFT_BLACK);
    157   tft.setTextDatum(MC_DATUM);
    158   tft.drawString("[Middle centre]", xpos, ypos);
    159   drawDatumMarker(xpos, ypos);
    160   delay(1000);
    161 
    162   tft.fillScreen(TFT_BLACK);
    163   tft.setTextDatum(MR_DATUM);
    164   tft.drawString("[Middle right]", xpos, ypos);
    165   drawDatumMarker(xpos, ypos);
    166   delay(1000);
    167 
    168   tft.fillScreen(TFT_BLACK);
    169   tft.setTextDatum(BL_DATUM);
    170   tft.drawString("[Bottom left]", xpos, ypos);
    171   drawDatumMarker(xpos, ypos);
    172   delay(1000);
    173 
    174   tft.fillScreen(TFT_BLACK);
    175   tft.setTextDatum(BC_DATUM);
    176   tft.drawString("[Bottom centre]", xpos, ypos);
    177   drawDatumMarker(xpos, ypos);
    178   delay(1000);
    179 
    180   tft.fillScreen(TFT_BLACK);
    181   tft.setTextDatum(BR_DATUM);
    182   tft.drawString("[Bottom right]", xpos, ypos);
    183   drawDatumMarker(xpos, ypos);
    184   delay(1000);
    185 
    186   tft.fillScreen(TFT_BLACK);
    187   tft.setTextDatum(L_BASELINE);
    188   tft.drawString("[Left baseline]", xpos, ypos);
    189   drawDatumMarker(xpos, ypos);
    190   delay(1000);
    191 
    192   tft.fillScreen(TFT_BLACK);
    193   tft.setTextDatum(C_BASELINE);
    194   tft.drawString("[Centre baseline]", xpos, ypos);
    195   drawDatumMarker(xpos, ypos);
    196   delay(1000);
    197 
    198   tft.fillScreen(TFT_BLACK);
    199   tft.setTextDatum(R_BASELINE);
    200   tft.drawString("[Right baseline]", xpos, ypos);
    201   drawDatumMarker(xpos, ypos);
    202   delay(1000);
    203 
    204   tft.unloadFont(); // Remove the font to recover memory used
    205 
    206   delay(4000);
    207 
    208 }
    209 
    210 // Draw a + mark centred on x,y
    211 void drawDatumMarker(int x, int y)
    212 {
    213   tft.drawLine(x - 5, y, x + 5, y, TFT_GREEN);
    214   tft.drawLine(x, y - 5, x, y + 5, TFT_GREEN);
    215 }