LCOV - code coverage report
Current view: top level - src/AH/Hardware - IncrementButton.hpp (source / functions) Hit Total Coverage
Test: ffed98f648fe78e7aa7bdd228474317d40dadbec Lines: 3 3 100.0 %
Date: 2022-05-28 15:22:59 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          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             :     /// @see    Button::invert
      63             :     void invert() { button.invert(); }
      64             : 
      65             :   protected:
      66             :     State updateImplementation();
      67             : 
      68             :   private:
      69             :     Button button;
      70             : 
      71             :     enum {
      72             :         Initial,
      73             :         LongPress,
      74             :     } longPressState = Initial;
      75             :     unsigned long longPressRepeat;
      76             : 
      77             :     State state = Nothing;
      78             : };
      79             : 
      80             : END_AH_NAMESPACE
      81             : 
      82             : AH_DIAGNOSTIC_POP()

Generated by: LCOV version 1.15