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.ino (4478B)
1 /* 2 Sketch to show scrolling of the graphics in 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 16 bit Sprite occupies (2 * width * height) bytes in RAM. 18 19 An 8 bit Sprite occupies (width * height) bytes in RAM. 20 21 */ 22 23 #include <TFT_eSPI.h> 24 25 TFT_eSPI tft = TFT_eSPI(); 26 27 TFT_eSprite graph1 = TFT_eSprite(&tft); // Sprite object graph1 28 29 TFT_eSprite stext1 = TFT_eSprite(&tft); // Sprite object stext1 30 31 TFT_eSprite stext2 = TFT_eSprite(&tft); // Sprite object stext2 32 33 int graphVal = 1; 34 int delta = 1; 35 int grid = 0; 36 int tcount = 0; 37 38 //========================================================================================== 39 void setup() { 40 tft.init(); 41 tft.fillScreen(TFT_BLACK); 42 43 // Create a sprite for the graph 44 graph1.setColorDepth(8); 45 graph1.createSprite(128, 61); 46 graph1.fillSprite(TFT_BLUE); // Note: Sprite is filled with black when created 47 48 // The scroll area is set to the full sprite size upon creation of the sprite 49 // but we can change that by defining a smaller area using "setScrollRect()"if needed 50 // parameters are x,y,w,h,color as in drawRect(), the color fills the gap left by scrolling 51 //graph1.setScrollRect(64, 0, 64, 61, TFT_DARKGREY); // Try this line to change the graph scroll area 52 53 // Create a sprite for the scrolling numbers 54 stext1.setColorDepth(8); 55 stext1.createSprite(32, 64); 56 stext1.fillSprite(TFT_BLUE); // Fill sprite with blue 57 stext1.setScrollRect(0, 0, 32, 64, TFT_BLUE); // here we set scroll gap fill color to blue 58 stext1.setTextColor(TFT_WHITE); // White text, no background 59 stext1.setTextDatum(BR_DATUM); // Bottom right coordinate datum 60 61 // Create a sprite for Hello World 62 stext2.setColorDepth(8); 63 stext2.createSprite(80, 16); 64 stext2.fillSprite(TFT_DARKGREY); 65 stext2.setScrollRect(0, 0, 40, 16, TFT_DARKGREY); // Scroll the "Hello" in the first 40 pixels 66 stext2.setTextColor(TFT_WHITE); // White text, no background 67 } 68 69 //========================================================================================== 70 void loop() { 71 // Draw point in graph1 sprite at far right edge (this will scroll left later) 72 graph1.drawFastVLine(127,60-graphVal,2,TFT_YELLOW); // draw 2 pixel point on graph 73 74 // Draw number in stext1 sprite at 31,63 (bottom right datum set) 75 stext1.drawNumber(graphVal, 31, 63, 2); // plot value in font 2 76 77 // Push the sprites onto the TFT at specified coordinates 78 graph1.pushSprite(0, 0); 79 stext1.pushSprite(0, 64); 80 stext2.pushSprite(40, 70); 81 82 // Change the value to plot 83 graphVal+=delta; 84 85 // If the value reaches a limit, then change delta of value 86 if (graphVal >= 60) delta = -1; // ramp down value 87 else if (graphVal <= 1) delta = +1; // ramp up value 88 89 delay(50); // wait so things do not scroll too fast 90 91 // Now scroll the sprites scroll(dt, dy) where: 92 // dx is pixels to scroll, left = negative value, right = positive value 93 // dy is pixels to scroll, up = negative value, down = positive value 94 graph1.scroll(-1, 0); // scroll graph 1 pixel left, 0 up/down 95 stext1.scroll(0,-16); // scroll stext 0 pixels left/right, 16 up 96 stext2.scroll(1); // scroll stext 1 pixel right, up/down default is 0 97 98 // Draw the grid on far right edge of sprite as graph has now moved 1 pixel left 99 grid++; 100 if (grid >= 10) 101 { // Draw a vertical line if we have scrolled 10 times (10 pixels) 102 grid = 0; 103 graph1.drawFastVLine(127, 0, 61, TFT_NAVY); // draw line on graph 104 } 105 else 106 { // Otherwise draw points spaced 10 pixels for the horizontal grid lines 107 for (int p = 0; p <= 60; p += 10) graph1.drawPixel(127, p, TFT_NAVY); 108 } 109 110 tcount--; 111 if (tcount <=0) 112 { // If we have scrolled 40 pixels the redraw text 113 tcount = 40; 114 stext2.drawString("Hello World", 6, 0, 2); // draw at 6,0 in sprite, font 2 115 } 116 117 } // Loop back and do it all again 118 //==========================================================================================