LCOV - code coverage report
Current view: top level - src/MIDI_Outputs/Bankable/Abstract - MIDIButton.hpp (source / functions) Hit Total Coverage
Test: 19d2efc7037c2e176feca44750a12594c76f466f Lines: 14 14 100.0 %
Date: 2019-11-24 14:50:27 Functions: 16 30 53.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #pragma once
       2             : 
       3             : #include <Banks/BankableMIDIOutput.hpp>
       4             : #include <Def/Def.hpp>
       5             : #include <Hardware/Button.hpp>
       6             : #include <MIDI_Outputs/Abstract/MIDIOutputElement.hpp>
       7             : 
       8             : BEGIN_CS_NAMESPACE
       9             : 
      10             : namespace Bankable {
      11             : 
      12             : /**
      13             :  * @brief   An abstract class for momentary push buttons that send MIDI events.
      14             :  *
      15             :  * The button is debounced.
      16             :  *
      17             :  * @see     Button
      18             :  */
      19             : template <class BankAddress, class Sender>
      20          12 : class MIDIButton : public MIDIOutputElement {
      21             :   public:
      22             :     /**
      23             :      * @brief   Construct a new bankable MIDIButton.
      24             :      *
      25             :      * @param   bankAddress
      26             :      *          The bankable MIDI address to send to.
      27             :      * @param   pin
      28             :      *          The digital input pin with the button connected.
      29             :      *          The internal pull-up resistor will be enabled.
      30             :      * @param   sender
      31             :      *          The MIDI sender to use.
      32             :      */
      33          12 :     MIDIButton(const BankAddress &bankAddress, pin_t pin, const Sender &sender)
      34          24 :         : address{bankAddress}, button{pin}, sender{sender} {}
      35             : 
      36           6 :     void begin() override { button.begin(); }
      37          26 :     void update() override {
      38          26 :         Button::State state = button.update();
      39          26 :         if (state == Button::Falling) {
      40           7 :             address.lock();
      41           7 :             sender.sendOn(address.getActiveAddress());
      42          26 :         } else if (state == Button::Rising) {
      43           7 :             sender.sendOff(address.getActiveAddress());
      44           7 :             address.unlock();
      45           7 :         }
      46          26 :     }
      47             : 
      48             : #ifdef INDIVIDUAL_BUTTON_INVERT
      49             :     void invert() { button.invert(); }
      50             : #endif
      51             : 
      52             :     Button::State getButtonState() const { return button.getState(); }
      53             : 
      54             :   private:
      55             :     BankAddress address;
      56             :     Button button;
      57             : 
      58             :   public:
      59             :     Sender sender;
      60             : };
      61             : 
      62             : } // namespace Bankable
      63             : 
      64             : END_CS_NAMESPACE

Generated by: LCOV version 1.14-5-g4ff2ed6