/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: "FastLED Control"
	- Source Code NOT compiled for: ESP32 DevKit V1
	- Source Code created on: 2024-07-03 20:21:27

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* "Convert the glediator code for ESP32 so that I */
	/* can control my LED strip with the RemoteLight app */
	/* via USB connection on a PC." */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include <FastLED.h>  //https://github.com/FastLED/FastLED

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void);
void readSerialData(void);

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t Ledstrip_WS2812_DIN_PIN_D4 = 4;

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool Ledstrip_WS2812_DIN_PIN_D4_rawData = 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float Ledstrip_WS2812_DIN_PIN_D4_phyData = 0.0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
#define NUM_LEDS 60
#define DATA_PIN 4
CRGB leds[NUM_LEDS];

void setup(void) {
  // Initialize serial communication at 115200 baud rate
  Serial.begin(115200);

  // Initialize the FastLED library
  FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(50); // Set brightness to 50 out of 255
}

void loop(void) {
  // Read data from serial
  readSerialData();

  // Refresh output data
  updateOutputs();

  // Show the LED data
  FastLED.show();
}

void updateOutputs() {
  digitalWrite(Ledstrip_WS2812_DIN_PIN_D4, Ledstrip_WS2812_DIN_PIN_D4_rawData);
}

void readSerialData() {
  static uint16_t dataIndex = 0;
  static uint8_t buffer[NUM_LEDS * 3]; // Buffer to store incoming data

  while (Serial.available()) {
    buffer[dataIndex++] = Serial.read();

    // If buffer is full, update LED strip
    if (dataIndex >= sizeof(buffer)) {
      for (uint16_t i = 0; i < NUM_LEDS; i++) {
        leds[i].r = buffer[i * 3];
        leds[i].g = buffer[i * 3 + 1];
        leds[i].b = buffer[i * 3 + 2];
      }
      dataIndex = 0; // Reset index for next frame
    }
  }
}

/* END CODE */