//
// ESP32 NeoPixel 32x32 Raidbow Demo - this demo uses four 8x32 panels
// to create a single 32x32 maxtrix.
//

#include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h>
#include <Adafruit_NeoPixel.h>

#define PIN 2
int Delay = 100;

// Four matrix matrix
Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(32, 8, 1, 4, PIN,
  NEO_MATRIX_TOP   + NEO_MATRIX_LEFT + NEO_MATRIX_ROWS + NEO_MATRIX_PROGRESSIVE +
  NEO_TILE_TOP     + NEO_TILE_LEFT   + NEO_TILE_ROWS   + NEO_TILE_PROGRESSIVE,
  NEO_GRB          + NEO_KHZ800);

//
// Setup function
//

void setup() {
  matrix.begin();
  matrix.setBrightness(255);

  buildRainbow();
}

//
// Loop function
//

//
// Define rainbow colors
//
uint16_t rainbowColors[1024]; 


//
// Loop function
//

void loop() {

  for (int loop=0; loop<=31; loop++) {
    for (int y=0; y<=31; y++) {
      for (int x=0; x<=31; x++) {
        int colorIndex = (((y*32) + x) + (loop*32)) % 1024;
        matrix.drawPixel(x,y,rainbowColors[colorIndex]); 
      } 
    }
    matrix.show();
    delay(Delay);
  }
}


void buildRainbow() {

  int index=0;
  int colorR = 255;
  int colorG = 0;
  int colorB = 0;

  // Red adding Green ...

  for (int i=0; i<=171; i++) {
    colorG = min(int(1.492*i),255);
    rainbowColors[index] = matrix.Color(colorR, colorG, colorB);
    index++;
  }

  // Green removing Red ...

  for (int i=0; i<=171; i++) {
    colorR = max(int(255-(1.492*i)),0);
    rainbowColors[index] = matrix.Color(colorR, colorG, colorB);
    index++;
  }

  // Green adding Blue ...

  for (int i=0; i<=170; i++) {
    colorB =  min(int(1.5*i),255);
    rainbowColors[index] = matrix.Color(colorR, colorG, colorB);
    index++;
  }

  // Blue removing Green ...

  for (int i=0; i<=171; i++) {
    colorG = max(int(255-(1.492*i)),0);
    rainbowColors[index] = matrix.Color(colorR, colorG, colorB);
    index++;
  }

  // Blue Adding Red ...

  for (int i=0; i<=171; i++) {
    colorR = min(int(1.492*i),255);
    rainbowColors[index] = matrix.Color(colorR, colorG, colorB);
    index++;
  }

  // Red Subtracting Blue ...

  for (int i=0; i<=170; i++) {
    colorB = max(int(255-(1.5*i)),0);
    rainbowColors[index] = matrix.Color(colorR, colorG, colorB);
    index++;
  }
}
X Coordinate -->
Y Coordinate -->