// see TestSoulmate Waves dbg2
#include <FastLED.h>

#define BRIGHTNESS 255
#define ROWS 16
#define COLS 16
#define N_LEDS (ROWS*COLS)

CRGB leds[N_LEDS];

void setup() { 
  // put your setup code here, to run once:
  //Serial.begin(115200);
  //Serial.println("Hello, ESP32!");
  FastLED.addLeds<WS2812B, 12, GRB>(leds, N_LEDS);
  FastLED.setBrightness( BRIGHTNESS );
}

  // Your pattern code goes here.
  // This draw() function is called repeatedly and the LEDs will be
  // printed after every loop.
  //
  // Some helpful variables:
  // ROWS: number of rows
  // COLS: number of columns
  // N_LEDS: total number of LEDs (LED_COLS * LED_ROWS)
  // leds: the LED array to print to
  //
  // You can also use:
  // uint16_t gridIndexHorizontal(x, y) - the index of a given x/y coordinate
  // uint8_t beatsin8(bpm, minimum, maximum, offset) - an 8-bit sine wave
  //
  // For more information, visit https://github.com/FastLED/FastLED/wiki/Overview

#define LED_ROWS ROWS
#define LED_COLS COLS
#define SOULMATE_SERPENTINE 0
#define SOULMATE_MIRROR 0
#define SOULMATE_REVERSE 0

int16_t gridIndexHorizontal(int16_t x, int16_t y) {
  if (y > LED_ROWS) return -1;
  if (x > LED_COLS) return -1;
  if (x < 0) return -1;
  if (y < 0) return -1;

  int16_t xIndex = x;

  // Serpentine row
  bool oddRow = y % 2 != 1;
  if (SOULMATE_SERPENTINE && oddRow) {
    xIndex = LED_COLS - 1 - xIndex;
  }

  // Mirrored left-to-right
  // Don't apply if we're reversing it (reversing does the same)
  if (SOULMATE_MIRROR && !SOULMATE_REVERSE) {
    xIndex = LED_COLS - 1 - xIndex;
  }

  int16_t index = y * LED_COLS + xIndex;

  if (index > -1 && index < N_LEDS) {
    return index;
  }

  return -1;
}

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


void draw() {
  static uint8_t off = 0;

  for(uint8_t i = 0; i<COLS; i++) {
    for(uint8_t j = 0; j<ROWS; j++){
      int k = gridIndexHorizontal(i,j);
      uint8_t t = beatsin8(1*256, N_LEDS/4, N_LEDS/4*3);    // Move 1/4 to 3/4
      //leds[k] = CHSV(uint8_t(100), 255, 255);
      leds[k] = CHSV(uint8_t(t*3+i*16+j*16+off), 255, 255);
      
    }
  }
  off++;
}

void loop() {
  //FastLED.delay(400); // this speeds up the simulation
  draw();
  FastLED.show();
  FastLED.setBrightness( BRIGHTNESS );
}