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