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_image_4bit.ino (3448B)
1 /* 2 3 Sketch to show how a Sprite can use a four-bit image with 4 a palette to change the appearance of an image while rendering 5 it only once. 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 color depth. 21 22 When 8 bit color 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 29 // Set delay after plotting the sprite 30 #define DELAY 30 31 32 // Width and height of sprite 33 #define WIDTH 164 34 #define HEIGHT 164 35 36 #include "sample_images.h" 37 38 TFT_eSPI tft = TFT_eSPI(); // Declare object "tft" 39 40 TFT_eSprite spr = TFT_eSprite(&tft); // Declare Sprite object "spr" with pointer to "tft" object 41 42 byte red = 31; // Red is the top 5 bits of a 16 bit colour value 43 byte green = 0;// Green is the middle 6 bits 44 byte blue = 0; // Blue is the bottom 5 bits 45 byte state = 0; 46 47 int rloop = 0; 48 int incr = 1; 49 50 uint16_t cmap[16]; 51 52 void setup() 53 { 54 Serial.begin(9600); 55 Serial.println(); 56 57 delay(50); 58 59 // Initialise the TFT registers 60 tft.init(); 61 62 spr.setColorDepth(4); 63 64 // Create a sprite of defined size 65 spr.createSprite(WIDTH, HEIGHT); 66 67 // Clear the TFT screen to black 68 tft.fillScreen(TFT_BLACK); 69 70 // push the image - only need to do this once. 71 spr.pushImage(2, 2, 160, 160, (uint16_t *)stars); 72 73 for (int i = 0; i < 16; i++) 74 cmap[i] = rainbow(); 75 } 76 77 void loop(void) 78 { 79 // create a palette with the defined colors and push it. 80 spr.createPalette(cmap, 16); 81 spr.pushSprite(tft.width() / 2 - WIDTH / 2, tft.height() / 2 - HEIGHT / 2); 82 83 // update the colors 84 for (int i = 0; i < 15; i++) { 85 cmap[i] = cmap[i + 1]; 86 } 87 if (incr == 2) { 88 (void)rainbow(); // skip alternate steps to go faster 89 } 90 cmap[15] = rainbow(); 91 rloop += incr; 92 if (rloop > 0xc0) { 93 incr = incr == 2 ? 1 : 2; 94 rloop = 0; 95 96 } 97 delay(DELAY); 98 99 } 100 101 // ######################################################################### 102 // Return a 16 bit rainbow colour 103 // ######################################################################### 104 unsigned int rainbow() 105 { 106 switch (state) { 107 case 0: 108 green ++; 109 if (green == 64) { 110 green = 63; 111 state = 1; 112 } 113 break; 114 case 1: 115 red--; 116 if (red == 255) { 117 red = 0; 118 state = 2; 119 } 120 break; 121 case 2: 122 blue ++; 123 if (blue == 32) { 124 blue = 31; 125 state = 3; 126 } 127 break; 128 case 3: 129 green --; 130 if (green == 255) { 131 green = 0; 132 state = 4; 133 } 134 break; 135 case 4: 136 red ++; 137 if (red == 32) { 138 red = 31; 139 state = 5; 140 } 141 break; 142 case 5: 143 blue --; 144 if (blue == 255) { 145 blue = 0; 146 state = 0; 147 } 148 break; 149 } 150 return red << 11 | green << 5 | blue; 151 } 152