LCOV - code coverage report
Current view: top level - src/Selectors - SwitchSelector.hpp (source / functions) Hit Total Coverage
Test: b8a30b4b7040ae1abf162fd0a258beaa2de43626 Lines: 5 17 29.4 %
Date: 2024-12-21 21:28:55 Functions: 3 7 42.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #pragma once
       2             : 
       3             : #include "Selector.hpp"
       4             : #include <AH/Hardware/Button.hpp>
       5             : 
       6             : BEGIN_CS_NAMESPACE
       7             : 
       8             : template <class Callback = EmptySelectorCallback>
       9             : class GenericSwitchSelector : public GenericSelector<2, Callback> {
      10             :     using Parent = GenericSelector<2, Callback>;
      11             : 
      12             :   public:
      13           2 :     GenericSwitchSelector(Selectable<2> &selectable, const Callback &callback,
      14             :                           const AH::Button &button)
      15           2 :         : GenericSelector<2, Callback>(selectable, callback), button(button) {}
      16             : 
      17           0 :     void begin() override {
      18           0 :         Parent::begin();
      19           0 :         button.begin();
      20           0 :     }
      21             : 
      22           0 :     void update() override {
      23           0 :         Parent::update();
      24           0 :         AH::Button::State state = button.update();
      25           0 :         if (state == AH::Button::Falling)
      26           0 :             this->set(1);
      27           0 :         else if (state == AH::Button::Rising)
      28           0 :             this->set(0);
      29           0 :     }
      30             : 
      31             :     AH::Button::State getButtonState() const { return button.getState(); }
      32             : 
      33             :     /// @see @ref AH::Button::invert()
      34             :     void invert() { button.invert(); }
      35             : 
      36             :   private:
      37             :     AH::Button button;
      38             : };
      39             : 
      40             : /**
      41             :  * @brief   Selector that selects one of two settings, based on the state of a 
      42             :  *          toggle or momentary switch.
      43             :  * 
      44             :  * @htmlonly 
      45             :  * <object type="image/svg+xml" data="../../selector-one-toggle-switch-LED.svg"></object>
      46             :  * @endhtmlonly
      47             :  * 
      48             :  * @ingroup Selectors
      49             :  */
      50             : class SwitchSelector : public GenericSwitchSelector<> {
      51             :   public:
      52           1 :     SwitchSelector(Selectable<2> &selectable, const AH::Button &button)
      53           1 :         : GenericSwitchSelector<> {
      54             :               selectable,
      55             :               {},
      56             :               button,
      57           1 :           } {}
      58             : };
      59             : 
      60             : END_CS_NAMESPACE

Generated by: LCOV version 1.15