//
// ESP32 NeoPixel Max LED Count - this sketch can be used to determine
// the max number of LEDs that can be controlled uisng the new NeoPixel
// library using the WOKWI simulation.
//
// Works with 1151 LEDs, Fails with 1152 LEDs
 
#include <Adafruit_NeoMatrix.h>

#define PIN 2
#define NUM_LEDS 1151
#define SPEED_DELAY 100


// Four matrix matrix
Adafruit_NeoPixel stripA = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

// Define rgb colors data type
struct rgbColor
{
  byte r;
  byte g;
  byte b;
};


//
// Setup function
//

void setup() {
  Serial.begin(15200);
  Serial.println("setup started.");

  stripA.begin();
  stripA.show(); // Initialize all pixels to 'off'

  // buildLogo();
  Serial.println("setup complete.");
}

void loop() {
  
  rgbColor stripColor;

  stripColor = StripA_DetermineColor();
  StripA_SetStriptColor(stripColor);
  StripA_Show();
  
  delay(SPEED_DELAY);

  //Serial.print("r = "); Serial.print(stripColor.r, HEX);
  //Serial.print("  g= "); Serial.print(stripColor.g, HEX);
  //Serial.print("  b = "); Serial.println(stripColor.b, HEX);
  
}

// Functions

// *** Strip A Functions ***

rgbColor StripA_DetermineColor() { 
  static int colorCount = 0;
  static int brightnessCount = 0;

  int red, green, blue;
  int brightnessValue;
  rgbColor returnColor;

	// Determine display color
	switch (colorCount) {
		case 0:
			red = 0xff; green = 0x00; blue = 0x00;  // red
  		break;
		case 1:
			red = 0x00; green = 0xff; blue = 0x00;  // green
  		break;
		case 2:
			red = 0x00; green = 0x00; blue = 0xff;  // blue
  		break;
    default:
			colorCount = 0;
      break;
	 }

	// Determine brightness value
  if ((brightnessCount >= 0) && (brightnessCount <= 255)) {
	    brightnessValue = brightnessCount;
  }
  if ((brightnessCount > 255) && (brightnessCount <= 511)) {
  		brightnessValue = 511 - brightnessCount;
  }
	brightnessCount = (brightnessCount+64) % 512; //1X speed

  // Determine rbg values to display desired color and intensity
  returnColor.r = (byte)((brightnessValue/256.0)*red);
  returnColor.g = (byte)((brightnessValue/256.0)*green);
  returnColor.b = (byte)((brightnessValue/256.0)*blue);
  
  // Update color count
	if (brightnessCount == 0) {	colorCount = (colorCount+1) % 3; }

  return returnColor;
}

void StripA_SetStriptColor(rgbColor stripColor) {
  for(int i = 0; i < NUM_LEDS; i++ ) {
    StripA_SetPixel(i, stripColor.r, stripColor.g, stripColor.b);
  }
}

void StripA_SetPixel(int Pixel, byte red, byte green, byte blue) {
  // NeoPixel
  stripA.setPixelColor(Pixel, stripA.Color(red, green, blue));
}

void StripA_Show() {
  // NeoPixel
  stripA.show();
}
X Coordinate -->
Y Coordinate -->