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_draw.ino (4159B)
1 /* 2 3 Sketch to show how a Sprite is created, how to draw pixels 4 and text within the Sprite and then push the Sprite onto 5 the display screen. 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 On a ESP8266 Sprite sizes up to 126 x 160 can be accommodated, 20 this size requires 40kBytes of RAM for a 16 bit colour depth. 21 22 When 8 bit colour depth sprites are created they occupy 23 (width * height) bytes in RAM, so larger sprites can be 24 created, or the RAM required is halved. 25 26 */ 27 28 // Set delay after plotting the sprite 29 #define DELAY 1000 30 31 // Width and height of sprite 32 #define WIDTH 128 33 #define HEIGHT 128 34 35 #include <TFT_eSPI.h> // Include the graphics library (this includes the sprite functions) 36 37 TFT_eSPI tft = TFT_eSPI(); // Declare object "tft" 38 39 TFT_eSprite spr = TFT_eSprite(&tft); // Declare Sprite object "spr" with pointer to "tft" object 40 41 void setup() 42 { 43 Serial.begin(250000); 44 Serial.println(); 45 46 // Initialise the TFT registers 47 tft.init(); 48 49 // Optionally set colour depth to 8 or 16 bits, default is 16 if not specified 50 // spr.setColorDepth(8); 51 52 // Create a sprite of defined size 53 spr.createSprite(WIDTH, HEIGHT); 54 55 // Clear the TFT screen to blue 56 tft.fillScreen(TFT_BLUE); 57 } 58 59 void loop(void) 60 { 61 // Fill the whole sprite with black (Sprite is in memory so not visible yet) 62 spr.fillSprite(TFT_BLACK); 63 64 // Number of pixels to draw 65 uint16_t n = 100; 66 67 // Draw 100 random colour pixels at random positions in sprite 68 while (n--) 69 { 70 uint16_t colour = random(0x10000); // Returns colour 0 - 0xFFFF 71 int16_t x = random(WIDTH); // Random x coordinate 72 int16_t y = random(HEIGHT); // Random y coordinate 73 spr.drawPixel( x, y, colour); // Draw pixel in sprite 74 } 75 76 // Draw some lines 77 spr.drawLine(1, 0, WIDTH, HEIGHT-1, TFT_GREEN); 78 spr.drawLine(0, 0, WIDTH, HEIGHT, TFT_GREEN); 79 spr.drawLine(0, 1, WIDTH-1, HEIGHT, TFT_GREEN); 80 spr.drawLine(0, HEIGHT-1, WIDTH-1, 0, TFT_RED); 81 spr.drawLine(0, HEIGHT, WIDTH, 0, TFT_RED); 82 spr.drawLine(1, HEIGHT, WIDTH, 1, TFT_RED); 83 84 // Draw some text with Middle Centre datum 85 spr.setTextDatum(MC_DATUM); 86 spr.drawString("Sprite", WIDTH / 2, HEIGHT / 2, 4); 87 88 // Now push the sprite to the TFT at position 0,0 on screen 89 spr.pushSprite(-40, -40); 90 spr.pushSprite(tft.width() / 2 - WIDTH / 2, tft.height() / 2 - HEIGHT / 2); 91 spr.pushSprite(tft.width() - WIDTH + 40, tft.height() - HEIGHT + 40); 92 93 delay(DELAY); 94 95 // Fill TFT screen with blue 96 tft.fillScreen(TFT_BLUE); 97 98 // Draw a blue rectangle in sprite so when we move it 1 pixel it does not leave a trail 99 // on the blue screen background 100 spr.drawRect(0, 0, WIDTH, HEIGHT, TFT_BLUE); 101 102 int x = tft.width() / 2 - WIDTH / 2; 103 int y = tft.height() / 2 - HEIGHT / 2; 104 105 uint32_t updateTime = 0; // time for next update 106 107 while (true) 108 { 109 // Random movement direction 110 int dx = 1; if (random(2)) dx = -1; 111 int dy = 1; if (random(2)) dy = -1; 112 113 // Pull it back onto screen if it wanders off 114 if (x < -WIDTH/2) dx = 1; 115 if (x >= tft.width()-WIDTH/2) dx = -1; 116 if (y < -HEIGHT/2) dy = 1; 117 if (y >= tft.height()-HEIGHT/2) dy = -1; 118 119 // Draw it 50 time, moving in random direct or staying still 120 n = 50; 121 int wait = random (50); 122 while (n) 123 { 124 if (updateTime <= millis()) 125 { 126 // Use time delay so sprite does not move fast when not all on screen 127 updateTime = millis() + wait; 128 129 // Push the sprite to the TFT screen 130 spr.pushSprite(x, y); 131 132 // Change coord for next loop 133 x += dx; 134 y += dy; 135 n--; 136 yield(); // Stop watchdog reset 137 } 138 } 139 } // Infinite while, will not exit! 140 } 141