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 and decrement some counter or 14 : * setting. 15 : * 16 : * It behaves the same way as a computer keyboard: when you press the increment 17 : * (decrement) button, it increments (decrements) the counter once. 18 : * If you keep on pressing it for longer than a certain threshold, it keeps on 19 : * incrementing (decrementing) at a faster rate, until you release it. 20 : * If both the increment and the decrement button are pressed at once, it resets 21 : * the counter. 22 : * 23 : * @ingroup AH_HardwareUtils 24 : */ 25 : class IncrementDecrementButtons { 26 : public: 27 : /** 28 : * @brief Create a IncrementDecrementButtons object. 29 : * 30 : * @param incrementButton 31 : * The button to increment the counter. 32 : * The button is copied. 33 : * @param decrementButton 34 : * The button to decrement the counter. 35 : * The button is copied. 36 : */ 37 12 : IncrementDecrementButtons(const Button &incrementButton, 38 : const Button &decrementButton) 39 24 : : incrementButton(incrementButton), decrementButton(decrementButton) {} 40 : 41 : /// @see Button::begin 42 7 : void begin() { 43 7 : incrementButton.begin(); 44 7 : decrementButton.begin(); 45 7 : } 46 : 47 : /** 48 : * @brief An enumeration of the different actions to be performed by the 49 : * counter. 50 : * @todo Add states for initial press. 51 : */ 52 : enum State { 53 : Nothing = 0, ///< The counter should not be incremented. 54 : Increment, ///< The counter should be incremented. 55 : Decrement, ///< The counter should be decremented. 56 : Reset, ///< The counter should be reset to the initial value. 57 : }; 58 : 59 : /** 60 : * @brief Update and return the state of the increment/decrement button. 61 : */ 62 38 : State update() { return state = updateImplementation(); } 63 : 64 : /** 65 : * @brief Return the state of the increment/decrement button without 66 : * updating it. 67 : * 68 : * Returns the same value as the last @ref update call. 69 : */ 70 : State getState() const { return state; } 71 : 72 : #ifdef AH_INDIVIDUAL_BUTTON_INVERT 73 : /// @see Button::invert 74 : void invert() { 75 : incrementButton.invert(); 76 : decrementButton.invert(); 77 : } 78 : #endif 79 : 80 : protected: 81 : State updateImplementation(); 82 : 83 : private: 84 : Button incrementButton; 85 : Button decrementButton; 86 : 87 : enum { 88 : Initial, 89 : LongPress, 90 : AfterReset, 91 12 : } longPressState = Initial; 92 : unsigned long longPressRepeat; 93 12 : State state = Nothing; 94 : }; 95 : 96 : END_AH_NAMESPACE 97 : 98 : AH_DIAGNOSTIC_POP()