//Blobs implementation for fibonacci 256 
//fastled fibonacci 256 leds  demo
//Stepko & kostyamat

//https://wokwi.com/arduino/projects/283705656027906572

#include "FastLED.h"
#define WIDTH 20
#define HEIGHT 20
// LEDs pin
#define DATA_PIN 3
// LED brightness
#define BRIGHTNESS 255
#define NUM_LEDS 257
// Define the array of leds
CRGB leds[257];

static const uint16_t FibonPlanarTable[] PROGMEM ={
  256,256,256,256,256,256,256,256,36,39,38,37,256,256,256,256,256,256,256,256,256,256,256,256,256,13,34,35,40,
  256,58,59,60,61,256,256,256,256,256,256,256,256,256,256,14,33,256,41,56,57,68,67,66,65,64,63,256,256,256,256,
  256,256,256,12,15,32,42,55,256,69,256,79,80,81,82,83,62,256,256,256,256,256,11,16,31,256,43,54,70,77,78,94,
  93,92,91,90,84,85,256,256,256,255,10,17,30,44,53,71,76,256,95,256,101,102,103,104,89,88,256,256,256,254,9,18,
  29,45,52,72,75,96,256,100,120,119,118,117,105,106,87,256,256,253,8,19,28,46,256,51,73,97,99,121,124,125,126,
  256,116,256,107,86,232,252,7,20,256,27,47,256,50,74,122,123,145,144,256,127,256,115,256,108,233,251,6,256,21,
  256,26,48,49,256,98,146,147,148,143,256,128,256,114,109,231,234,250,5,256,22,23,25,24,0,195,171,170,169,149,
  142,256,129,113,110,230,235,256,249,4,3,2,1,244,243,256,194,172,256,168,150,141,130,112,256,256,229,236,256,
  248,247,246,245,242,220,219,196,193,173,167,151,140,131,111,256,256,208,228,237,238,239,240,241,256,221,218,
  197,192,174,166,152,139,132,256,256,256,256,209,227,226,256,256,256,222,256,217,198,191,175,165,153,138,133,
  256,256,256,256,207,210,211,225,224,223,215,216,199,256,190,176,164,154,137,134,256,256,256,256,256,206,205,
  212,213,214,201,200,256,189,177,163,256,155,136,256,256,256,256,256,256,256,256,204,203,202,256,187,188,178,
  162,256,156,135,256,256,256,256,256,256,256,256,256,183,184,185,186,180,179,161,256,157,256,256,256,256,256,
  256,256,256,256,256,256,256,256,182,181,159,160,256,158,256,256,256,256,256,256,256 
};  

//Blobs
//by stepko and kostyamat
bool loadingFlag = true;
#define Speed (255)
#define Scale (110)
#define Am (64)
#define Color (0) //0- HSV 1- FromPalette
#define regime (1) //0-small 1-big 

void drawPixelXYF(float x, float y, const CRGB & color) {
  if (x < 0 || y < 0 || x > ((float) WIDTH - 1) || y > ((float) HEIGHT - 1)) return;

  // extract the fractional parts and derive their inverses
  uint8_t xx = (x - (int) x) * 255, yy = (y - (int) y) * 255, ix = 255 - xx, iy = 255 - yy;
  // calculate the intensities for each affected pixel
#define WU_WEIGHT(a, b)((uint8_t)(((a) * (b) + (a) + (b)) >> 8))
  uint8_t wu[4] = {
    WU_WEIGHT(ix, iy),
    WU_WEIGHT(xx, iy),
    WU_WEIGHT(ix, yy),
    WU_WEIGHT(xx, yy)
  };
  // multiply the intensities by the colour, and saturating-add them to the pixels
  for (uint8_t i = 0; i < 4; i++) {
    int16_t xn = x + (i & 1), yn = y + ((i >> 1) & 1);
    CRGB clr = leds[XY(xn, yn)];
      clr.r = qadd8(clr.r, (color.r * wu[i]) >> 8);
      clr.g = qadd8(clr.g, (color.g * wu[i]) >> 8);
      clr.b = qadd8(clr.b, (color.b * wu[i]) >> 8);
    leds[XY(xn, yn)] = clr;
  }
#undef WU_WEIGHT
}
float ball[WIDTH][4]; //0-PosY 1-PosX 2-SpeedY 3-SpeedX
float radius[WIDTH];
bool rrad[WIDTH];
byte color[WIDTH];

