#include <Arduino.h>
// Enable support for the NEC infrared protocol before including the IRremote library.
#define DECODE_NEC
// IRremote.hpp supplies the receiver object and decode functions.
#include <IRremote.hpp>

const uint8_t RECEIVER_PIN = 11;
// Keep the last full button code so repeat messages can reuse it.
uint32_t previousCode = 0;
bool havePrevious = false;

// This function receives a 32-bit infrared code and prints a familiar button name.
void printButton(uint32_t code) {
  // switch compares code with each case; break exits the chosen case.
  switch (code) {
    case 0xBA45FF00: Serial.println("POWER"); break;
    case 0xB847FF00: Serial.println("FUNC/STOP"); break;
    case 0xB946FF00: Serial.println("VOL+"); break;
    case 0xBB44FF00: Serial.println("FAST BACK"); break;
    case 0xBF40FF00: Serial.println("PAUSE"); break;
    case 0xBC43FF00: Serial.println("FAST FORWARD"); break;
    case 0xF807FF00: Serial.println("DOWN"); break;
    case 0xEA15FF00: Serial.println("VOL-"); break;
    case 0xF609FF00: Serial.println("UP"); break;
    case 0xE619FF00: Serial.println("EQ"); break;
    case 0xF20DFF00: Serial.println("ST/REPT"); break;
    case 0xE916FF00: Serial.println("0"); break;
    case 0xF30CFF00: Serial.println("1"); break;
    case 0xE718FF00: Serial.println("2"); break;
    case 0xA15EFF00: Serial.println("3"); break;
    case 0xF708FF00: Serial.println("4"); break;
    case 0xE31CFF00: Serial.println("5"); break;
    case 0xA55AFF00: Serial.println("6"); break;
    case 0xBD42FF00: Serial.println("7"); break;
    case 0xAD52FF00: Serial.println("8"); break;
    case 0xB54AFF00: Serial.println("9"); break;
    // Handle any button code that is not listed above.
    default: Serial.println("other button"); break;
  }
}

// Start Serial and initialize the IR receiver on the selected pin.
void setup() {
  Serial.begin(9600);
  Serial.println("IR Receiver Button Decode");
  // Start receiving pulses, without blinking a feedback LED.
  IrReceiver.begin(RECEIVER_PIN, DISABLE_LED_FEEDBACK);
}

// Process a command only when the receiver has completed a message.
void loop() {
  // ! means NOT; return leaves this loop() call early when there is no message.
  if (!IrReceiver.decode()) return;
  uint32_t code = IrReceiver.decodedIRData.decodedRawData;
  // & tests a bit flag to see whether the remote sent a held-button repeat.
  const bool repeat = (IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT) != 0;
  // Tell the receiver it may begin collecting the next message.
  IrReceiver.resume();
  // A repeat event reuses the last known key, if there is one.
  if (repeat) {
    // Ignore a repeat when we have not seen any complete key yet.
    if (!havePrevious) return;
    code = previousCode;
    Serial.println("REPEAT");
  } else {
    // Save the latest complete button code for future repeats.
    previousCode = code;
    havePrevious = true;
    Serial.print("IR code:0x");
    Serial.println(code, HEX);
  }
  // Translate the final code into a readable button label.
  printButton(code);
  delay(500);
}
