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