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