acidportal

- 😈 Worlds smallest Evil Portal on a LilyGo T-QT
git clone git://git.acid.vegas/acidportal.git
Log | Files | Refs | Archive | README | LICENSE

TFT_Starfield.ino (2189B)

      1 // Animates white pixels to simulate flying through a star field
      2 
      3 #include <SPI.h>
      4 #include <TFT_eSPI.h>
      5 
      6 // Use hardware SPI
      7 TFT_eSPI tft = TFT_eSPI();
      8 
      9 // With 1024 stars the update rate is ~65 frames per second
     10 #define NSTARS 1024
     11 uint8_t sx[NSTARS] = {};
     12 uint8_t sy[NSTARS] = {};
     13 uint8_t sz[NSTARS] = {};
     14 
     15 uint8_t za, zb, zc, zx;
     16 
     17 // Fast 0-255 random number generator from http://eternityforest.com/Projects/rng.php:
     18 uint8_t __attribute__((always_inline)) rng()
     19 {
     20   zx++;
     21   za = (za^zc^zx);
     22   zb = (zb+za);
     23   zc = ((zc+(zb>>1))^za);
     24   return zc;
     25 }
     26 
     27 void setup() {
     28   za = random(256);
     29   zb = random(256);
     30   zc = random(256);
     31   zx = random(256);
     32 
     33   Serial.begin(115200);
     34   tft.init();
     35   tft.setRotation(1);
     36   tft.fillScreen(TFT_BLACK);
     37 
     38   // fastSetup() must be used immediately before fastPixel() to prepare screen
     39   // It must be called after any other graphics drawing function call if fastPixel()
     40   // is to be called again
     41   //tft.fastSetup(); // Prepare plot window range for fast pixel plotting
     42 }
     43 
     44 void loop()
     45 {
     46   unsigned long t0 = micros();
     47   uint8_t spawnDepthVariation = 255;
     48 
     49   for(int i = 0; i < NSTARS; ++i)
     50   {
     51     if (sz[i] <= 1)
     52     {
     53       sx[i] = 160 - 120 + rng();
     54       sy[i] = rng();
     55       sz[i] = spawnDepthVariation--;
     56     }
     57     else
     58     {
     59       int old_screen_x = ((int)sx[i] - 160) * 256 / sz[i] + 160;
     60       int old_screen_y = ((int)sy[i] - 120) * 256 / sz[i] + 120;
     61 
     62       // This is a faster pixel drawing function for occassions where many single pixels must be drawn
     63       tft.drawPixel(old_screen_x, old_screen_y,TFT_BLACK);
     64 
     65       sz[i] -= 2;
     66       if (sz[i] > 1)
     67       {
     68         int screen_x = ((int)sx[i] - 160) * 256 / sz[i] + 160;
     69         int screen_y = ((int)sy[i] - 120) * 256 / sz[i] + 120;
     70   
     71         if (screen_x >= 0 && screen_y >= 0 && screen_x < 320 && screen_y < 240)
     72         {
     73           uint8_t r, g, b;
     74           r = g = b = 255 - sz[i];
     75           tft.drawPixel(screen_x, screen_y, tft.color565(r,g,b));
     76         }
     77         else
     78           sz[i] = 0; // Out of screen, die.
     79       }
     80     }
     81   }
     82   unsigned long t1 = micros();
     83   //static char timeMicros[8] = {};
     84 
     85  // Calcualte frames per second
     86   Serial.println(1.0/((t1 - t0)/1000000.0));
     87 }
     88 
     89