21 #include "avrlibdefs.h" 
   27 #define DEBOUNCE_COUNT   3   // should be equal to 2 or greater 
   29 static bool isDown[NUM_BUTTONS]   = { 
false, 
false, 
false,
 
   30                                       false, 
false, 
false };
 
   31 static bool wasEvent[NUM_BUTTONS] = { 
false, 
false, 
false,
 
   32                                       false, 
false, 
false };
 
   33 static uint8_t upCount[NUM_BUTTONS] = { DEBOUNCE_COUNT, DEBOUNCE_COUNT,
 
   34                                         DEBOUNCE_COUNT, DEBOUNCE_COUNT,
 
   35                                         DEBOUNCE_COUNT, DEBOUNCE_COUNT };
 
   36 static uint8_t downCount[NUM_BUTTONS] = { 0, 0, 0, 0, 0, 0 };
 
   38 void waitFor(uint8_t button)
 
   52     return wasEvent[button] && isDown[button];
 
   60     return wasEvent[button] && !isDown[button];
 
   69     for (button = 0; button < NUM_BUTTONS; button++)
 
   74             downCount[button] = MIN(downCount[button]+1, DEBOUNCE_COUNT);
 
   79             upCount[button] = MIN(upCount[button]+1, DEBOUNCE_COUNT);
 
   80             downCount[button] = 0;
 
   86             if (upCount[button] >= DEBOUNCE_COUNT)
 
   88                 isDown[button] = 
false;
 
   89                 wasEvent[button] = 
true;
 
   93                 wasEvent[button] = 
false;
 
   98             if (downCount[button] >= DEBOUNCE_COUNT)
 
  100                 isDown[button] = 
true;
 
  101                 wasEvent[button] = 
true;
 
  105                 wasEvent[button] = 
false;
 
  118     case RED_BUTTON:      
return RED_BUTTON_DOWN;
 
  119     case L_UP_BUTTON:     
return L_UP_BUTTON_DOWN;
 
  120     case L_DOWN_BUTTON:   
return L_DOWN_BUTTON_DOWN;
 
  121     case R_UP_BUTTON:     
return R_UP_BUTTON_DOWN;
 
  122     case R_DOWN_BUTTON:   
return R_DOWN_BUTTON_DOWN;
 
  123     case NEST_BUTTON:     
return NEST_BUTTON_DOWN;
 
  130 inline bool bothRightButtonsPressed(
void)
 
  133            (
justPressed(R_UP_BUTTON) && isDown[R_DOWN_BUTTON])      ||
 
  134            (
justPressed(R_DOWN_BUTTON) && isDown[R_UP_BUTTON]);
 
  138 inline bool bothLeftButtonsPressed(
void)
 
  141            (
justPressed(L_UP_BUTTON) && isDown[L_DOWN_BUTTON])      ||
 
  142            (
justPressed(L_DOWN_BUTTON) && isDown[L_UP_BUTTON]);