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_Rainbow480.ino (5761B)

      1 /*
      2  An example showing rainbow colours on a 3.0 or 3.2" TFT LCD screen
      3  and to show basic examples of font use.
      4 
      5  This sketch uses the GLCD, 2, 4, 6 fonts only.
      6 
      7  Make sure all the required fonts are loaded by editing the
      8  User_Setup.h file in the TFT_eSPI library folder.
      9 
     10 
     11   #########################################################################
     12   ###### DON'T FORGET TO UPDATE THE User_Setup.h FILE IN THE LIBRARY ######
     13   ######           TO SELECT THE FONTS AND PINS YOU USE              ######
     14   #########################################################################
     15  */
     16 
     17 #include <SPI.h>
     18 
     19 #include <TFT_eSPI.h> // Hardware-specific library
     20 
     21 TFT_eSPI tft = TFT_eSPI();       // Invoke custom library
     22 
     23 unsigned long targetTime = 0;
     24 byte red = 31;
     25 byte green = 0;
     26 byte blue = 0;
     27 byte state = 0;
     28 unsigned int colour = red << 11; // Colour order is RGB 5+6+5 bits each
     29 
     30 void setup(void) {
     31   tft.init();
     32   tft.setRotation(2);
     33   tft.fillScreen(TFT_BLACK);
     34 
     35   targetTime = millis() + 1000;
     36 }
     37 
     38 void loop() {
     39 
     40   if (targetTime < millis()) {
     41     targetTime = millis() + 10000;
     42 
     43     rainbow_fill(); // Fill the screen with rainbow colours
     44 
     45     // The standard AdaFruit font still works as before
     46     tft.setTextColor(TFT_BLACK); // Background is not defined so it is transparent
     47     tft.setCursor (100, 5);
     48     tft.setTextFont(1);        // Select font 1 which is the Adafruit GLCD font
     49     tft.print("Original Adafruit font!");
     50 
     51     // The new larger fonts do not need to use the .setCursor call, coords are embedded
     52     tft.setTextColor(TFT_BLACK); // Do not plot the background colour
     53     // Overlay the black text on top of the rainbow plot (the advantage of not drawing the backgorund colour!)
     54     tft.drawCentreString("Font size 2", 160, 14, 2); // Draw text centre at position 120, 14 using font 2
     55     tft.drawCentreString("Font size 4", 160, 30, 4); // Draw text centre at position 120, 30 using font 4
     56     tft.drawCentreString("12.34", 160, 54, 6);       // Draw text centre at position 120, 54 using font 6
     57     tft.drawCentreString("12.34 is in font 6", 160, 92, 2); // Draw text centre at position 120, 92 using font 2
     58     // Note the x, y position is the top left corner of the first character printed!
     59 
     60     // draw a floating point number
     61     float pi = 3.14159; // Value to print
     62     int precision = 3;  // Number of digits after decimal point
     63     int xpos = 130;     // x position
     64     int ypos = 110;     // y position
     65     int font = 2;       // font number 2
     66     xpos += tft.drawFloat(pi, precision, xpos, ypos, font); // Draw rounded number and return new xpos delta for next print position
     67     tft.drawString(" is pi", xpos, ypos, font);             // Continue printing from new x position
     68 
     69     tft.setTextSize(1);           // We are using a text size multiplier of 1
     70 
     71     tft.setTextColor(TFT_BLACK);  // Set text colour to black, no background (so transparent)
     72     tft.setCursor(76, 150, 4);    // Set cursor to x = 76, y = 150 and use font 4
     73     tft.println("Transparent...");  // As we use println, the cursor moves to the next line
     74 
     75     tft.setCursor(70, 175);    // Set cursor to x = 70, y = 175
     76     tft.setTextColor(TFT_WHITE, TFT_BLACK);  // Set text colour to white and background to black
     77     tft.println("White on black");
     78 
     79     tft.setTextFont(4);        // Select font 4 without moving cursor
     80     tft.setCursor(00, 210);    // Set cursor to x = 90, y = 210 without changing the font
     81     tft.setTextColor(TFT_WHITE);
     82     
     83     // By using the print class we can use all the formatting features like printing HEX
     84     tft.print(57005, HEX);    // Cursor does no move to next line
     85     tft.println(48879, HEX);  // print and move cursor to next line
     86 
     87     tft.setTextColor(TFT_GREEN, TFT_BLACK); // This time we will use green text on a black background
     88     tft.setTextFont(2); // Select font 2
     89     //Text will wrap to the next line if needed, by luck it breaks the lines at a space..
     90     tft.println(" Ode to a Small Lump of Green Putty I Found in My Armpit One Midsummer Morning ");
     91 
     92     tft.drawCentreString("34.56", 160, 300, 7);       // Draw text centre at position 120, 54 using font 6
     93     tft.drawCentreString("34.56 is in font 7", 160, 350, 2); // Draw text centre at position 120, 92 using font 2
     94 
     95     tft.drawCentreString("78.90", 160, 370, 8);       // Draw text centre at position 120, 54 using font 6
     96     tft.drawCentreString("78.90 is in font 8", 160, 450, 2); // Draw text centre at position 120, 92 using font 2
     97   }
     98 }
     99 
    100 // Fill screen with a rainbow pattern
    101 void rainbow_fill()
    102 {
    103   // The colours and state are not initialised so the start colour changes each time the funtion is called
    104   
    105   for (int i = 479; i > 0; i--) {
    106     // Draw a vertical line 1 pixel wide in the selected colour
    107     tft.drawFastHLine(0, i, tft.width(), colour); // in this example tft.width() returns the pixel width of the display
    108     // This is a "state machine" that ramps up/down the colour brightnesses in sequence
    109     switch (state) {
    110       case 0:
    111         green ++;
    112         if (green == 64) {
    113           green = 63;
    114           state = 1;
    115         }
    116         break;
    117       case 1:
    118         red--;
    119         if (red == 255) {
    120           red = 0;
    121           state = 2;
    122         }
    123         break;
    124       case 2:
    125         blue ++;
    126         if (blue == 32) {
    127           blue = 31;
    128           state = 3;
    129         }
    130         break;
    131       case 3:
    132         green --;
    133         if (green == 255) {
    134           green = 0;
    135           state = 4;
    136         }
    137         break;
    138       case 4:
    139         red ++;
    140         if (red == 32) {
    141           red = 31;
    142           state = 5;
    143         }
    144         break;
    145       case 5:
    146         blue --;
    147         if (blue == 255) {
    148           blue = 0;
    149           state = 0;
    150         }
    151         break;
    152     }
    153     colour = red << 11 | green << 5 | blue;
    154   }
    155 }
    156 
    157 
    158