Line data Source code
1 : /* ✔ */ 2 : 3 : #pragma once 4 : 5 : #include "Button.hpp" 6 : 7 : BEGIN_AH_NAMESPACE 8 : 9 : /** 10 : * @brief A class for buttons that increment some counter or setting. 11 : * 12 : * It behaves the same way as a computer keyboard: when you press the button, 13 : * it increments the counter once. If you keep on pressing it for longer than 14 : * a certain threshold, it keeps on incrementing at a faster rate, until you 15 : * release it. 16 : * 17 : * @ingroup AH_HardwareUtils 18 : */ 19 : class IncrementButton { 20 : public: 21 : /** 22 : * @brief Create a IncrementButton. 23 : * 24 : * @param button 25 : * The button to read from. 26 : * The button is copied. 27 : */ 28 2 : IncrementButton(const Button &button) : button(button) {} 29 : 30 : /// @see Button::begin 31 2 : void begin() { button.begin(); } 32 : 33 : /** 34 : * @brief An enumeration of the different actions to be performed by the 35 : * counter. 36 : * @todo Add states for initial press. 37 : */ 38 : enum State { 39 : Nothing = 0, ///< The counter must not be incremented. 40 : IncrementShort, ///< The counter must be incremented (after short press). 41 : IncrementLong, ///< The counter must be incremented (after long press). 42 : IncrementHold, ///< The counter must be incremented (still pressed). 43 : ReleasedShort, ///< The button was released after a short press. 44 : ReleasedLong, ///< The button was released after a long press. 45 : }; 46 : 47 : /** 48 : * @brief Update and return the state of the increment button. 49 : */ 50 10 : State update() { return state = updateImplementation(); } 51 : 52 : /** 53 : * @brief Return the state of the increment button without updating it. 54 : * 55 : * Returns the same value as the last @ref update call. 56 : */ 57 : State getState() const { return state; } 58 : 59 : /// @see Button::invert 60 : void invert() { button.invert(); } 61 : 62 : protected: 63 : State updateImplementation(); 64 : 65 : private: 66 : Button button; 67 : 68 : enum { 69 : Initial, 70 : LongPress, 71 : } longPressState = Initial; 72 : unsigned long longPressRepeat; 73 : 74 : State state = Nothing; 75 : }; 76 : 77 : END_AH_NAMESPACE