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