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_Mandlebrot.ino (2210B)

      1 // Mandlebrot
      2 
      3 // This will run quite slowly due to the large number of floating point calculations per pixel
      4 
      5 #include <TFT_eSPI.h> // Hardware-specific library
      6 #include <SPI.h>
      7 
      8 TFT_eSPI tft = TFT_eSPI();       // Invoke custom library
      9 
     10 #define TFT_GREY 0x7BEF
     11 
     12 unsigned long runTime = 0;
     13 
     14 float sx = 0, sy = 0;
     15 uint16_t x0 = 0, x1 = 0, yy0 = 0, yy1 = 0;
     16 
     17 void setup()
     18 {
     19   Serial.begin(250000);
     20   //randomSeed(analogRead(A0));
     21   Serial.println();
     22   // Setup the LCD
     23   tft.init();
     24   tft.setRotation(3);
     25 }
     26 
     27 void loop()
     28 {
     29   runTime = millis();
     30 
     31   tft.fillScreen(TFT_BLACK);
     32   tft.startWrite();
     33   for (int px = 1; px < 320; px++)
     34   {
     35     for (int py = 0; py < 240; py++)
     36     {
     37       float x0 = (map(px, 0, 320, -250000/2, -242500/2)) / 100000.0; //scaled x coordinate of pixel (scaled to lie in the Mandelbrot X scale (-2.5, 1))
     38       float yy0 = (map(py, 0, 240, -75000/4, -61000/4)) / 100000.0; //scaled y coordinate of pixel (scaled to lie in the Mandelbrot Y scale (-1, 1))
     39       float xx = 0.0;
     40       float yy = 0.0;
     41       int iteration = 0;
     42       int max_iteration = 128;
     43       while ( ((xx * xx + yy * yy) < 4)  &&  (iteration < max_iteration) )
     44       {
     45         float xtemp = xx * xx - yy * yy + x0;
     46         yy = 2 * xx * yy + yy0;
     47         xx = xtemp;
     48         iteration++;
     49       }
     50       int color = rainbow((3*iteration+64)%128);
     51       yield();tft.drawPixel(px, py, color);
     52     }
     53   }
     54   tft.endWrite();
     55 
     56   Serial.println(millis()-runTime);
     57   while(1) yield();
     58 }
     59 
     60 unsigned int rainbow(int value)
     61 {
     62   // Value is expected to be in range 0-127
     63   // The value is converted to a spectrum colour from 0 = blue through to red = blue
     64 
     65   byte red = 0; // Red is the top 5 bits of a 16 bit colour value
     66   byte green = 0;// Green is the middle 6 bits
     67   byte blue = 0; // Blue is the bottom 5 bits
     68 
     69   byte quadrant = value / 32;
     70 
     71   if (quadrant == 0) {
     72     blue = 31;
     73     green = 2 * (value % 32);
     74     red = 0;
     75   }
     76   if (quadrant == 1) {
     77     blue = 31 - (value % 32);
     78     green = 63;
     79     red = 0;
     80   }
     81   if (quadrant == 2) {
     82     blue = 0;
     83     green = 63;
     84     red = value % 32;
     85   }
     86   if (quadrant == 3) {
     87     blue = 0;
     88     green = 63 - 2 * (value % 32);
     89     red = 31;
     90   }
     91   return (red << 11) + (green << 5) + blue;
     92 }
     93 
     94