LCOV - code coverage report
Current view: top level - src/Selectors - ManyButtonsSelector.hpp (source / functions) Hit Total Coverage
Test: e224b347cd670555e44f06608ac41bd1ace9d9d8 Lines: 25 25 100.0 %
Date: 2020-09-08 17:44:46 Functions: 8 13 61.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #pragma once
       2             : 
       3             : #include "Selector.hpp"
       4             : #include <AH/Hardware/ExtendedInputOutput/ExtendedInputOutput.hpp>
       5             : #include <Def/Def.hpp>
       6             : 
       7             : BEGIN_CS_NAMESPACE
       8             : 
       9             : template <setting_t N, class Callback = EmptySelectorCallback>
      10           3 : class GenericManyButtonsSelector : public GenericSelector<N, Callback> {
      11             :     using Parent = GenericSelector<N, Callback>;
      12             : 
      13             :   public:
      14           3 :     GenericManyButtonsSelector(Selectable<N> &selectable,
      15             :                                const Callback &callback,
      16             :                                const PinList<N> &buttonPins)
      17           3 :         : GenericSelector<N, Callback>{selectable, callback},
      18           6 :           buttonPins(buttonPins) {}
      19             : 
      20           1 :     void begin() override {
      21           1 :         Parent::begin();
      22           5 :         for (const pin_t &pin : buttonPins)
      23           4 :             AH::ExtIO::pinMode(pin, INPUT_PULLUP);
      24           1 :     }
      25             : 
      26           1 :     void update() override {
      27           1 :         Parent::update();
      28           5 :         for (setting_t i = 0; i < N; i++)
      29             :             // TODO: invert?
      30           4 :             if (AH::ExtIO::digitalRead(buttonPins[i]) == LOW) {
      31           1 :                 if (this->get() != i &&
      32           1 :                     AH::ExtIO::digitalRead(buttonPins[this->get()]) != LOW)
      33           1 :                     this->set(i);
      34           1 :                 break;
      35             :             }
      36           1 :     }
      37             : 
      38             :   private:
      39             :     PinList<N> buttonPins;
      40             : };
      41             : 
      42             : // -------------------------------------------------------------------------- //
      43             : 
      44             : /**
      45             :  * @brief   Selector that reads from @f$ N @f$ buttons.
      46             :  * 
      47             :  * Pressing the @f$ n @f$-th button selects the @f$ n @f$-th setting.
      48             :  * 
      49             :  * @htmlonly 
      50             :  * <object type="image/svg+xml" data="../../selector-multiple-momentary-switches-LED.svg"></object>
      51             :  * @endhtmlonly
      52             :  * 
      53             :  * @ingroup Selectors
      54             :  * 
      55             :  * @tparam  N 
      56             :  *          The number of settings.
      57             :  */
      58             : template <setting_t N>
      59           2 : class ManyButtonsSelector : public GenericManyButtonsSelector<N> {
      60             :   public:
      61           2 :     ManyButtonsSelector(Selectable<N> &selectable, const PinList<N> &buttonPins)
      62           2 :         : GenericManyButtonsSelector<N>{
      63           2 :               selectable,
      64           2 :               {},
      65           2 :               buttonPins,
      66           4 :           } {}
      67             : };
      68             : 
      69             : END_CS_NAMESPACE

Generated by: LCOV version 1.14-6-g40580cd