//Stepko
#include "FastLED.h"

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

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

#define C_HEIGHT LED_COLS - 1
#define C_WIDTH (LED_COLS - 1) * 8
bool setupm = 1;
#define C_X (LED_COLS / 2)
#define C_Y (LED_ROWS / 2)
uint16_t C_XY[C_WIDTH][C_HEIGHT];
void loop() {
  if (setupm) {
    setupm = 0;
      FastLED.clear();
      for (uint16_t x = 0; x < C_WIDTH; x++) {
        float angle = radians(map(x,0,C_WIDTH,0,360));
        for (uint8_t y = 0; y < C_HEIGHT; y++) {
          int8_t m_x = (((float)C_X + 0.5f) + (sin(angle) * y));
          int8_t m_y = (((float)C_Y + 0.5f) + (cos(angle) * y));
          //Serial.println(String(m_x)+','+String(m_y));
          C_XY[x][y] = (((m_x < 0 ) || (m_x > LED_COLS - 1)) || ((m_y < 0 ) || (m_y > LED_ROWS - 1))) ? NUM_LEDS : XY(m_x, m_y);
        }
      } 
      /*for (byte x = 0; x < C_WIDTH; x++) {
        String aaa;
        for (byte y = 0; y < C_HEIGHT; y++) {
          aaa += String(C_XY[x][y])+"\t";
          leds[C_XY[x][y]] += CHSV(150, 255, 100);
        }
        Serial.println(aaa);
        //FastLED.show();
      }*/
  }
  static byte scale = 32;
  static byte speed = 92;
  static uint32_t t;
  t += speed;
  for (byte x = 0; x < C_WIDTH; x++) {
    for (byte y = 0; y < C_HEIGHT; y++) {
      int16_t Bri = inoise8(x * scale, (y * scale) - t) - (y * (255 / LED_ROWS));
      byte Col = Bri;// inoise8(x * scale, (y * scale) - t) - (y * (255 / LED_ROWS));
      if (Bri < 0) Bri = 0; if (Bri != 0) Bri = 256 - (Bri * 0.2);
      nblend(leds[C_XY[x][y]], ColorFromPalette(HeatColors_p, Col, Bri), speed);
    }
  }
  FastLED.show();
}


uint16_t XY (uint8_t x, uint8_t y) {
  return (y * LED_COLS + x);
}