LCOV - code coverage report
Current view: top level - src/Hardware - IncrementDecrementButtons.hpp (source / functions) Hit Total Coverage
Test: 19d2efc7037c2e176feca44750a12594c76f466f Lines: 9 9 100.0 %
Date: 2019-11-24 14:50:27 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.14-5-g4ff2ed6