Created
February 5, 2021 20:36
-
-
Save tobozo/dac14b82814b417d5e1197a9b3307c7e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP32-Chimera-Core.h> | |
#define tft M5.Lcd | |
const uint16_t palette[] = { | |
63488, 63520, 63584, 63616, 63680, 63712, 63776, 63808, | |
63872, 63904, 63968, 64000, 64064, 64096, 64160, 64192, | |
64224, 64288, 64320, 64384, 64416, 64480, 64512, 64576, | |
64608, 64672, 64704, 64768, 64800, 64864, 64896, 64960, | |
64992, 65056, 65088, 65152, 65184, 65248, 65280, 65344, | |
65376, 65440, 65472, 65504, 63456, 63456, 61408, 59360, | |
57312, 57312, 55264, 53216, 51168, 51168, 49120, 47072, | |
45024, 45024, 42976, 40928, 38880, 38880, 36832, 34784, | |
32736, 32736, 30688, 28640, 26592, 26592, 24544, 22496, | |
22496, 20448, 18400, 16352, 16352, 14304, 12256, 10208, | |
10208, 8160, 6112, 4064, 4064, 2016, 2016, 2017, | |
2018, 2018, 2019, 2020, 2021, 2021, 2022, 2023, | |
2024, 2024, 2025, 2026, 2026, 2027, 2028, 2029, | |
2029, 2030, 2031, 2032, 2032, 2033, 2034, 2035, | |
2035, 2036, 2037, 2038, 2038, 2039, 2040, 2041, | |
2041, 2042, 2043, 2044, 2044, 2045, 2046, 2047, | |
2047, 2015, 1951, 1919, 1855, 1823, 1759, 1727, | |
1663, 1631, 1567, 1535, 1471, 1439, 1375, 1343, | |
1279, 1247, 1183, 1151, 1087, 1055, 991, 959, | |
895, 863, 799, 767, 703, 671, 639, 575, | |
543, 479, 447, 383, 351, 287, 255, 191, | |
159, 95, 63, 31, 2079, 2079, 4127, 6175, | |
8223, 8223, 10271, 12319, 14367, 14367, 16415, 18463, | |
20511, 20511, 22559, 24607, 24607, 26655, 28703, 30751, | |
30751, 32799, 34847, 36895, 36895, 38943, 40991, 43039, | |
43039, 45087, 47135, 49183, 49183, 51231, 53279, 55327, | |
55327, 57375, 59423, 61471, 61471, 63519, 63519, 63518, | |
63517, 63517, 63516, 63515, 63514, 63514, 63513, 63512, | |
63511, 63511, 63510, 63509, 63508, 63508, 63507, 63506, | |
63505, 63505, 63504, 63503, 63502, 63502, 63501, 63500, | |
63499, 63499, 63498, 63497, 63497, 63496, 63495, 63494, | |
63494, 63493, 63492, 63491, 63491, 63490, 63489, 63488, | |
}; | |
TFT_eSprite *canvas = new TFT_eSprite( &tft ); | |
static uint8_t currentSprite = 0; | |
static TaskHandle_t *plasmaTaskHandle = NULL; | |
uint16_t *fb; // frame buffer | |
uint8_t *plasma; // pointer to plasma array | |
uint32_t canvasHeight=0, canvasWidth=0; | |
// fps counter | |
unsigned long framesCount = 0; | |
uint32_t fstart = millis(); | |
int fpsInterval = 100; | |
float fpsscale = 1000.0/fpsInterval; // fpi to fps | |
int fps = 0; // frames per second | |
int lastfps = -1; | |
float fpi = 0; // frames per interval | |
void renderFPS() { | |
unsigned long nowMillis = millis(); | |
if(nowMillis - fstart >= fpsInterval) { | |
fpi = float(framesCount * fpsInterval) / float(nowMillis - fstart); | |
fps = int(fpi*fpsscale); | |
fstart = nowMillis; | |
framesCount = 0; | |
} else { | |
framesCount++; | |
} | |
if( fps != lastfps ) { | |
lastfps = fps; | |
} | |
char text[12] = {0}; | |
snprintf( text, 12, "fps: %3d", fps ); | |
canvas->drawString( text, 0,0 ); | |
} | |
void initPlasma() | |
{ | |
for ( uint16_t y = 0; y < canvasHeight; y++) { | |
for ( uint16_t x = 0; x < canvasWidth; x++) { | |
plasma[ y * canvasWidth + x ] = ( | |
128.0 + ( 128.0 * cos ( x / 16.0 ) ) | |
- 128.0 + (128.0 * sin ( y / 32.0 ) ) | |
- 128.0 + (128.0 * sin(sqrt((x - canvasWidth / 2.0)* (x - canvasWidth / 2.0) + (y - canvasHeight / 2.0) * (y - canvasHeight / 2.0)) / 8.0)) | |
) / 3; | |
} | |
} | |
} | |
void drawPlasma( uint8_t paletteShift ) | |
{ | |
for ( uint16_t y = 0; y < canvasHeight; y++) { | |
for ( uint16_t x = 0; x < canvasWidth; x++) { | |
int colorIndex = ( plasma[ y * canvasWidth + x ] + paletteShift ) % 256; | |
uint16_t swapped = (palette[ colorIndex ]>>8) | (palette[ colorIndex ]<<8); | |
fb[ y * canvasWidth + x ] = swapped; | |
} | |
} | |
renderFPS(); | |
canvas->pushSprite(0,0); | |
} | |
static void animatePlasma( void * param ) | |
{ | |
while ( true ) { | |
for (int paletteShift = 0; paletteShift < 256; paletteShift+=2 ) { | |
drawPlasma ( paletteShift ); | |
vTaskDelay(1); | |
} | |
} | |
} | |
void startPlasmaTask() | |
{ | |
if( plasmaTaskHandle == NULL ) { | |
log_w("Starting plasma task"); | |
xTaskCreatePinnedToCore( animatePlasma, "animatePlasma", 2048, NULL, 16, plasmaTaskHandle, 0 ); | |
} | |
} | |
void stopPlasmaTask() | |
{ | |
if( plasmaTaskHandle != NULL ) { | |
log_w("Stopping plasma task"); | |
vTaskDelete( plasmaTaskHandle ); | |
//delay(10); | |
plasmaTaskHandle = NULL; | |
} | |
} | |
void plasma_main() | |
{ | |
psramInit(); | |
canvasWidth = tft.width(); | |
canvasHeight = tft.height(); | |
plasma = (uint8_t *) ps_malloc( canvasHeight * canvasWidth * sizeof ( uint8_t ) ); | |
canvas->setColorDepth(16); | |
canvas->setPsram(false); | |
fb = (uint16_t*)canvas->createSprite( canvasWidth, canvasHeight ); | |
if( fb == NULL ) { | |
log_e("Could not create sprite"); | |
while(1); | |
} | |
canvas->setTextSize(2); | |
canvas->setTextColor( TFT_WHITE, TFT_BLACK ); | |
canvas->setTextDatum( TL_DATUM ); | |
initPlasma(); | |
startPlasmaTask(); | |
} | |
void setup() | |
{ | |
M5.begin( true ); | |
tft.setRotation(2); | |
tft.setColorDepth( 8 ); | |
plasma_main(); | |
} | |
void loop() | |
{ | |
vTaskDelete( NULL ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment