/******************************************************************************
 Title:    Code for an IR reciever 
 
 made for a SONY RM-D505 remote,
 tiny26 running at 1Mhz,
 
 0           1ms       8-10   counts
         Barrier at 14
 1           2.5ms     18-19 counts
         Barrier at 28 
 start bit   5ms       37-38 counts

 
 Author:   rue_mohr
 Date:     Jan 8-14 2006
 Software: AVR-GCC 3.3 
 Hardware: attiny26
 
 ******************************************************************************
 
 New Hardware M32 at 6Mhz
 IO on D3 (int 1)
 
  0           0.5ms       12   counts
         Barrier at 18
 1           1ms          23 counts
         Barrier at 41
 start bit   2.5ms       58 counts
 
    
*******************************************************************************/
#include"IR.h"

/*
int main (void)  {
 
      
  TimerInit();   // initialize timer    
  Int0Init();    // initialize int0  
  sei();         // turn on interrupts
  
  SetBit(IRDet, PORTB);            // sets pullup on IR reciever
                  
  while(1) {      
     if (IsHigh(IRDR, IRStatus)) { // when the flag gets set
       if (IRData == 0x0AD1) { SetBit(2, PORTA); }
       if (IRData == 0x0831) { ClearBit(2, PORTA); }
       ClearBit(IRDR, IRStatus);   // this is to ack the byte was read
     }
  }
}
    */
//------------------------| FUNCTIONS |------------------------


//------| Timerc Functions |--------


void IRInit() {

  // this is pin change for port B, bit 5
  SetBit(PCIE0, PCICR);
  SetBit(PCINT5, PCMSK0);
  
  // 8 bit timer, set up 10Khz  (15.625Khz)
  // tho it dosn't have to be a 8 bit timer, it dosn't matter really
  // as long as all the diodes down my left side hurt, nothing really matters.
  // robot with a brain the size of a planet, and they have me writing c code, pathetic.
  TCCR1B = (1<<CS12);  // 6Mhz / 256  !!!PROCESSOR!!!!
  OCR1A = 1024;
  SetBit(OCIE1A, TIMSK1);
  TimerReset();
  
}


//ISR (INT1_vect) { 
ISR(PCINT0_vect) {
  if (IRL()) {  // bit just went high, check value of time
    OnEdge(0);
  } else {      // bit just went low, reset timer
     TimerReset();  // start timing      
  }
}

ISR(TIMER1_COMPA_vect) {
  OnEdge(1);
}

void inline OnEdge(unsigned char reset) {
  volatile static unsigned      BitCount;
  volatile static unsigned int  IRRecReg;
  unsigned int         count;  
  
  count = TCNT1;   // snap shot of timer0 !!!PROCESSOR!!!!
  
  if (reset) {
      BitCount = 0;  // clear reciever register counter
      IRRecReg = 0;      
  }
  
  if (0) {
  } else if (count > 256) {    // 1ms pulse = 1 (18)
      IRRecReg <<= 1;   
      IRRecReg |= 0x01;  // no this is not an error, dont ask!
      BitCount++; 
  } else {                    // 0.5ms pulse = 0  
      IRRecReg <<= 1;
      BitCount++;
  } 
  
  // set to recieve 12 bits
  if (BitCount == 10){   
     if ((IRRecReg & 0xF00) == 0x300) { 
       IRData = (IRRecReg & 0xFF);   // only 8 bit codes on this one  
       IRSetDR();  // set data ready flag
     }
  }

  
}
