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

          Line data    Source code
       1             : /* ✔ */
       2             : 
       3             : #pragma once
       4             : 
       5             : #include <Def/Def.hpp>
       6             : 
       7             : BEGIN_CS_NAMESPACE
       8             : 
       9             : /**
      10             :  * @brief   A class that reads the states of a button matrix.
      11             :  *
      12             :  * @tparam  nb_rows
      13             :  *          The number of rows in the button matrix.
      14             :  * @tparam  nb_cols
      15             :  *          The number of columns in the button matrix.
      16             :  * 
      17             :  * @ingroup HardwareUtils
      18             :  */
      19             : template <uint8_t nb_rows, uint8_t nb_cols>
      20             : class ButtonMatrix {
      21             :   public:
      22             :     /**
      23             :      * @brief   Construct a new ButtonMatrix object.
      24             :      *
      25             :      * @param   rowPins
      26             :      *          A list of pin numbers connected to the rows of the button
      27             :      *          matrix.  
      28             :      *          **⚠** These pins will be driven LOW as outputs (Lo-Z).
      29             :      * @param   colPins
      30             :      *          A list of pin numbers connected to the columns of the button
      31             :      *          matrix.  
      32             :      *          These pins will be used as inputs (Hi-Z), and the internal 
      33             :      *          pull-up resistor will be enabled.
      34             :      */
      35             :     ButtonMatrix(const PinList<nb_rows> &rowPins,
      36             :                  const PinList<nb_cols> &colPins);
      37             :     /**
      38             :      * @brief   Destructor.
      39             :      */
      40           9 :     virtual ~ButtonMatrix() = default;
      41             : 
      42             :     /**
      43             :      * @brief   Initialize (enable internal pull-up resistors on column pins).
      44             :      */
      45             :     void begin();
      46             : 
      47             :     /**
      48             :      * @brief   Scan the matrix, read all button states, and call the
      49             :      *          onButtonChanged callback.
      50             :      */
      51             :     void refresh();
      52             : 
      53             :   private:
      54             :     /**
      55             :      * @brief   The callback function that is called whenever a button changes
      56             :      *          state.
      57             :      * 
      58             :      * @param   row
      59             :      *          The row of the button that changed state.
      60             :      * @param   col
      61             :      *          The column of the button that changed state.
      62             :      * @param   state
      63             :      *          The new state of the button.
      64             :      */
      65             :     virtual void onButtonChanged(uint8_t row, uint8_t col, bool state) = 0;
      66             : 
      67             :     static inline uint8_t positionToBits(uint8_t col, uint8_t row);
      68             :     static inline uint8_t bitsToIndex(uint8_t bits);
      69             :     static inline uint8_t bitsToBitmask(uint8_t bits);
      70             :     bool getPrevState(uint8_t col, uint8_t row);
      71             :     void setPrevState(uint8_t col, uint8_t row, bool state);
      72             : 
      73             :     unsigned long prevRefresh = 0;
      74             :     uint8_t prevStates[(nb_cols * nb_rows + 7) / 8];
      75             : 
      76             :     const PinList<nb_rows> rowPins;
      77             :     const PinList<nb_cols> colPins;
      78             : };
      79             : 
      80             : END_CS_NAMESPACE
      81             : 
      82             : #include "ButtonMatrix.ipp" // Template implementations

Generated by: LCOV version 1.14-5-g4ff2ed6