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 6 : IncrementButton(const Button &button) : button(button) {} 32 : 33 : /// @see Button::begin 34 4 : 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 : Increment, ///< The counter must be incremented. 44 : }; 45 : 46 : /** 47 : * @brief Update and return the state of the increment button. 48 : */ 49 19 : State update() { return state = updateImplementation(); } 50 : 51 : /** 52 : * @brief Return the state of the increment button without updating it. 53 : * 54 : * Returns the same value as the last @ref update call. 55 : */ 56 : State getState() const { return state; } 57 : 58 : #ifdef AH_INDIVIDUAL_BUTTON_INVERT 59 : /// @see Button::invert 60 : void invert() { button.invert(); } 61 : #endif 62 : 63 : protected: 64 : State updateImplementation(); 65 : 66 : private: 67 : Button button; 68 : 69 : enum { 70 : Initial, 71 : LongPress, 72 6 : } longPressState = Initial; 73 : unsigned long longPressRepeat; 74 : 75 6 : State state = Nothing; 76 : }; 77 : 78 : END_AH_NAMESPACE 79 : 80 : AH_DIAGNOSTIC_POP()