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

TFT_Screen_Capture.ino (7212B)

      1 /*
      2   This sketch has been written to test the Processing screenshot client.
      3 
      4   It has been created to work with the TFT_eSPI library here:
      5   https://github.com/Bodmer/TFT_eSPI
      6 
      7   It sends screenshots to a PC running a Processing client sketch.
      8 
      9   The Processing IDE that will run the client sketch can be downloaded
     10   here:  https://processing.org/
     11 
     12   The Processing sketch needed is contained within a tab attached to this
     13   Arduino sketch. Cut and paste that tab into the Processing IDE and run.
     14   Read the Processing sketch header for instructions.
     15 
     16   This sketch uses the GLCD, 2, 4, 6 fonts only.
     17 
     18   Make sure all the display driver and pin connections are correct by
     19   editing the User_Setup.h file in the TFT_eSPI library folder.
     20 
     21   Maximum recommended SPI clock rate is 27MHz when reading pixels, 40MHz
     22   seems to be OK with ILI9341 displays but this is above the manufacturers
     23   specified maximum clock rate.
     24 
     25   In the setup file you can define different write and read SPI clock rates
     26   
     27   In the setup file you can define TFT_SDA_READ for a TFT with bi-directional
     28   SDA pin (otherwise the normal MISO pin will be used to read from the TFT)
     29 
     30   >>>>   NOTE: NOT ALL TFTs SUPPORT READING THE CGRAM (pixel) MEMORY   <<<<
     31 
     32   #########################################################################
     33   ###### DON'T FORGET TO UPDATE THE User_Setup.h FILE IN THE LIBRARY ######
     34   #########################################################################
     35 */
     36 
     37 // Created by: Bodmer  5/3/17
     38 // Updated by: Bodmer 10/3/17
     39 // Updated by: Bodmer 23/11/18 to support SDA reads and the ESP32
     40 // Version: 0.07
     41 
     42 // MIT licence applies, all text above must be included in derivative works
     43 
     44 #include <TFT_eSPI.h> // Hardware-specific library
     45 #include <SPI.h>
     46 
     47 TFT_eSPI tft = TFT_eSPI(); // Invoke custom library
     48 
     49 unsigned long targetTime = 0;
     50 byte red = 0x1F;
     51 byte green = 0;
     52 byte blue = 0;
     53 byte state = 0;
     54 unsigned int colour = red << 11; // Colour order is RGB 5+6+5 bits each
     55 
     56 void setup(void) {
     57   Serial.begin(921600); // Set to a high rate for fast image transfer to a PC
     58 
     59   tft.init();
     60   tft.setRotation(0);
     61   tft.fillScreen(TFT_BLACK);
     62 
     63   randomSeed(analogRead(A0));
     64 
     65   targetTime = millis() + 1000;
     66 }
     67 
     68 #define RGB_TEST false // true produces a simple RGB color test screen
     69 
     70 void loop() {
     71 
     72   if (targetTime < millis()) {
     73     if (!RGB_TEST)
     74     {
     75       targetTime = millis() + 1500; // Wait a minimum of 1.5s
     76 
     77       tft.setRotation(random(4));
     78       rainbow_fill(); // Fill the screen with rainbow colours
     79 
     80       tft.setTextColor(TFT_BLACK); // Text background is not defined so it is transparent
     81       tft.setTextDatum(TC_DATUM);  // Top Centre datum
     82       int xpos = tft.width() / 2;  // Centre of screen
     83 
     84       tft.setTextFont(0);        // Select font 0 which is the Adafruit font
     85       tft.drawString("Original Adafruit font!", xpos, 5);
     86 
     87       // The new larger fonts do not need to use the .setCursor call, coords are embedded
     88       tft.setTextColor(TFT_BLACK); // Do not plot the background colour
     89 
     90       // Overlay the black text on top of the rainbow plot (the advantage of not drawing the background colour!)
     91       tft.drawString("Font size 2", xpos, 14, 2); // Draw text centre at position xpos, 14 using font 2
     92       tft.drawString("Font size 4", xpos, 30, 4); // Draw text centre at position xpos, 30 using font 4
     93       tft.drawString("12.34", xpos, 54, 6);       // Draw text centre at position xpos, 54 using font 6
     94 
     95       tft.drawString("12.34 is in font size 6", xpos, 92, 2); // Draw text centre at position xpos, 92 using font 2
     96       // Note the x position is the top of the font!
     97 
     98       // draw a floating point number
     99       float pi = 3.1415926; // Value to print
    100       int precision = 3;    // Number of digits after decimal point
    101 
    102       int ypos = 110;     // y position
    103 
    104       tft.setTextDatum(TR_DATUM); // Top Right datum so text butts neatly to xpos (right justified)
    105 
    106       tft.drawFloat(pi, precision, xpos, ypos, 2); // Draw rounded number and return new xpos delta for next print position
    107 
    108       tft.setTextDatum(TL_DATUM);  // Top Left datum so text butts neatly to xpos (left justified)
    109 
    110       tft.drawString(" is pi", xpos, ypos, 2);
    111 
    112       tft.setTextSize(1);           // We are using a font size multiplier of 1
    113       tft.setTextDatum(TC_DATUM);   // Top Centre datum
    114       tft.setTextColor(TFT_BLACK);  // Set text colour to black, no background (so transparent)
    115 
    116       tft.drawString("Transparent...", xpos, 125, 4);  // Font 4
    117 
    118       tft.setTextColor(TFT_WHITE, TFT_BLACK);          // Set text colour to white and background to black
    119       tft.drawString("White on black", xpos, 150, 4);  // Font 4
    120 
    121       tft.setTextColor(TFT_GREEN, TFT_BLACK); // This time we will use green text on a black background
    122 
    123       tft.setTextFont(2); // Select font 2, now we do not need to specify the font in drawString()
    124 
    125       // An easier way to position text and blank old text is to set the datum and use width padding
    126       tft.setTextDatum(BC_DATUM);          // Bottom centre for text datum
    127       tft.setTextPadding(tft.width() + 1); // Pad text to full screen width + 1 spare for +/-1 position rounding
    128 
    129       tft.drawString("Ode to a Small Lump of Green Putty", xpos, 230 - 32);
    130       tft.drawString("I Found in My Armpit One Midsummer", xpos, 230 - 16);
    131       tft.drawString("Morning", xpos, 230);
    132 
    133       tft.setTextDatum(TL_DATUM); // Reset to top left for text datum
    134       tft.setTextPadding(0);      // Reset text padding to 0 pixels
    135 
    136       // Now call the screen server to send a copy of the TFT screen to the PC running the Processing client sketch
    137       screenServer();
    138     }
    139     else
    140     {
    141       tft.fillScreen(TFT_BLACK);
    142       tft.fillRect( 0, 0, 16, 16, TFT_RED);
    143       tft.fillRect(16, 0, 16, 16, TFT_GREEN);
    144       tft.fillRect(32, 0, 16, 16, TFT_BLUE);
    145       screenServer();
    146     }
    147   }
    148 }
    149 
    150 // Fill screen with a rainbow pattern
    151 void rainbow_fill()
    152 {
    153   // The colours and state are not initialised so the start colour changes each time the function is called
    154   int rotation = tft.getRotation();
    155   tft.setRotation(random(4));
    156   for (int i = tft.height() - 1; i >= 0; i--) {
    157     // This is a "state machine" that ramps up/down the colour brightnesses in sequence
    158     switch (state) {
    159       case 0:
    160         green ++;
    161         if (green == 64) {
    162           green = 63;
    163           state = 1;
    164         }
    165         break;
    166       case 1:
    167         red--;
    168         if (red == 255) {
    169           red = 0;
    170           state = 2;
    171         }
    172         break;
    173       case 2:
    174         blue ++;
    175         if (blue == 32) {
    176           blue = 31;
    177           state = 3;
    178         }
    179         break;
    180       case 3:
    181         green --;
    182         if (green == 255) {
    183           green = 0;
    184           state = 4;
    185         }
    186         break;
    187       case 4:
    188         red ++;
    189         if (red == 32) {
    190           red = 31;
    191           state = 5;
    192         }
    193         break;
    194       case 5:
    195         blue --;
    196         if (blue == 255) {
    197           blue = 0;
    198           state = 0;
    199         }
    200         break;
    201     }
    202     colour = red << 11 | green << 5 | blue;
    203     // Draw a line 1 pixel wide in the selected colour
    204     tft.drawFastHLine(0, i, tft.width(), colour); // tft.width() returns the pixel width of the display
    205   }
    206   tft.setRotation(rotation);
    207 }