Button Read Optimizations

So does this code now solve the debouncing issue as well?
I'm testing only in an online Arduino simulator environment so debounce testing is a no go for me, as it's without hardware :smiley:

Is there a way to shorten this? Or is it about as good as it can get now?



enum ActionType { NOTHING = 0,
                       INCREASE = 1,
                       DECREASE = 2,
                       SHORT_PRESS = 3,
                       LONG_PRESS = 4
};

class Button
{
public:
    static const unsigned long longPressMin = 1000;
    static const unsigned long shortPressMin = 25;
    byte _pin;

    Button(int pin)
    {
        _pin = pin;
        pinMode(_pin, INPUT_PULLUP);
    }

    int read()
    {
        static unsigned long timeCapture;
        static byte lastState = HIGH;
        byte currentState = digitalRead(_pin);

        if (lastState != currentState)
        {
            lastState = currentState;

            if (currentState == LOW)
            {
                timeCapture = millis();
                timeCapture = (timeCapture == 0 ? 1 : timeCapture);
            }

            // released before timeout
            else if (timeCapture != 0)
            {
                if ((millis() - timeCapture) > Button::shortPressMin)
                {
                    timeCapture = 0;
                    return SHORT_PRESS;
                }
                timeCapture = 0;
            }
        }

        // timeout
        if (timeCapture != 0 && (millis() - timeCapture) > Button::longPressMin)
        {
            timeCapture = 0;
            return LONG_PRESS;
        }

        return NOTHING;

    } // End of read()
};

Button button(2);

// -----------------------------------------------------------------------------

void setup()
{
    Serial.begin(9600);
}

void loop()
{
    switch (button.read())
    {
    case SHORT_PRESS:
        Serial.println("short");
        break;

    case LONG_PRESS:
        Serial.println("long");
        break;
    }
}