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

Viewport_Demo.ino (3498B)

      1 // Viewport Demo
      2 
      3 // See viewport_commands tab for details of functions available
      4 
      5 // This example uses the viewport commands to create a "virtual TFT" within the
      6 // normal TFT display area. This allows a sketch written for a smaller screen to
      7 // be run in a viewport window. By default, the graphics 0,0 datum is set to the
      8 // top left corner of the viewport, but optionally the datum can be kept at the
      9 // corner of the TFT.
     10 
     11 // Viewports have a number of potential uses:
     12 // -  create a "virtual" TFT screen smaller than the actual TFT screen
     13 //  - render GUI items (menus etc) in a viewport, erase GUI item by redrawing whole screen,
     14 //    this will be fast because only the viewport will be refreshed (e.g. clearing menu)
     15 //  - limit screen refresh to a particular area, e.g. changing numbers, icons or graph plotting
     16 //  - showing a small portion of a larger image or sprite, this allows panning and scrolling
     17 
     18 // A viewport can have the coordinate datum (position 0,0) either at the top left corner of
     19 // the viewport or at the normal top left corner of the TFT.
     20 // Putting the coordinate datum at the viewport corner means that functions that draw graphics
     21 // in a fixed position can be relocated anywhere on the screen. (see plotBox() below). This
     22 // makes it easier to reposition groups of graphical objects (for example GUI buttons) that have
     23 // fixed relative positions.
     24 
     25 #include <SPI.h>
     26 #include <TFT_eSPI.h>
     27 
     28 TFT_eSPI tft = TFT_eSPI();
     29 
     30 void setup() {
     31   Serial.begin(115200);
     32 
     33   tft.init();
     34   tft.setRotation(1);
     35   tft.fillScreen(TFT_BLACK);
     36 }
     37 
     38 void loop()
     39 {
     40   // Normal Screen
     41   drawX();
     42 
     43   delay(2000);
     44 
     45   // Viewport screen
     46   tft.setViewport(10, 10, 140, 100);
     47   tft.frameViewport(TFT_NAVY, -2);
     48   tft.fillScreen(TFT_BLACK);
     49   drawX();
     50   tft.resetViewport();
     51 
     52   delay(2000);
     53 
     54   //Normal screen
     55   tft.fillScreen(TFT_BLACK);
     56   drawX();
     57 
     58   delay(2000);
     59 
     60   tft.fillScreen(TFT_BLACK);
     61 
     62   // Viewport as a clipping window (false parameter means coordinate datum stays at TFT top left)
     63   tft.setViewport(10, 10, tft.width()/2 - 10, tft.height() - 20, false);
     64   //tft.frameViewport(TFT_NAVY,  2); // Add 2 pixel border inside viewport
     65   //tft.frameViewport(TFT_NAVY, -2); // Add 2 pixel border outside viewport
     66   drawX();
     67 
     68   delay(2000);
     69 
     70   while(1)
     71   {
     72     tft.resetViewport(); // Reset viewport so width() and height() return TFT size
     73 
     74     uint16_t w = 40;
     75     uint16_t h = 40;
     76     uint16_t x = random(tft.width()  - w);
     77     uint16_t y = random(tft.height() - h);
     78 
     79     tft.setViewport(x, y, w, h);
     80 
     81     plotBox();
     82 
     83     delay(0);
     84   }
     85 }
     86 
     87 void drawX(void)
     88 {
     89   tft.fillScreen(tft.color565(25,25,25)); // Grey
     90 
     91   // Draw circle
     92   tft.drawCircle(tft.width()/2, tft.height()/2, tft.width()/4, TFT_RED);
     93 
     94   // Draw diagonal lines
     95   tft.drawLine(0 ,                0, tft.width()-1, tft.height()-1, TFT_GREEN);
     96   tft.drawLine(0 , tft.height()-1, tft.width()-1,                0, TFT_BLUE);
     97 
     98   tft.setTextDatum(MC_DATUM);
     99   tft.setTextColor(TFT_WHITE, tft.color565(25,25,25));
    100   tft.drawString("Hello World!", tft.width()/2, tft.height()/2, 4); // Font 4
    101 }
    102 
    103 void plotBox(void)
    104 {
    105   // These are always plotted at a fixed position but they can 
    106   // be plotted into a viewport anywhere on the screen because
    107   // a viewport can move the screen datum
    108   tft.fillScreen(TFT_BLACK); // When a viewport is set, this just fills the viewport
    109   tft.drawRect(0,0, 40,40, TFT_BLUE);
    110   tft.setTextDatum(MC_DATUM);
    111   tft.setTextColor(TFT_WHITE);
    112   tft.drawNumber( random(100), 20, 23, 4); // Number in font 4
    113 }