acidportal

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

Sprite_scroll_1bit.ino (4899B)

      1 /*
      2   Sketch to show scrolling of the graphics in sprites.
      3 
      4   This sketch scrolls a 1 bit per pixel (1 bpp) Sprite.
      5   
      6   In a 1 bit Sprite any colour except TFT_BLACK turns a pixel "ON"
      7   TFT_BLACK turns a pixel "OFF".
      8 
      9   ON and OFF pixels can be set to any two colours before
     10   rendering to the screen with pushSprite, for example:
     11   tft.setBitmapColor(ON_COLOR, OFF_COLOR);
     12 
     13   Scrolling moves the pixels in a defined rectangle within
     14   the Sprite. By default the whole sprite is scrolled.
     15   The gap left by scrolling is filled with a defined colour.
     16 
     17   Example for library:
     18   https://github.com/Bodmer/TFT_eSPI
     19 
     20   A Sprite is notionally an invisible graphics screen that is
     21   kept in the processors RAM. Graphics can be drawn into the
     22   Sprite just as it can be drawn directly to the screen. Once
     23   the Sprite is completed it can be plotted onto the screen in
     24   any position. If there is sufficient RAM then the Sprite can
     25   be the same size as the screen and used as a frame buffer.
     26 
     27   A 1 bit Sprite occupies (width * height)/8 bytes in RAM.
     28 
     29 */
     30 
     31 #include <TFT_eSPI.h>
     32 
     33 TFT_eSPI tft = TFT_eSPI();
     34 
     35 TFT_eSprite graph1 = TFT_eSprite(&tft); // Sprite object graph1
     36 
     37 TFT_eSprite stext1 = TFT_eSprite(&tft); // Sprite object stext1
     38 
     39 TFT_eSprite stext2 = TFT_eSprite(&tft); // Sprite object stext2
     40 
     41 int graphVal = 1;
     42 int delta = 1;
     43 int grid = 0;
     44 int tcount = 0;
     45 
     46 //==========================================================================================
     47 void setup() {
     48   tft.init();
     49   tft.fillScreen(TFT_BLACK);
     50 
     51   // Create a sprite for the graph
     52   graph1.setColorDepth(1);
     53   graph1.createSprite(128, 61);
     54   graph1.fillSprite(TFT_BLACK); // Note: Sprite is filled with black when created
     55 
     56   // The scroll area is set to the full sprite size upon creation of the sprite
     57   // but we can change that by defining a smaller area using "setScrollRect()"if needed
     58   // parameters are x,y,w,h,color as in drawRect(), the color fills the gap left by scrolling
     59   //graph1.setScrollRect(64, 0, 64, 61, TFT_BLACK);  // Try this line to change the graph scroll area
     60 
     61   // Create a sprite for the scrolling numbers
     62   stext1.setColorDepth(1);
     63   stext1.createSprite(32, 64);
     64   stext1.fillSprite(TFT_BLACK); // Fill sprite with blue
     65   stext1.setScrollRect(0, 0, 32, 64, TFT_BLACK);     // here we set scroll gap fill color to blue
     66   stext1.setTextColor(TFT_WHITE); // White text, no background
     67   stext1.setTextDatum(BR_DATUM);  // Bottom right coordinate datum
     68 
     69   // Create a sprite for Hello World
     70   stext2.setColorDepth(1);
     71   stext2.createSprite(80, 16);
     72   stext2.fillSprite(TFT_BLACK);
     73   stext2.setScrollRect(0, 0, 40, 16, TFT_BLACK); // Scroll the "Hello" in the first 40 pixels
     74   stext2.setTextColor(TFT_WHITE); // White text, no background
     75 }
     76 
     77 //==========================================================================================
     78 void loop() {
     79   // Draw point in graph1 sprite at far right edge (this will scroll left later)
     80   graph1.drawFastVLine(127,60-graphVal,2,TFT_WHITE); // draw 2 pixel point on graph
     81 
     82   // Draw number in stext1 sprite at 31,63 (bottom right datum set)
     83   stext1.drawNumber(graphVal, 31, 63, 2); // plot value in font 2
     84 
     85   // Push the sprites onto the TFT at specified coordinates
     86   tft.setBitmapColor(TFT_WHITE, TFT_BLUE); // Specify the colours of the ON and OFF pixels
     87   graph1.pushSprite(0, 0);
     88 
     89   tft.setBitmapColor(TFT_GREEN, TFT_BLACK);
     90   stext1.pushSprite(0, 64);
     91 
     92   tft.setBitmapColor(TFT_BLACK, TFT_YELLOW);
     93   stext2.pushSprite(60, 70);
     94 
     95   // Change the value to plot
     96   graphVal+=delta;
     97 
     98   // If the value reaches a limit, then change delta of value
     99   if (graphVal >= 60)     delta = -1;  // ramp down value
    100   else if (graphVal <= 1) delta = +1;  // ramp up value
    101 
    102   delay(50); // wait so things do not scroll too fast
    103 
    104   // Now scroll the sprites scroll(dt, dy) where:
    105   // dx is pixels to scroll, left = negative value, right = positive value
    106   // dy is pixels to scroll, up = negative value, down = positive value
    107   graph1.scroll(-1, 0); // scroll graph 1 pixel left, 0 up/down
    108   stext1.scroll(0,-16); // scroll stext 0 pixels left/right, 16 up
    109   stext2.scroll(1);     // scroll stext 1 pixel right, up/down default is 0
    110 
    111   // Draw the grid on far right edge of sprite as graph has now moved 1 pixel left
    112   grid++;
    113   if (grid >= 10)
    114   { // Draw a vertical line if we have scrolled 10 times (10 pixels)
    115     grid = 0;
    116     graph1.drawFastVLine(127, 0, 61, TFT_WHITE); // draw line on graph
    117   }
    118   else
    119   { // Otherwise draw points spaced 10 pixels for the horizontal grid lines
    120     for (int p = 0; p <= 60; p += 10) graph1.drawPixel(127, p, TFT_WHITE);
    121   }
    122 
    123   tcount--;
    124   if (tcount <=0)
    125   { // If we have scrolled 40 pixels the redraw text
    126     tcount = 40;
    127     stext2.drawString("Hello World", 6, 0, 2); // draw at 6,0 in sprite, font 2
    128   }
    129 
    130 } // Loop back and do it all again
    131 //==========================================================================================