// IR receiver test: TSOP38238 on GP21. Prints each received code to serial
// (115200 baud). The onboard LED flickers whenever IR is being received.
// Unknown protocols (e.g. air-con remotes) are also dumped as raw timings.

#define RAW_BUFFER_LENGTH 750  // room for long air-con frames
#include <IRremote.hpp>

const uint8_t IR_RECEIVE_PIN = 21;
const uint8_t IR_SEND_PIN = 22;

void setup() {
  pinMode(IR_SEND_PIN, OUTPUT);  // keep the transmitter LED off
  digitalWrite(IR_SEND_PIN, LOW);

  Serial.begin(115200);
  while (!Serial && millis() < 3000) {}

  IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
  Serial.println("IR receive test on GP21 - point a remote at the receiver and press buttons");
}

void loop() {
  if (!IrReceiver.decode()) return;

  Serial.println();
  IrReceiver.printIRResultShort(&Serial);

  if (IrReceiver.decodedIRData.flags & IRDATA_FLAGS_WAS_OVERFLOW) {
    Serial.println("Overflow: frame longer than RAW_BUFFER_LENGTH");
  } else if (IrReceiver.decodedIRData.protocol == UNKNOWN) {
    Serial.println("Unknown protocol, raw timings (us):");
    IrReceiver.printIRResultRawFormatted(&Serial, true);
  } else {
    IrReceiver.printIRSendUsage(&Serial);
  }

  IrReceiver.resume();
}