//F_lying fibonacci 256
//Fastled fibonacci 256 RGB leds demo
//Yaroslaw Turbin, 28.01.2020 
//https://vk.com/ldirko
//https://www.reddit.com/user/ldirko/

#include "FastLED.h"

// Matrix size
#define NUM_ROWS 20 
#define NUM_COLS 20
// LEDs pin
#define DATA_PIN 3
// LED brightness
#define BRIGHTNESS 255

// Define the array of leds
CRGB leds[257];


void setup() {
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, 256);
  FastLED.setBrightness(BRIGHTNESS);
}

void loop() {
static byte hue =0;
EVERY_N_MILLISECONDS(30) { hue++; }  //30 - speed of hue change

byte x1 = beatsin8 (18, 0, (NUM_COLS-1));
byte x2 = beatsin8 (23, 0, (NUM_COLS-1)); 
byte x3 = beatsin8 (27, 0, (NUM_COLS-1)); 
byte x4 = beatsin8 (31, 0, (NUM_COLS-1)); 
byte x5 = beatsin8 (33, 0, (NUM_COLS-1)); 

byte y1 = beatsin8 (20, 0, (NUM_ROWS-1)); 
byte y2 = beatsin8 (26, 0, (NUM_ROWS-1));
byte y3 = beatsin8 (15, 0, (NUM_ROWS-1));
byte y4 = beatsin8 (27, 0, (NUM_ROWS-1));
byte y5 = beatsin8 (30, 0, (NUM_ROWS-1));

CRGB color = CHSV (hue,255,BRIGHTNESS);

fadeToBlackBy (leds,256, 40 );

mydrawLine(x1, y1,  x2, y2, color,1);
mydrawLine(x2, y2,  x3, y3, color,1);
mydrawLine(x2, y2,  x4, y4, color,1);
mydrawLine(x3, y3,  x4, y4, color,1);
mydrawLine(x3, y3,  x1, y1, color,1);
mydrawLine(x4, y4,  x1, y1, color,1);

FastLED.show();

} //loop

void mydrawLine (byte x, byte y, byte x1, byte y1, CRGB color, bool dot){   // my ugly line draw function )))

byte xsteps = abs8(x-x1)+1;  
byte ysteps = abs8(y-y1)+1;
byte steps =  xsteps >= ysteps? xsteps:ysteps;

for (byte i = 1; i <= steps; i++) {
byte dx = lerp8by8 (x, x1, i*255/steps);
byte dy = lerp8by8 (y, y1, i*255/steps);
leds[XY(dx, dy)] += color;    // change to += for brightness look
}

if (dot) {     //add white point at the ends of line 
    leds[XY(x,y)] += CRGB ::White;
    leds[XY(x1,y1)] += CRGB ::White;
         }
}


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