acidportal

- 😈 Worlds smallest Evil Portal on a LilyGo T-QT
git clone git://git.acid.vegas/acidportal.git
Log | Files | Refs | Archive | README | LICENSE

TFT_String_Align.ino (3203B)

      1 /*
      2 Tests string alignment
      3 
      4 Normally strings are printed relative to the top left corner but this can be
      5 changed with the setTextDatum() function. The library has #defines for:
      6 
      7 TL_DATUM = 0 = Top left
      8 TC_DATUM = 1 = Top centre
      9 TR_DATUM = 2 = Top right
     10 ML_DATUM = 3 = Middle left
     11 MC_DATUM = 4 = Middle centre
     12 MR_DATUM = 5 = Middle right
     13 BL_DATUM = 6 = Bottom left
     14 BC_DATUM = 7 = Bottom centre
     15 BR_DATUM = 8 = Bottom right
     16 
     17 L_BASELINE =  9 = Left character baseline (Line the 'A' character would sit on)
     18 C_BASELINE = 10 = Centre character baseline
     19 R_BASELINE = 11 = Right character baseline
     20 
     21 So you can use lines to position text like:
     22 
     23   tft.setTextDatum(BC_DATUM); // Set datum to bottom centre
     24  
     25  Needs fonts 2, 4, 6, 7 and 8
     26 
     27   #########################################################################
     28   ###### DON'T FORGET TO UPDATE THE User_Setup.h FILE IN THE LIBRARY ######
     29   ######        TO SELECT THE FONTS AND PINS YOU USE, SEE ABOVE      ######
     30   #########################################################################
     31  */
     32 
     33 
     34 #include <SPI.h>
     35 
     36 #include <TFT_eSPI.h> // Hardware-specific library
     37 
     38 TFT_eSPI tft = TFT_eSPI();       // Invoke custom library
     39 
     40 unsigned long drawTime = 0;
     41 
     42 int x, y;  // Coordinates for drawing
     43 
     44 void setup(void) {
     45   Serial.begin(115200);
     46   tft.init();
     47   tft.setRotation(1);
     48 }
     49 
     50 void loop() {
     51 
     52   tft.fillScreen(TFT_BLACK);
     53 
     54   x = 160, y = 120;
     55   
     56   //Test all 9 datums 0-8 when plotting numbers
     57   for(byte datum = 0; datum < 9; datum++) {
     58     tft.setTextColor(TFT_WHITE, TFT_BLACK);
     59     
     60     tft.setTextDatum(datum);
     61     
     62     tft.drawNumber(42, x, y, 8);
     63     
     64     drawCross(x, y, TFT_RED);
     65     
     66     delay(1000);
     67     tft.fillScreen(TFT_BLACK);
     68   }
     69 
     70   //Test all 9 datums 0-8 when plotting a string
     71   // Datum works when text size is > 1
     72   for(byte datum = 0; datum < 9; datum++) {
     73     tft.setTextColor(TFT_WHITE, TFT_BLACK);
     74     
     75     tft.setTextDatum(datum);
     76 
     77     tft.setTextSize(2);
     78     
     79     tft.drawString("[xox]", x, y, 4);
     80     
     81     drawCross(x, y, TFT_GREEN);
     82     
     83     delay(1000);
     84     tft.fillScreen(TFT_BLACK);
     85   }
     86 
     87   tft.setTextSize(1);
     88 
     89   tft.setTextColor(TFT_BLUE, TFT_BLACK);
     90   // Here a special function is used that automatically centres the text
     91   // on the x, y coordinate
     92   tft.drawCentreString("69", x, y, 8);
     93   
     94   drawCross(x, y, TFT_CYAN);
     95 
     96   delay(1000);
     97   
     98   tft.fillScreen(TFT_BLACK);
     99   
    100   tft.setTextColor(TFT_RED, TFT_BLACK);
    101   tft.drawRightString("88",160,120,8);
    102   
    103   drawCross(x, y, TFT_RED);
    104   
    105   delay(1000);
    106   
    107   tft.fillScreen(TFT_BLACK);
    108 
    109   tft.setTextColor(TFT_WHITE, TFT_BLUE);
    110 
    111   //Test floating point drawing function
    112 
    113   tft.setTextDatum(BC_DATUM); // Set datum to bottom centre
    114 
    115   float test = 67.125;
    116   y = 180;
    117   tft.drawFloat(test, 4, x, y, 4);
    118   delay(1000);
    119   tft.fillScreen(TFT_BLACK);
    120   test = -0.555555;
    121   tft.drawFloat(test, 3, x, y, 4);
    122   delay(1000);
    123   tft.fillScreen(TFT_BLACK);
    124   test = 0.1;
    125   tft.drawFloat(test, 4, x, y, 4);
    126   delay(1000);
    127   tft.fillScreen(TFT_BLACK);
    128   test = 9999999;
    129   tft.drawFloat(test, 1, x, y, 4);
    130 
    131   drawCross(x, y, TFT_RED);
    132   
    133   delay(4000);
    134 }
    135 
    136 void drawCross(int x, int y, unsigned int color)
    137 {
    138   tft.drawLine(x - 5, y, x + 5, y, color);
    139   tft.drawLine(x, y - 5, x, y + 5, color);
    140 }
    141 
    142 
    143 
    144 
    145