LCOV - code coverage report
Current view: top level - src/MIDI_Outputs/Bankable/Abstract - MIDIButtonLatched.hpp (source / functions) Hit Total Coverage
Test: 90a1b9beff85a60dc6ebcea034a947a845e56960 Lines: 4 23 17.4 %
Date: 2019-11-30 15:53:32 Functions: 4 16 25.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #pragma once
       2             : 
       3             : #include <AH/Hardware/Button.hpp>
       4             : #include <Banks/BankableMIDIOutput.hpp>
       5             : #include <Def/Def.hpp>
       6             : #include <MIDI_Outputs/Abstract/MIDIOutputElement.hpp>
       7             : 
       8             : BEGIN_CS_NAMESPACE
       9             : 
      10             : namespace Bankable {
      11             : 
      12             : /**
      13             :  * @brief   A class for momentary buttons and switches that send MIDI events.
      14             :  *
      15             :  * The button is debounced, and the button is latched (press once to enable, 
      16             :  * press again to disable) (toggle).
      17             :  *
      18             :  * @see     AH::Button
      19             :  */
      20             : template <class BankAddress, class Sender>
      21           2 : class MIDIButtonLatched : public MIDIOutputElement {
      22             :   protected:
      23             :     /**
      24             :      * @brief   Create a new bankable MIDIButtonLatched object on the given pin
      25             :      *          and address.
      26             :      * 
      27             :      * @param   bankAddress
      28             :      *          The bankable MIDI address to send to.
      29             :      * @param   pin
      30             :      *          The digital input pin with the button connected.
      31             :      *          The internal pull-up resistor will be enabled.
      32             :      * @param   sender
      33             :      *          The MIDI sender to use.
      34             :      */
      35           2 :     MIDIButtonLatched(const BankAddress &bankAddress, pin_t pin,
      36             :                       const Sender &sender)
      37           4 :         : address{bankAddress}, button{pin}, sender{sender} {}
      38             : 
      39             :   public:
      40           0 :     void begin() override { button.begin(); }
      41           0 :     void update() override {
      42           0 :         AH::Button::State state = button.update();
      43           0 :         if (state == AH::Button::Falling)
      44           0 :             toggleState();
      45           0 :     }
      46             : 
      47           0 :     bool toggleState() {
      48           0 :         setState(!getState());
      49           0 :         return getState();
      50             :     }
      51           0 :     bool getState() const { return state; }
      52           0 :     void setState(bool state) {
      53           0 :         this->state = state;
      54           0 :         if (state) {
      55           0 :             address.lock();
      56           0 :             sender.sendOn(address.getActiveAddress());
      57           0 :         } else {
      58           0 :             sender.sendOff(address.getActiveAddress());
      59           0 :             address.unlock();
      60             :         }
      61           0 :     }
      62             : 
      63             :     AH::Button::State getButtonState() const { return button.getState(); }
      64             : 
      65             :   private:
      66             :     BankAddress address;
      67             :     AH::Button button;
      68           2 :     bool state = false;
      69             : 
      70             :   public:
      71             :     Sender sender;
      72             : };
      73             : 
      74             : } // namespace Bankable
      75             : 
      76             : END_CS_NAMESPACE

Generated by: LCOV version 1.14-5-g4ff2ed6