//Fastled circular 241 rgb led matrix demo
//Yaroslaw Turbin, 22.01.2020 
//https://vk.com/ldirko
//https://www.reddit.com/user/ldirko/

#include "FastLED.h"

// Matrix size
#define WIDTH 17 
#define HEIGHT 17
// LEDs pin
#define DATA_PIN 3
// LED brightness
#define BRIGHTNESS 255
#define NUM_LEDS HEIGHT * WIDTH
// Define the array of leds
CRGB leds[NUM_LEDS+1];


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

void loop() {
  for(byte i=8;i--;)leds[XY(beatsin8(12+i,0,WIDTH-1),beatsin8(15-i,0,HEIGHT-1))]=CHSV(beatsin8(12,0,255),255,255);
  blur2d(leds,WIDTH,HEIGHT,16);
  FastLED.show();
} //loop


void DrawLine(int x1, int y1, int x2, int y2, CRGB color)
{
  int deltaX = abs(x2 - x1);
  int deltaY = abs(y2 - y1);
  int signX = x1 < x2 ? 1 : -1;
  int signY = y1 < y2 ? 1 : -1;
  int error = deltaX - deltaY;

  leds[XY(x2, y2)] = color;
  while (x1 != x2 || y1 != y2) {
     leds[(x1, y1)] = color;
      int error2 = error * 2;
      if (error2 > -deltaY) {
          error -= deltaY;
          x1 += signX;
      }
      if (error2 < deltaX) {
          error += deltaX;
          y1 += signY;
      }
  }
}

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

// uint16_t XY(uint8_t x, uint8_t y) {
//   if (x >= NUM_COLS) return NUM_LEDS;
//   if (y >= NUM_ROWS) return NUM_LEDS;
//   // if (y & 1)
//   //   return (y + 1) * WIDTH - 1 - x;
//   // else
//     return y * NUM_COLS + x;
// }