#include <IRremote.hpp>
#include <FastLED.h>

//设定三个按键的红外信号值 Set the infrared signal value of the three keys
#define NUM1 0xF30CFF00        //number 1
#define NUM2 0xE718FF00        //number 2
#define NUM3 0xA15EFF00        //number 3
#define IR_RECEIVE_PIN A2      //定义红外接收器的引脚 Define the pins of the infrared receiver

#define LED_PIN 7              //定义WS2812B RGB灯引脚  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];           //定义一个长度为 NUM_LEDS 的 CRGB 类型数组，用于控制 WS2812B LED 条  A CRGB type array of length NUM_LEDS is defined to control WS2812B LED strips
static uint8_t hue = 0;        //设置颜色的值  Sets the value of the color
int Color = 1;                 //定义灯光颜色模式的变量  A variable that defines the color pattern of the light

void setup() {
  Serial.begin(9600);
  IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);   //启用红外接收功能  Enable infrared receiver function
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);  //使用FastLED库将LED条上的LED添加到控制器中  The LED on the LED strip is added to the controller using the FastLED library
  FastLED.setBrightness(BRIGHTNESS);                       //设置灯光亮度  Set the light brightness
  pinMode(LED_PIN, OUTPUT);                                //定义7引脚为输出模式  Pin 7 is defined as the output mode
  digitalWrite(LED_PIN, LOW);                              //设定7引脚为低电平输出  Set pin 7 for low output
}

void loop() {
  if (IrReceiver.decode()) {
    // Serial.println(IrReceiver.decodedIRData.decodedRawData, HEX);  //打印“旧的”原始数据  Print the "old" raw data
    // IrReceiver.printIRResultShort(&Serial);                        //在一行中打印完整的接收数据  Print the complete received data on one line
    // IrReceiver.printIRSendUsage(&Serial);                          //打印发送此数据所需的语句  Print the statements needed to send this data
    if (IrReceiver.decodedIRData.decodedRawData == NUM1) {
      Color = 1;  //如果按下按键1，颜色模式为红 If key 1 is pressed, the color mode is red
    } else if (IrReceiver.decodedIRData.decodedRawData == NUM2) {
      Color = 2;  //如果按下按键2，颜色模式为绿 If key 1 is pressed, the color mode is green
    } else if (IrReceiver.decodedIRData.decodedRawData == NUM3) {
      Color = 3;  //如果按下按键3，颜色模式为蓝 If key 1 is pressed, the color mode is blue
    }
    IrReceiver.resume();  // 等下接收下一个值 Wait to receive the next value
  }

  for (int i = 0; i < NUM_LEDS; i++) {
    // 根据颜色值设置LED的颜色
    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();// 显示LED的颜色 Displays the color of the LED
    FastLED.delay(2 / FRAMES_PER_SECOND);// 延迟以控制帧率 Delay to control the frame rate
  }
}
