/*******************************************************************************
 Input test code
 
 Rue_mohr
  
*******************************************************************************/


#include <avr/io.h>

// misc

#define OUTPUT             1
#define INPUT              0

#define IsHigh(BIT, PORT)    ((PORT & (1<<BIT)) != 0)
#define IsLow(BIT, PORT)     ((PORT & (1<<BIT)) == 0)
#define SetBit(BIT, PORT)     PORT |= (1<<BIT)
#define ClearBit(BIT, PORT)   PORT &= ~(1<<BIT)

// which bit the buttons are on (porta)
#define Button1         0
#define Button2         1
#define LED             2

#define LedOn()         SetBit(LED, PORTA)
#define LedOff()        ClearBit(LED, PORTA)

void Delay(int delay);

int main (void) {

 char buttonStat, oldButtonStat;
 char changes;

  // set up directions 
  DDRA = (INPUT << PA0 | INPUT << PA1 |OUTPUT << PA2 |INPUT << PA3 |INPUT << PA4 |INPUT << PA5 |INPUT << PA6 |INPUT << PA7);
  DDRB = (INPUT << PB0 | INPUT << PB1 |INPUT << PB2 |INPUT << PB3 |INPUT << PB4 |INPUT << PB5 |INPUT << PB6 |INPUT << PB7);
  DDRC = (INPUT << PC0 | INPUT << PC1 |INPUT << PC2 |INPUT << PC3 |INPUT << PC4 |INPUT << PC5 |INPUT << PC6 |INPUT << PC7);
  DDRD = (INPUT << PD0 | INPUT << PD1 |INPUT << PD2 |INPUT << PD3 |INPUT << PD4 |INPUT << PD5 |INPUT << PD6 |INPUT << PD7);        

 while(1) {

      buttonStat = PINA;  // take a snapshot of the port status 
      
      changes = buttonStat ^ oldButtonStat;  // calculate difference
      
      // we want to know the buttons that are now 0 (now low), as in they were 1, so we AND with oldbuttons
      changes &= oldButtonStat;

      // test for conditions
      if (0) { 
      } else if (IsHigh(Button1, changes)) {
         LedOn();
      } else if (IsHigh(Button2, changes)) {
         LedOff();
      } else {      
        // if there was no difference, dont do anything
      } 
           
      oldButtonStat = buttonStat; // new that were done with teh data, its old
           
   }


}
