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

Sprite_scroll_4bit.ino (5083B)

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