
/*
 *  
Toshbia TV codes: (THE REMOTE AUTOMATICCALLY ONESHOTS FOR YOU)  
 chan +  D827
 vol+   58A7
6 609f

 menu/enter   1FE
 EXIT   1AE5
 "100"    (for context menu)   50AF
 up  41BE
 down   C13E
 left     B847
 right  9867
 chan -   F807
 vol -  7887
 PLAY   8A75
 PAUSE  32CD
 STOP   F20D
 REWIND\  8877
 FAST FORWRD  CA35

 *  
 SAMSUNG TV CODE (careful, it sends continously rapid fire)*  
 *  
  
 vol+   E01F
 chan +   48B7
6 50AF

ENTER 16E9
 EXIT   (for escape/backspace)  B44B   
//"RED A" context menu   0x36c9 (or  Return is  1AE5, same as Exit on Toshiba_)
 up 6F9
 down   8679
 left     A659
 right  46B9
 chan -   8F7
 vol -  D02F
 PLAY   E21D
 PAUSE  52AD
 STOP   629D
 REWIND\  A25D
 FAST FORWRD  12ED

 */
#include "IRLibAll.h"
#include <Keyboard.h>
unsigned int received_command;
unsigned int previous_command;
unsigned long key_repeat_timer;
unsigned int receive_buffer[4];
unsigned long time_value;


IRrecvPCI myReceiver(2);  //use pin 2 for IR input
 //Create a decoder object 
IRdecode myDecoder;   

//******************************************************************************************
void setup() {
  //  setup code here, to run once:
  
    pinMode(2, INPUT_PULLUP); //IR input
  
     //Serial.begin(9600);   //DEBUGGING
   
    digitalWrite(5, HIGH);        
     delay(3000); // anti-stupid (so don't get locked out)     
     Keyboard.begin();          //rem for debug? 
    
     
    myReceiver.enableIRIn(); // Start the receiver
    
    //Serial.println(F("Ready to receive IR signals")); //DEBUGGING !!!!
}
// *********************************************************************************************
  void act_on_input()
  {
     //Serial.println(received_command,HEX); //DEBUGGING!!!!!!!!!!!
     receive_buffer[2]=receive_buffer[1];     //store in buffer to look for 666
     receive_buffer[1]=receive_buffer[0];
     receive_buffer[0]=received_command;
                

  
       //toshiba codes
    if  (received_command==0x1FE)  Keyboard.write(0xb2); //enter
    if  (received_command==0x1AE5) Keyboard.write(0xb0); //backspace    MUST REM THIS FOR SAMSUNG download!!!
    if  (received_command==0x50AF) Keyboard.write(0x63); //"c" context menu         
    if  (received_command==0x41BE) Keyboard.write(0xda); //up
    if  (received_command==0xC13E) Keyboard.write(0xd9); //down
    if  (received_command==0xB847) Keyboard.write(0xd8); //left
    if  (received_command==0x9867) Keyboard.write(0xd7); //right
    if  (received_command==0x8A75) Keyboard.write(0x70); //p play
    if  (received_command==0x32CD) Keyboard.write(0x20); //space pause
    if  (received_command==0xF20D) Keyboard.write(0x78); //x stop
    if  (received_command==0x8877) Keyboard.write(0x72); //r rewind
    if  (received_command==0xCA35) Keyboard.write(0x66); //f fast frwrd

        //samsung codes
    if  (received_command==0x16E9) Keyboard.write(0xb0); //enter
    if  (received_command==0xB44B) Keyboard.write(0xb2); //EXIT backspace    
   
   //if  (received_command==0x1ae5) Keyboard.write(0x63); //Return         MUST REM THIS FOR TOSHIBA download!!! (because it would trigge rthis from backspace
    if  (received_command==0x6F9)  Keyboard.write(0xda); //up
    if  (received_command==0x8679) Keyboard.write(0xd9); //down
    if  (received_command==0xA659) Keyboard.write(0xd8); //left
    if  (received_command==0x46B9) Keyboard.write(0xd7); //right
    if  (received_command==0xE21D) Keyboard.write(0x70); //p play
    if  (received_command==0x52AD) Keyboard.write(0x20); //space pause
    if  (received_command==0x629D) Keyboard.write(0x78); //x stop
    if  (received_command==0xA25D) Keyboard.write(0x72); //r rewind
    if  (received_command==0x12ED) Keyboard.write(0x66); //f fast frwrd
  
} 
  //********************************************  end of ACT_ON_INPUT

void loop() {
  //  main code here, to run repeatedly:

  if (myReceiver.getResults()) {
    myDecoder.decode();           //Decode it
   
        //myDecoder.dumpResults(true);  //Now print results. Use false for less detail
    received_command=myDecoder.value&=0xffff;        //only use the least significant 16 bits 
        
        
        //Serial.println(received_command,HEX);        //DEBUGGING  !!!!!!
    myReceiver.enableIRIn();      //Restart receiver  
 
     if (received_command!=0xFFFF) { // IGNORE FFFF which means they holding finger on button (for samsung anyway)

          //we must check for rapid fire and ignore it (toshiba)
          if (received_command!=previous_command) {    //if its new unique keypress
            key_repeat_timer=millis();                 //record time
            previous_command=received_command;         //update the previous holder
            act_on_input();                           // actually process it
          }
            else                           //its a repeat
            {
                if ( (millis()-key_repeat_timer) > 150 ) {      // repeat but after a long enough pause so dont ignore (too small it wont block repeats, too big it slows me down)
                   key_repeat_timer=millis();                 //record time
                   act_on_input();  
                }
                 else {     //repeat and it was TOO QUICK (ignore it
                 key_repeat_timer=millis();                 //record time
                 }
            }
   
     }    
    }

    
 } // ************************************************************************************** end of LOOP
  


