#include "FastLED.h"

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


//// ----------------------------- Spider ------------------------------
//(c)stepko
#define Speed 250
#define Koef 9 //1,3,5,7,9(standart),10,12
#define lines 7
#define Color 1 //By Kostyamat https://editor.soulmatelights.com/gallery/550
#define Fader 30 //By Kostyamat https://editor.soulmatelights.com/gallery/550
#define sx ((256 / (WIDTH / 2)) - (WIDTH / 16))
#define cy ((256 / (HEIGHT / 2)) - (HEIGHT / 16))

void drawLine(int x1, int y1, int x2, int y2,
  const 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[XY(x1, y1)] += color;
    int error2 = error * 2;
    if (error2 > -deltaY) {
      error -= deltaY;
      x1 += signX;
    }
    if (error2 < deltaX) {
      error += deltaX;
      y1 += signY;
    }
  }
}

void draw() {
  fadeToBlackBy(leds, NUM_LEDS, Fader);
  double t = millis() / (256 - Speed);
  for (uint8_t c = 0; c < lines; c++) {
    byte xx = sin8(t + (100 * c) * Koef) / sx;
    byte yy = cos8(t + (150 * c) * Koef) / cy;
    if (Color)
      drawLine(xx, yy, WIDTH - xx - 1, HEIGHT - yy - 1, CHSV(c * (256 / lines), 200, 255));
    else
      drawLine(xx, yy, WIDTH - xx - 1, HEIGHT - yy - 1, CHSV(0, 0, 255));
  }
  blur2d(leds, WIDTH, HEIGHT, 32);
  delay(16);
}
void setup() {
  //Serial.begin(250000);
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  FastLED.setBrightness(BRIGHTNESS);
}

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


uint16_t XY (uint8_t x, uint8_t y) {
 if(x<0||x>= WIDTH || y<0 || y >= HEIGHT) return;
  if ((y % 2 == 0) || MATRIX_TYPE)                     // если чётная строка
  {
    return ((uint32_t)y *WIDTH + x) % (WIDTH  * HEIGHT);
  }
  else                                                      // если нечётная строка
  {
    return ((uint32_t)y  * HEIGHT + WIDTH - x - 1) % (WIDTH  * HEIGHT);
  }
}