/* esp32 by espressif system 라이브러리 필요 v 2.0.17 빌드 가능
  Irecv 이슈
*/

#include <IRremoteESP8266.h>
#include <IRrecv.h>
#include <IRutils.h>
#include <FastLED.h>

#define LED_PIN     6           // Define the WS2812B RGB lamp pin
#define NUM_LEDS    12          // Define the number of beads
#define BRIGHTNESS  5           // Defining the brightness of the light
#define FRAMES_PER_SECOND  120  // Defines frames per second
CRGB leds[NUM_LEDS];            // WS2812B LED 
static uint8_t hue = 0;         // Sets the value of the color
int Color = 1;                  // A variable that defines the color pattern of the light

//const int IR_RECEIVE_PIN = 8;   // Infrared receiver connected to GPI8 pin
const int IR_RECEIVE_PIN = 20;
IRrecv irReceiver(IR_RECEIVE_PIN);
decode_results results;

void setup() {
  Serial.begin(9600);
  irReceiver.enableIRIn();                                  // Enable infrared receiver function
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);   // The LED on the LED strip is added to the controller using the FastLED library
  FastLED.setBrightness(BRIGHTNESS);                        // Set the light brightness

}

void loop() {
  if (irReceiver.decode(&results)) {        // If you receive an infrared signal
    Serial.println(results.value, HEX);  // Print the numerical value of the infrared signal
    
    if (results.value == 0xFF30CF) {
      Color = 1; // If key 1 is pressed, the color mode is red
    }else if(results.value == 0xFF18E7){
      Color = 2; // If key 1 is pressed, the color mode is green
    }else if(results.value == 0xFF7A85){
      Color = 3; // If key 1 is pressed, the color mode is blue
    }
    irReceiver.resume(); // Continue to receive the next signal
  }

  for (int i = 0; i < NUM_LEDS; i++) {
    if (Color == 1) {
      leds[i] = CHSV(hue, 255, 255);        //set it to red
    } else if (Color == 2) {
      leds[i] = CHSV(hue + 85, 255, 255);   // set it to green
    } else if (Color == 3) {
      leds[i] = CHSV(hue + 170, 255, 255);  // set it to blue
    }
    FastLED.show(); // Displays the color of the LED
    FastLED.delay(2 / FRAMES_PER_SECOND); // Delay to control the frame rate
  }
}
