LCOV - code coverage report
Current view: top level - src/MIDI_Outputs/Bankable/Abstract - MIDIFilteredAnalog.hpp (source / functions) Hit Total Coverage
Test: 19d2efc7037c2e176feca44750a12594c76f466f Lines: 6 16 37.5 %
Date: 2019-11-24 14:50:27 Functions: 8 20 40.0 %
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/FilteredAnalog.hpp>
       6             : #include <MIDI_Outputs/Abstract/MIDIOutputElement.hpp>
       7             : 
       8             : BEGIN_CS_NAMESPACE
       9             : 
      10             : namespace Bankable {
      11             : 
      12             : /**
      13             :  * @brief   A class for potentiometers and faders that send MIDI events and that
      14             :  *          can be added to a Bank.
      15             :  *
      16             :  * The analog input is filtered and hysteresis is applied.
      17             :  *
      18             :  * @see     FilteredAnalog
      19             :  */
      20             : template <class BankAddress, class Sender>
      21           2 : class MIDIFilteredAnalogAddressable : public MIDIOutputElement {
      22             :   protected:
      23             :     /**
      24             :      * @brief   Construct a new MIDIFilteredAnalog.
      25             :      *
      26             :      * @param   bankAddress
      27             :      *          The bankable MIDI address to send to.
      28             :      * @param   analogPin
      29             :      *          The analog input pin with the wiper of the potentiometer
      30             :      *          connected.
      31             :      * @param   sender
      32             :      *          The MIDI sender to use.
      33             :      */
      34           2 :     MIDIFilteredAnalogAddressable(const BankAddress &bankAddress,
      35             :                                   pin_t analogPin, const Sender &sender)
      36           4 :         : address{bankAddress}, filteredAnalog{analogPin}, sender{sender} {}
      37             : 
      38             :   public:
      39           0 :     void begin() override {}
      40           0 :     void update() override {
      41           0 :         if (filteredAnalog.update())
      42           0 :             sender.send(filteredAnalog.getValue(), address.getActiveAddress());
      43           0 :     }
      44             : 
      45             :     /**
      46             :      * @brief   Specify a mapping function that is applied to the raw
      47             :      *          analog value before sending.
      48             :      *
      49             :      * @param   fn
      50             :      *          A function pointer to the mapping function. This function
      51             :      *          should take the filtered analog value of @f$ 16 - 
      52             :      *          \mathrm{ANALOG\_FILTER\_SHIFT\_FACTOR} @f$ bits as a parameter, 
      53             :      *          and should return a value in the same range.
      54             :      * 
      55             :      * @see     FilteredAnalog::map     
      56             :      */
      57             :     void map(MappingFunction fn) { filteredAnalog.map(fn); }
      58             : 
      59             :     /// Invert the analog value.
      60             :     void invert() { filteredAnalog.invert(); } 
      61             : 
      62             :     /**
      63             :      * @brief   Get the raw value of the analog input (this is the value 
      64             :      *          without applying the filter or the mapping function first).
      65             :      */
      66             :     analog_t getRawValue() const { return filteredAnalog.getRawValue(); }
      67             : 
      68             :     /**
      69             :      * @brief   Get the value of the analog input (this is the value after first
      70             :      *          applying the mapping function).
      71             :      */
      72             :     analog_t getValue() const { return filteredAnalog.getValue(); }
      73             : 
      74             :   private:
      75             :     BankAddress address;
      76             :     FilteredAnalog<Sender::precision()> filteredAnalog;
      77             : 
      78             :   public:
      79             :     Sender sender;
      80             : };
      81             : 
      82             : // -------------------------------------------------------------------------- //
      83             : 
      84             : /**
      85             :  * @brief   A class for potentiometers and faders that send MIDI events (with
      86             :  *          only a channel, no address) and that can be added to a Bank.
      87             :  *
      88             :  * The analog input is filtered and hysteresis is applied.
      89             :  *
      90             :  * @see     FilteredAnalog
      91             :  */
      92             : template <class BankAddress, class Sender>
      93           2 : class MIDIFilteredAnalog : public MIDIOutputElement {
      94             :   protected:
      95             :     /**
      96             :      * @brief   Construct a new MIDIFilteredAnalog.
      97             :      *
      98             :      * @param   bankAddress
      99             :      *          The bankable MIDI address to send to.
     100             :      * @param   analogPin
     101             :      *          The analog input pin with the wiper of the potentiometer
     102             :      *          connected.
     103             :      * @param   sender
     104             :      *          The MIDI sender to use.
     105             :      */
     106           2 :     MIDIFilteredAnalog(const BankAddress &bankAddress, pin_t analogPin,
     107             :                        const Sender &sender)
     108           4 :         : address{bankAddress}, filteredAnalog{analogPin}, sender{sender} {}
     109             : 
     110             :   public:
     111           0 :     void begin() final override {}
     112           0 :     void update() final override {
     113           0 :         if (filteredAnalog.update())
     114           0 :             sender.send(filteredAnalog.getValue(), address.getActiveAddress());
     115           0 :     }
     116             : 
     117             :     /**
     118             :      * @brief   Specify a mapping function that is applied to the raw
     119             :      *          analog value before sending.
     120             :      *
     121             :      * @param   fn
     122             :      *          A function pointer to the mapping function. This function
     123             :      *          should take the filtered analog value of @f$ 16 - 
     124             :      *          \mathrm{ANALOG\_FILTER\_SHIFT\_FACTOR} @f$ bits as a parameter, 
     125             :      *          and should return a value in the same range.
     126             :      * 
     127             :      * @see     FilteredAnalog::map     
     128             :      */
     129             :     void map(MappingFunction fn) { filteredAnalog.map(fn); }
     130             : 
     131             :     /// Invert the analog value.
     132             :     void invert() { filteredAnalog.invert(); }
     133             : 
     134             :     /**
     135             :      * @brief   Get the raw value of the analog input (this is the value 
     136             :      *          without applying the filter or the mapping function first).
     137             :      */
     138             :     analog_t getRawValue() const { return filteredAnalog.getRawValue(); }
     139             : 
     140             :     /**
     141             :      * @brief   Get the value of the analog input (this is the value after first
     142             :      *          applying the mapping function).
     143             :      */
     144             :     analog_t getValue() const { return filteredAnalog.getValue(); }
     145             : 
     146             :   private:
     147             :     BankAddress address;
     148             :     FilteredAnalog<Sender::precision()> filteredAnalog;
     149             : 
     150             :   public:
     151             :     Sender sender;
     152             : };
     153             : 
     154             : } // namespace Bankable
     155             : 
     156             : END_CS_NAMESPACE

Generated by: LCOV version 1.14-5-g4ff2ed6