ESP3D/libraries/TFT_eSPI-2.5.43/examples/320 x 240/TFT_Starfield/TFT_Starfield.ino
Luc 93312ff8b5
Idf 5.1.4/Arduino 3.0.4 porting for esp32 (#1046)
* Update WebSocket library
* Update SSDP library
* Update TFT_eSPI library
* Update EspLuaEngine library
* Update SDFat library
* Change to pioarduino
* Make ESP3DMessageFIFO and ESP3DMessage  more thread safe
* Fix sanity checks for BT
* Add some C6 support
* Refactor ethernet code
* Split Ethernet Sta / WiFi sta ESP Commands  and settings
* Simplify wait and wdtFeed code
* Set C3 with 4MB by default in platformio.ini
* Apply Disable brown out only on ESP32 to avoid crash e.g:ESP32S3
* Add missing entries in platformio.ini
2024-09-05 16:27:47 +08:00

89 lines
2.1 KiB
C++

// Animates white pixels to simulate flying through a star field
#include <SPI.h>
#include <TFT_eSPI.h>
// Use hardware SPI
TFT_eSPI tft = TFT_eSPI();
// With 1024 stars the update rate is ~65 frames per second
#define NSTARS 1024
uint8_t sx[NSTARS] = {};
uint8_t sy[NSTARS] = {};
uint8_t sz[NSTARS] = {};
uint8_t za, zb, zc, zx;
// Fast 0-255 random number generator from http://eternityforest.com/Projects/rng.php:
uint8_t __attribute__((always_inline)) rng()
{
zx++;
za = (za^zc^zx);
zb = (zb+za);
zc = ((zc+(zb>>1))^za);
return zc;
}
void setup() {
za = random(256);
zb = random(256);
zc = random(256);
zx = random(256);
Serial.begin(115200);
tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
// fastSetup() must be used immediately before fastPixel() to prepare screen
// It must be called after any other graphics drawing function call if fastPixel()
// is to be called again
//tft.fastSetup(); // Prepare plot window range for fast pixel plotting
}
void loop()
{
unsigned long t0 = micros();
uint8_t spawnDepthVariation = 255;
for(int i = 0; i < NSTARS; ++i)
{
if (sz[i] <= 1)
{
sx[i] = 160 - 120 + rng();
sy[i] = rng();
sz[i] = spawnDepthVariation--;
}
else
{
int old_screen_x = ((int)sx[i] - 160) * 256 / sz[i] + 160;
int old_screen_y = ((int)sy[i] - 120) * 256 / sz[i] + 120;
// This is a faster pixel drawing function for occasions where many single pixels must be drawn
tft.drawPixel(old_screen_x, old_screen_y,TFT_BLACK);
sz[i] -= 2;
if (sz[i] > 1)
{
int screen_x = ((int)sx[i] - 160) * 256 / sz[i] + 160;
int screen_y = ((int)sy[i] - 120) * 256 / sz[i] + 120;
if (screen_x >= 0 && screen_y >= 0 && screen_x < 320 && screen_y < 240)
{
uint8_t r, g, b;
r = g = b = 255 - sz[i];
tft.drawPixel(screen_x, screen_y, tft.color565(r,g,b));
}
else
sz[i] = 0; // Out of screen, die.
}
}
}
unsigned long t1 = micros();
//static char timeMicros[8] = {};
// Calculate frames per second
Serial.println(1.0/((t1 - t0)/1000000.0));
}