// a Simple Button class
// brought the "debounce" example towards OOP


class Button {                         // a simple class for buttons based on the "Debounce" example
    const byte buttonPin;              // the GPIO / pin for the button
    static constexpr byte debounceDelay = 30;    // the debounce time; increase if the output flickers. Static because we only need one value for all buttons
    const bool active;                 // is the pin active HIGH or active LOW (will also activate the pullups!)
    bool lastButtonState = HIGH;       // the previous reading from the input pin
    byte lastDebounceTime = 0;         // the last time the output pin was toggled - we check only ONE byte, so I didn't mess around with unsigned long

  public:
    /**
       \brief constructor for a button

       The constructor takes the GPIO as parameter.
       If you omit the second parameter, the library will activate the internal pullup resistor
       and the button should connect to GND.
       If you set the second parameter to HIGH, the button is active HIGH. 
       The button should connect to VCC. 
       The internal pullups will not be used but you will need an external pulldown resistor.

       \param attachTo the GPIO for the button
       \param active LOW (default) - if button connects to GND, HIGH if button connects to VCC
    */
    Button(byte attachTo, bool active = LOW) : buttonPin(attachTo), active(active) {}

    /**
       \brief set the pin to the proper state

       Call this function in your setup().
       The pinMode will be set according to your constructor.
    */
    void begin() {
      if (active == LOW)
        pinMode(buttonPin, INPUT_PULLUP);
      else
        pinMode(buttonPin, INPUT);
    }

    /**
        \brief indicate if button was pressed since last call

        @return HIGH if button was pressed since last call - debounce
    */
    bool wasPressed() {
      bool buttonState = LOW;                                        // the current reading from the input pin
      byte reading = LOW;                                            // "translated" state of button LOW = released, HIGH = pressed, despite the electrical state of the input pint
      if (digitalRead(buttonPin) == active) reading = HIGH;          // if we are using INPUT_PULLUP we are checking invers to LOW Pin
      if (((millis() & 0xFF ) - lastDebounceTime) > debounceDelay)   // If the switch changed, AFTER any pressing or noise
      {
        if (reading != lastButtonState && lastButtonState == LOW)    // If there was a change and and last state was LOW (= released)
        {
          buttonState = HIGH;
        }
        lastDebounceTime = millis() & 0xFF;
        lastButtonState = reading;
      }
      return buttonState;
    }
};

Button buttonA{A0};
Button buttonB{A1};
Button buttonC{A2};

void setup()
{
  Serial.begin(115200);
  buttonA.begin();
  buttonB.begin();
  buttonC.begin();
}

void loop()
{
  if (buttonA.wasPressed()) 
    Serial.println(F("ButtonA"));
  if (buttonB.wasPressed()) 
    Serial.println(F("ButtonB"));
  if (buttonC.wasPressed()) 
    Serial.println(F("ButtonC"));
}