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_8bit.ino (6474B)
1 /* 2 Display "flicker free" scrolling text and updating number 3 4 This sketch uses 8 bit colour sprites to save RAM. 5 6 Example for library: 7 https://github.com/Bodmer/TFT_eSPI 8 9 The sketch has been tested on a 320x240 ILI9341 based TFT, it 10 coule be adapted for other screen sizes. 11 12 A Sprite is notionally an invisible graphics screen that is 13 kept in the processors RAM. Graphics can be drawn into the 14 Sprite just as it can be drawn directly to the screen. Once 15 the Sprite is completed it can be plotted onto the screen in 16 any position. If there is sufficient RAM then the Sprite can 17 be the same size as the screen and used as a frame buffer. 18 19 A 16 bit colour Sprite occupies (2 * width * height) bytes. 20 21 An 8 bit colour Sprite occupies (width * height) bytes. 22 23 On a ESP8266, 16 bit Sprite sizes up to 128 x 160 can be accommodated, 24 this size requires 128*160*2 bytes (40kBytes) of RAM. 25 26 This sketch sets the colour depth to 8 bits so larger sprites can be 27 created. 8 bit colour sprites use half amount of RAM. If the colour 28 depth is not specified then 16 bits is assumed. 29 30 You need to make the sprite small enough to fit, with RAM spare for 31 any "local variables" that may be needed by your sketch and libraries. 32 33 Created by Bodmer 21/11/17 34 35 ######################################################################### 36 ###### DON'T FORGET TO UPDATE THE User_Setup.h FILE IN THE LIBRARY ###### 37 ######################################################################### 38 */ 39 40 // Size of sprite image for the scrolling text, this requires ~14 Kbytes of RAM 41 #define IWIDTH 240 42 #define IHEIGHT 30 43 44 // Pause in milliseconds to set scroll speed 45 #define WAIT 0 46 47 #include <TFT_eSPI.h> // Include the graphics library (this includes the sprite functions) 48 49 TFT_eSPI tft = TFT_eSPI(); // Create object "tft" 50 51 TFT_eSprite img = TFT_eSprite(&tft); // Create Sprite object "img" with pointer to "tft" object 52 // // the pointer is used by pushSprite() to push it onto the TFT 53 54 // ------------------------------------------------------------------------- 55 // Setup 56 // ------------------------------------------------------------------------- 57 void setup(void) { 58 tft.init(); 59 tft.setRotation(0); 60 61 tft.fillScreen(TFT_BLUE); 62 } 63 64 // ------------------------------------------------------------------------- 65 // Main loop 66 // ------------------------------------------------------------------------- 67 void loop() { 68 69 while (1) 70 { 71 // Set colour depth of Sprite to 8 (or 16) bits 72 img.setColorDepth(8); 73 74 // Create the sprite and clear background to black 75 img.createSprite(IWIDTH, IHEIGHT); 76 //img.fillSprite(TFT_BLACK); // Optional here as we fill the sprite later anyway 77 78 for (int pos = IWIDTH; pos > 0; pos--) 79 { 80 build_banner("Hello World", pos); 81 img.pushSprite(0, 0); 82 83 build_banner("TFT_eSPI sprite" , pos); 84 img.pushSprite(0, 50); 85 86 delay(WAIT); 87 } 88 89 // Delete sprite to free up the memory 90 img.deleteSprite(); 91 92 // Create a sprite of a different size 93 numberBox(random(100), 60, 100); 94 95 } 96 } 97 98 // ######################################################################### 99 // Build the scrolling sprite image from scratch, draw text at x = xpos 100 // ######################################################################### 101 102 void build_banner(String msg, int xpos) 103 { 104 int h = IHEIGHT; 105 106 // We could just use fillSprite(color) but lets be a bit more creative... 107 108 // Fill with rainbow stripes 109 while (h--) img.drawFastHLine(0, h, IWIDTH, rainbow(h * 4)); 110 111 // Draw some graphics, the text will appear to scroll over these 112 img.fillRect (IWIDTH / 2 - 20, IHEIGHT / 2 - 10, 40, 20, TFT_YELLOW); 113 img.fillCircle(IWIDTH / 2, IHEIGHT / 2, 10, TFT_ORANGE); 114 115 // Now print text on top of the graphics 116 img.setTextSize(1); // Font size scaling is x1 117 img.setTextFont(4); // Font 4 selected 118 img.setTextColor(TFT_BLACK); // Black text, no background colour 119 img.setTextWrap(false); // Turn of wrap so we can print past end of sprite 120 121 // Need to print twice so text appears to wrap around at left and right edges 122 img.setCursor(xpos, 2); // Print text at xpos 123 img.print(msg); 124 125 img.setCursor(xpos - IWIDTH, 2); // Print text at xpos - sprite width 126 img.print(msg); 127 } 128 129 // ######################################################################### 130 // Create sprite, plot graphics in it, plot to screen, then delete sprite 131 // ######################################################################### 132 void numberBox(int num, int x, int y) 133 { 134 // Create a sprite 80 pixels wide, 50 high (8kbytes of RAM needed) 135 img.createSprite(80, 50); 136 137 // Fill it with black 138 img.fillSprite(TFT_BLACK); 139 140 // Draw a backgorund of 2 filled triangles 141 img.fillTriangle( 0, 0, 0, 49, 40, 25, TFT_RED); 142 img.fillTriangle( 79, 0, 79, 49, 40, 25, TFT_DARKGREEN); 143 144 // Set the font parameters 145 img.setTextSize(1); // Font size scaling is x1 146 img.setFreeFont(&FreeSerifBoldItalic24pt7b); // Select free font 147 img.setTextColor(TFT_WHITE); // White text, no background colour 148 149 // Set text coordinate datum to middle centre 150 img.setTextDatum(MC_DATUM); 151 152 // Draw the number in middle of 80 x 50 sprite 153 img.drawNumber(num, 40, 25); 154 155 // Push sprite to TFT screen CGRAM at coordinate x,y (top left corner) 156 img.pushSprite(x, y); 157 158 // Delete sprite to free up the RAM 159 img.deleteSprite(); 160 } 161 162 163 // ######################################################################### 164 // Return a 16 bit rainbow colour 165 // ######################################################################### 166 unsigned int rainbow(byte value) 167 { 168 // Value is expected to be in range 0-127 169 // The value is converted to a spectrum colour from 0 = red through to 127 = blue 170 171 byte red = 0; // Red is the top 5 bits of a 16 bit colour value 172 byte green = 0;// Green is the middle 6 bits 173 byte blue = 0; // Blue is the bottom 5 bits 174 175 byte sector = value >> 5; 176 byte amplit = value & 0x1F; 177 178 switch (sector) 179 { 180 case 0: 181 red = 0x1F; 182 green = amplit; 183 blue = 0; 184 break; 185 case 1: 186 red = 0x1F - amplit; 187 green = 0x1F; 188 blue = 0; 189 break; 190 case 2: 191 red = 0; 192 green = 0x1F; 193 blue = amplit; 194 break; 195 case 3: 196 red = 0; 197 green = 0x1F - amplit; 198 blue = 0x1F; 199 break; 200 } 201 202 return red << 11 | green << 6 | blue; 203 } 204 205