/*

PAB49162 Wokwi projets
https://wokwi.com/makers/pab49162

WLED multi-strip support
https://kno.wled.ge/features/multi-strip/

Multiple controller examples
https://github.com/FastLED/FastLED/wiki/Multiple-Controller-Examples

Strip effects for NeoPixel and FastLED
https://www.tweaking4all.com/hardware/arduino/adruino-led-strip-effects/

RGB color code chart
https://www.rapidtables.com/web/color/RGB_Color.html

*/

#include <Adafruit_NeoPixel.h>
#define PINA 22
#define PINB 23
#define NUM_LEDS 4
#define SPEED_DELAY 10

// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

Adafruit_NeoPixel stripA = Adafruit_NeoPixel(NUM_LEDS, PINA, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel stripB = Adafruit_NeoPixel(NUM_LEDS, PINB, NEO_GRB + NEO_KHZ800);

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

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

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

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

void loop() {
  
  rgbColor stripColor;

  stripColor = StripA_DetermineColor();
  StripA_SetStriptColor(stripColor);

  stripColor = StripB_DetermineColor();
  StripB_SetStriptColor(stripColor);

  StripA_Show();
  StripB_Show();
  
  delay(SPEED_DELAY);

  /*
  Serial.print("stripAFadeCount = "); Serial.println(stripAFadeCount, DEC);
  Serial.print("red = "); Serial.println(red, DEC);
  Serial.print("r ="); Serial.println(r, HEX);
  Serial.print("g = "); Serial.println(g, HEX);
  Serial.print("b = "); Serial.println(b, HEX);
  Serial.println("");
  */
}

// 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 = 0xff; green =0xff; blue = 0xff;  // white
  		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+1) % 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();
}

// *** Strip B Functions ***

rgbColor StripB_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 =0x5a; blue = 0x00;  // burnt orange
  		break;
		case 1:
			red = 0xff; green =0xff; blue = 0xff;  // white
  		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+1) % 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) % 2; }

  return returnColor;
}

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

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

void StripB_Show() {
  // NeoPixel
  stripB.show();
}