/****** RFD21733 & Teensy 3.0 HCI ************/

/************ Constants **********************/
// Define pin numbers
const int ledPin = 13;   // LED Pin is 13 for the Teensy 3.0

const int in0 = 0; // Define input pin 0
const int in1 = 1; // Define input pin 1
const int in2 = 2; // Define input pin 2
const int button1 = 20; // Define input pin 20
/*********************************************/

/************ Variables **********************/
// varibles to store input states
int read0 = 0;
int read1 = 0;
int read2 = 0;
int read_button = 0;

// store recieved data (in decimal)
int rx_in = 0;

// timers for debouncing
elapsedMicros bounce_time;
/*********************************************/

/************ Interrupt handler **************/
void isr()
{
    // Disable external pin interrupts
    intr_dis();
    
    // Read the input
    getInput();
    
    // Enable interrupts
    intr_en();
}
/*********************************************/

/************ Interrupt Disable **************/
void intr_dis(void)
{
 // Disable interrupts
 detachInterrupt(in0);
 detachInterrupt(in1);
 detachInterrupt(in2);
}
/*********************************************/

/************ Interrupt Enable ***************/
void intr_en(void)
{
 // Enable interrupts
 attachInterrupt(in0, isr, RISING); // Attach interrupt to pin 0
 attachInterrupt(in1, isr, RISING); // Attach interrupt to pin 1
 attachInterrupt(in2, isr, RISING); // Attach interrupt to pin 2
}
/*********************************************/

/************ Read Inputs ********************/
void getInput(void)
{
  // Read values from input pins
  read0 = digitalRead(in0); // get input from in0
  read1 = digitalRead(in1); // get input from in1
  read2 = digitalRead(in2); // get input from in2
  read_button = digitalRead(button1); // get input from button
    
  // Debounce
  // (IE. Accept if mechanical bounce hasn't
  // caused the command to be sent again)
  if(bounce_time > 100)
  {
    // Convert to single integer value
    rx_in = (read0*1)+(read1*2)+(read2*4);
    
    // reset timer
    bounce_time = 0;
    
    // Run a function that prints the output
    print_input();
  }
}
/*********************************************/

/************ Serial Output ******************/
void print_input(void)
{
  // print input value and button state
  Serial.print(rx_in);

//  Serial.print("   ");
//  Serial.print(bounce_time);
  
  Serial.print("   ");
  Serial.print(read_button);

  Serial.println(" ");
}
/*********************************************/

/************ Initialisations **************/
void setup()
{
  Serial.begin(9600); // USB is always 12 Mbit/sec
  
  pinMode(ledPin, OUTPUT); // LED pin is an output  
  
  pinMode(in0, INPUT);  // Configure in0 pin as input
  pinMode(in1, INPUT);  // Configure in1 pin as input
  pinMode(in2, INPUT);  // Configure in2 pin as input
  pinMode(button1, INPUT_PULLUP);  // Configure button pin as input with pullup resistor

  // Enable interrupts on input pins
  intr_en();
  
  // The interrupts have been set to only respond to the rising edge,
  // this is so that when the RFD21733 chip automaticall resets the
  // outputs to 000 after the 20ms hang time, another interrupt isn't
  // triggered.
  
  // Flash LED to signal ready... (one second on, one second off)
  digitalWrite(ledPin, HIGH);
  delay(1000);
  digitalWrite(ledPin, LOW);
  delay(1000);
  
  Serial.println("RFD21733 Interface:");
}
/*********************************************/

/************ Main Program Loop **************/
void loop()
{
  // Purely reactive program
}
/*********************************************/