void fill_circle(float cx, float cy, float radius, CRGB col) {
  for (float y = -radius; y <= radius; y++) {
    for (float x = -radius; x <= radius; x++) {
      if (x * x + y * y <= radius * radius)
        drawPixelXYF(cx + x, cy + y, col);
    }
  }
}


byte Amount = map(Am, 1, 255, 1, WIDTH);


//Draw the scene with the background defined by RGB colors and the 5 balls in different colors
void BlobS() {
  if (loadingFlag) { 
    for (byte i = 0; i < Amount; i++) {
      if (regime)
        radius[i] = random(1, 40) / 10;
      else
        radius[i] = 1;
      ball[i][2] = (float)random(5, 11) / (float)(257U - Speed) / 4.0;
      ball[i][3] = (float)random(5, 11) / (float)(257U - Speed) / 4.0;
      ball[i][0] = random(0, WIDTH);
      ball[i][1] = random(0, HEIGHT);//random(0,WIDTH);
      color[i] = random(0, 255);
      if ( ball[i][2] == 0)
        ball[i][2] = 1;
      if ( ball[i][3] == 0)
        ball[i][3] = 1;
    }
    loadingFlag = false;
  }
  //FastLED.clear();
  fadeToBlackBy(leds, NUM_LEDS, 20);
  // Bounce three balls around
  for (byte i = 0; i < Amount; i++) {
    if (rrad[i]) {  // тут у нас шарики надуваются\сдуваются по ходу движения
      radius[i] += (fabs(ball[i][2]) > fabs(ball[i][3])? fabs(ball[i][2]) : fabs(ball[i][3])) * 0.05;
      if (radius[i] >= 4.) {
        rrad[i] = false;
      }
    } else {
      radius[i] -= (fabs(ball[i][2]) > fabs(ball[i][3])? fabs(ball[i][2]) : fabs(ball[i][3])) * 0.05;
      if (radius[i] < 1) {
        rrad[i] = true;
        color[i] = random(0, 255);
      }
    }
    if (radius[i]> 1)
      fill_circle(ball[i][1], ball[i][0], radius[i], ColorFromPalette(RainbowColors_p, color[i]));
    else 
      drawPixelXYF(ball[i][1], ball[i][0], ColorFromPalette(RainbowColors_p, color[i]));
    //----------------------
    if (ball[i][0] + radius[i] >= HEIGHT - 1)
      ball[i][0] += (ball[i][2] * ((HEIGHT - 1 - ball[i][0]) / radius[i] + 0.005));
    else if (ball[i][0] - radius[i] <= 0)
      ball[i][0] += (ball[i][2] * (ball[i][0] / radius[i] + 0.005));
    else
      ball[i][0] += ball[i][2];
    //-----------------------
    if (ball[i][1] + radius[i] >= WIDTH - 1)
      ball[i][1] += (ball[i][3] * ((WIDTH - 1 - ball[i][1]) / radius[i] + 0.005));
    else if (ball[i][1] - radius[i] <= 0)
      ball[i][1] += (ball[i][3] * (ball[i][1] / radius[i] + 0.005));
    else
      ball[i][1] += ball[i][3];
    //------------------------
    if (ball[i][0] < 0.01) {
      ball[i][2] = (float)random8(5, 11) / (257U - Speed) / 4.0;
      ball[i][0] = 0.01;
    }
    else if (ball[i][0] > HEIGHT - 1.01) {
      ball[i][2] = (float)random8(5, 11) / (257U - Speed) / 4.0;
      ball[i][2] = -ball[i][2];
      ball[i][0] = HEIGHT - 1.01;
    }
    //----------------------
    if (ball[i][1] < 0.01) {
      ball[i][3] = (float)random8(5, 11) / (257U - Speed) / 4.0;
      ball[i][1] = 0.01;
    }
    else if (ball[i][1] > WIDTH - 1.01) {
      ball[i][3] = (float)random8(5, 11) / (257U - Speed) / 4.0;
      ball[i][3] = -ball[i][3];
      ball[i][1] = WIDTH - 1.01;
    }
  }
  //blur2d(leds, WIDTH, HEIGHT, 128);
}



void setup() {
  //Serial.begin(250000);
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  FastLED.setBrightness(BRIGHTNESS);
}

void loop() {
  BlobS();
  FastLED.show();
} //loop

uint16_t XY(byte x, byte y) { 
uint16_t ledsindex = pgm_read_word (FibonPlanarTable+y*WIDTH+x);
return (ledsindex);
}
FPS: 0
Power: 0.00W