#define FASTLED_ALLOW_INTERRUPTS 0
#include <FastLED.h>
#include "palettes.h"

#define MATRIX  16 //could be 16, 24, 32, 40 and 48
#define DATA_PIN            3
#define NUM_LEDS            ((MATRIX) * (MATRIX))
#define LED_TYPE            WS2811
#define COLOR_ORDER         GRB
int BRIGHTNESS = 180;
CRGB leds[NUM_LEDS];
CRGBPalette16 currentPalette;

#define LED_ROWS 16
#define LED_COLS 16
bool setupm = 1;
#define C_X LED_COLS / 2
#define C_Y LED_ROWS / 2
byte XY_angle[LED_COLS][LED_ROWS];
byte XY_radius[LED_COLS][LED_ROWS];


#include "functions.h"
void setup()
{
	FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setDither(BRIGHTNESS < 255); // cpt-city palettes have different color balance
	FastLED.setBrightness(BRIGHTNESS);
	currentPalette = RainbowColors_p;
	randomSeed(analogRead(A0));
}
void loop()
{
	#if 1
	draw();
	FastLED.show();

	#else
	int random_palette = random(gGradientPaletteCount);
	currentPalette = gGradientPalettes[random_palette];
	int function = random(9);
	switch (function) {
	case 0: pixels_eighth_no_symmetry(); break;
	case 1: pixels_eighth_horizontal_symmetry(); break;
	case 2: pixels_eighth_vertical_symmetry(); break;
	case 3: pixels_eighth_both_symmetry(); break;
	case 4: pixels_quarter_no_symmetry(); break;
	case 5: pixels_quarter_horizontal_symmetry(); break;
	case 6: pixels_quarter_vertical_symmetry(); break;
	case 7: pixels_quarter_both_symmetry(); break;
	case 8: pixels_half_both_symmetry(); break;
	}
	FastLED.show();
	delay(2000);
	fill_solid( leds, NUM_LEDS, CRGB(0,0,0));
	#endif
}


void draw() {
  FastLED.clear();
  if (setupm) {
    setupm = 0;
    for (int8_t x = -C_X; x < C_X + (LED_COLS % 2); x++) {
      for (int8_t y = -C_Y; y < C_Y + (LED_ROWS % 2); y++) {
        // XY_angle[x + C_X][y + C_Y] = atan2(y, x) * (180. / 2. / PI) * LED_COLS;
        XY_angle[x + C_X][y + C_Y] = (atan2(x, y) / PI) * 128 + 127; //thanks ldirko 
        XY_radius[x + C_X][y + C_Y] = hypot(x, y); //thanks Sutaburosu
      }
    }
  }
  
  static byte speed = 1;
  static uint32_t t;
  t += speed;
  for (uint8_t x = 0; x < LED_COLS; x++) {
    for (uint8_t y = 0; y < LED_ROWS; y++) {
      byte angle = XY_angle[x][y];
      byte radius = XY_radius[x][y];
      leds[XY(x, y)] = CHSV(t + radius * (255 / LED_COLS), 255, sin8(sin8(t+angle*5+( radius * (255 / LED_COLS)))+t * 4 + sin8(t * 4 - radius * (255 / LED_COLS)) + angle * 5));
    }
  }
  delay(16);
}