
#define IRButton_1 1
#define IRButton_2 2
#define IRButton_3 3
#define IRButton_4 4
#define IRButton_5 5
#define IRButton_6 6
#define IRButton_7 7
#define IRButton_8 8
#define IRButton_9 9
#define IRButton_STAR 10 //*
#define IRButton_0 11 
#define IRButton_SHARP 12 //#
#define IRButton_UP 13
#define IRButton_LEFT 14 
#define IRButton_OK 15 
#define IRButton_RIGHT 16 
#define IRButton_DOWN 17 

#define IRButton_1_COMMAND 0x45
#define IRButton_2_COMMAND 0x46
#define IRButton_3_COMMAND 0x47
#define IRButton_4_COMMAND 0x44
#define IRButton_5_COMMAND 0x40
#define IRButton_6_COMMAND 0x43
#define IRButton_7_COMMAND 0x7
#define IRButton_8_COMMAND 0x15
#define IRButton_9_COMMAND 0x9
#define IRButton_STAR_COMMAND 0x16 
#define IRButton_0_COMMAND 0x19 
#define IRButton_SHARP_COMMAND 0xD
#define IRButton_UP_COMMAND 0x18 
#define IRButton_LEFT_COMMAND 0x8  
#define IRButton_OK_COMMAND 0x1C 
#define IRButton_RIGHT_COMMAND 0x5A 
#define IRButton_DOWN_COMMAND 0x52 

typedef struct  {
  uint8_t code;
  uint16_t command;
  bool isPressed;
} IRButton;

IRButton irbuttonArray[] = {
  {IRButton_1,     IRButton_1_COMMAND,     false},
  {IRButton_2,     IRButton_2_COMMAND,     false},
  {IRButton_3,     IRButton_3_COMMAND,     false},
  {IRButton_4,     IRButton_4_COMMAND,     false},
  {IRButton_5,     IRButton_5_COMMAND,     false},
  {IRButton_6,     IRButton_6_COMMAND,     false},
  {IRButton_7,     IRButton_7_COMMAND,     false},
  {IRButton_8,     IRButton_8_COMMAND,     false},
  {IRButton_9,     IRButton_9_COMMAND,     false},
  {IRButton_STAR,  IRButton_STAR_COMMAND,  false},
  {IRButton_0,     IRButton_0_COMMAND,     false},
  {IRButton_SHARP, IRButton_SHARP_COMMAND, false},

  {IRButton_UP,    IRButton_UP_COMMAND,    false},
  {IRButton_LEFT,  IRButton_LEFT_COMMAND,  false},
  {IRButton_OK,    IRButton_OK_COMMAND,    false},
  {IRButton_RIGHT, IRButton_RIGHT_COMMAND, false},
  {IRButton_DOWN,  IRButton_DOWN_COMMAND,  false},  
};

void IR_Init(){
  IrReceiver.begin(10, ENABLE_LED_FEEDBACK);
}

void IR_Check(){
  if (IrReceiver.decode()) {
      if (IrReceiver.decodedIRData.protocol == UNKNOWN) {
          //Receive noise
          IrReceiver.resume(); 
          return;
      }
      if(IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT){
        //Long press, do nothing
        IrReceiver.resume(); 
        return;
      }

      for (uint8_t i=0;i<17;i++){
        if(irbuttonArray[i].command == IrReceiver.decodedIRData.command){
          irbuttonArray[i].isPressed = true;
          break;
        }
      }

      IrReceiver.resume();
  }
}

int8_t IR_isDigitPressed(){
  for(uint8_t i=1;i<10;i++){
    if(IR_isButtonPressed(i)){
      return i;
    }
  }
  if(IR_isButtonPressed(IRButton_0)){
    return 0;
  }

  return -1;
}

bool IR_isButtonPressed(uint8_t code){
  for (uint8_t i=0;i<17;i++){
    if(irbuttonArray[i].code == code){
      if(irbuttonArray[i].isPressed){
        irbuttonArray[i].isPressed = false;
        return true;
      } else {
        return false;
      }
    }
  }
  return false;
}