// Lighing Up the Three (3) Shelves in the back of the Hiker Trailer
// with FastLED Parallel Strip Support on ESP32
//
// Uses FastLED's RMT support on the ESP32 to drive 3 channels in parallel
//

#include <FastLED.h>                              // FastLED for the LED panels

#define B_POT 4
#define G_POT 2
#define R_POT 15
#define B_PIN 14
#define G_PIN 12
#define R_PIN 13

#define LED_PIN0  5                              // Got these to work with ESP-WROOM-32
#define LED_PIN1  18
#define LED_PIN2  19



#define STACK_SIZE      4096				      // Stack size for each new thread
#define ARRAYSIZE(n) (sizeof(n)/sizeof(n[0]))     // Count of elements in array, as opposed to bytecount

#define STRIP_SIZE 12                          // Just over 1000 LEDs per segment x 8 segments = 8064 LEDs
#define NUM_STRIPS 3
#define NUM_LEDS_PER_STRIP 12

CRGB g_rgbData0[STRIP_SIZE];                      // Frame buffers (color bytes) for the 8 LED strip segments
CRGB g_rgbData1[STRIP_SIZE];
CRGB g_rgbData2[STRIP_SIZE];

// Variable for storing the potentiometer value
uint8_t potRValue = 0;
uint8_t potGValue = 0;
uint8_t potBValue = 0;

uint8_t readPot(int pin) {
  return map(analogRead(pin), 0, 1023, 0, 255);
}

// setup
//
// Invoked once at boot, does initial chip setup and application initial

void setup() 
{
    FastLED.clear();
    delay(1000); 

    Serial.begin(115200);
    delay(10);

    pinMode(R_POT, INPUT);
    pinMode(G_POT, INPUT);
    pinMode(B_POT, INPUT);

    pinMode(R_PIN, OUTPUT);
    pinMode(G_PIN, OUTPUT);
    pinMode(B_PIN, OUTPUT);

    // Add all 3 segmetns to FastLED
    FastLED.addLeds<NEOPIXEL, LED_PIN0>(g_rgbData0, STRIP_SIZE);
    FastLED.addLeds<NEOPIXEL, LED_PIN1>(g_rgbData1, STRIP_SIZE);
    FastLED.addLeds<NEOPIXEL, LED_PIN2>(g_rgbData2, STRIP_SIZE);

    FastLED.clear();
    delay(1000); 

    // Check yourself before you wreck yourself, make sure your power supply doesn't burn down your shop
    FastLED.setBrightness(64);

}


void loop()
{
    // Simple walking rainbow offset on the various strips

    static int hue = 0;
    hue+=8;

    fill_rainbow(g_rgbData0, STRIP_SIZE, hue, 5);
    fill_rainbow(g_rgbData1, STRIP_SIZE, hue+32, 5);
    fill_rainbow(g_rgbData2, STRIP_SIZE, hue+64, 5);
    
    unsigned static long lastTime = 0;
    FastLED.show();
    delay(5); 
    
    potRValue = readPot(R_POT);
    potGValue = readPot(G_POT);
    potBValue = readPot(B_POT);

    analogWrite(R_PIN, potRValue);
    analogWrite(G_PIN, potGValue);
    analogWrite(B_PIN, potBValue);

    Serial.printf("RGB(%d, %d, %d)\n", potRValue, potGValue, potBValue);
    delay(500);
    
    FastLED.clear();
    delay(500); 
}