   // ---------------------------------
   // Left mouse button state update
   // ---------------------------------
-  if (!(PINB & 0b1000) && !leftButton)
-  {
-    Mouse.press(MOUSE_LEFT);
-    leftButton = true;
-  }
-  else
-  {
-    if ((PINB & 0b1000) && leftButton)
-    {
-      Mouse.release(MOUSE_LEFT);
-      leftButton = false;
-    }
-  }
+  ReadButton(leftButton);
+  UpdateButton(leftButton);
 
   // ---------------------------------
   // Right mouse button state update
   // ---------------------------------  
-  if (!(PINB & 0b0100) && !rightButton)
-  {
-    Mouse.press(MOUSE_RIGHT);
-    rightButton = true;
-  }
-  else
-  {
-    if ((PINB & 0b0100) && rightButton)
-    {
-      Mouse.release(MOUSE_RIGHT);
-      rightButton = false;
-    }
-  }
+  ReadButton(rightButton);
+  UpdateButton(rightButton);
   
   // ---------------------------------
   // Middle mouse button state update
   // ---------------------------------
-  if (!(PINB & 0b0010) && !middleButton)
-  {
-    Mouse.press(MOUSE_MIDDLE);
-    middleButton = true;
-  }
-  else
-  {
-    if ((PINB & 0b0010) && middleButton)
-    {
-      Mouse.release(MOUSE_MIDDLE);
-      middleButton = false;
-    }
-  }
+  ReadButton(middleButton);
+  UpdateButton(middleButton);
 
   // Wait a little before next update
   delay(10);

  yAxis.index       = (yAxis.index << 2) | ((PIND & 0b00001100) >> 2);
  yAxis.coordinate += lookupTable[yAxis.index & 0b00001111];
}

     - Grey   : Switch 1                     |   Pin: PB3
     - White  : Switch 2                     |   Pin: PB2
     - Black  : Switch 3                     |   Pin: PB1
   --------------------------------------------------------------------------------
   Latest additions:
     - 2016-01-28: Software switch debouncing
   ================================================================================ */

// =================================================================================
// Type definition
// =================================================================================

#ifndef DEBOUNCE_THREASHOLD
#define DEBOUNCE_THREASHOLD 50
#endif

// =================================================================================
// Type definition
// =================================================================================
typedef struct ENCODER_
{
  int8_t  coordinate;
  uint8_t index;
} ENCODER_;

typedef struct BUTTON_
{
  boolean state;
  boolean needUpdate;
  char    button;
  byte    bitmask;
  long    lastDebounceTime;
} BUTTON_;

// =================================================================================
// Constant definition
