#ifndef porting_h
#define porting_h

// Define target flags to test in the code if necessary

#define PORT_TEENSY_2 500
#define PORT_ARDUINO_UNO_R3 510

#ifdef __AVR_ATmega32U4__

// I have a Teensy 2.0 board, perhaps this also works for an Arduino Leonardo
// (which uses the same chip), but I don't know if the pin mappings are
// the same.

#define SYSCLOCK 16000000

#define USECPERTICK 50

#define IR_RECIEVE_PIN 22

#define IR_XMIT_PIN 10

#define DISABLE_SEND_TIMER_INTERRUPTS \
   TIMSK4 = 0

   // Set WGM2 mode to 5 for phase correct PWM mode (bits split across 2
   // control regs) and CS2 to 1 (meaning run at full rate of sysclock).
// PUT CODE HERE - look up what all this means... (and fix comment)
#define INITIALIZE_SEND_TIMER_PWM_MODE_AND_CLOCK_DIVIDER \
   TCCR4A = 0; \
   TCCR4B = _BV(CS40); \
   TCCR4C = 0; \
   TCCR4D = 0; \
   TCCR4E = 0; \
   TC4H = (SYSCLOCK * USECPERTICK / 1000000) >> 8; \
   OCR4C = (SYSCLOCK * USECPERTICK / 1000000) & 255; \
   TC4H = 0; \
   TCNT4 = 0

// COM4A1 COM4A0 COM4B1 COM4B0 FOC4A FOC4B PWM4A PWM4B all set to 0 (TCCR4A)
// PWM4X PSR4 DTPS41 DTPS40 CS43 CS42 CS41, all set to 0 CS40 set to 1 (TCCR4B)
// COM4A1S COM4A0S COM4B1S COMAB0S COM4D1 COM4D0 FOC4D PWM4D set to 0 (TCCR4C)
// FPIE4 FPEN4 FPNC4 FPES4 FPAC4 FPF4 WGM41 WGM40 set to 0 (TCCR4D)
// TLOCK4 ENHC4 OC4OE5 OC4OE4 OC4OE3 OC4OE2 OC4OE1 OC4OE0 set to 0 (TCCR4E)
// TC4H is a temp register for fiddling 10 bit values
// OCR4C value to compare to TCNT4 which resets TCNT4
// TCNT4 is the actual timer4 counter value

#define DISABLE_SEND_TIMER_PWM_OUTPUT \
   TCCR4A &= ~_BV(COM4A1)

#define ENABLE_SEND_TIMER_PWM_OUTPUT \
   TCCR4A |= _BV(COM4A1)

#define PORT_TARGET PORT_TEENSY_2

#endif

#ifdef __AVR_ATmega328P__

// My original AardRemote prototype used an Arduino UNO R3 board, so these
// are the definitions I started with.

#define SYSCLOCK 16000000

#define IR_RECIEVE_PIN 8

#define IR_XMIT_PIN 3

#define DISABLE_SEND_TIMER_INTERRUPTS \
   TIMSK2 = 0

   // Set WGM2 mode to 5 for phase correct PWM mode (bits split across 2
   // control regs) and CS2 to 1 (meaning run at full rate of sysclock).
#define INITIALIZE_SEND_TIMER_PWM_MODE_AND_CLOCK_DIVIDER \
   TCCR2A = _BV(WGM20); \
   TCCR2B = _BV(WGM22) | _BV(CS20)

#define DISABLE_SEND_TIMER_PWM_OUTPUT \
   TCCR2A &= ~_BV(COM2B1)

#define ENABLE_SEND_TIMER_PWM_OUTPUT \
   TCCR2A |= _BV(COM2B1)

#define PORT_TARGET PORT_ARDUINO_UNO_R3

#endif

#ifndef PORT_TARGET
#error "AardRemote IR code not yet ported to this target"
#endif

#endif // porting_h
