Line data Source code
1 : #pragma once 2 : 3 : #include <AH/Hardware/ExtendedInputOutput/ExtendedInputOutput.hpp> 4 : #include <AH/Math/IncreaseBitDepth.hpp> 5 : #include <MIDI_Inputs/NoteCCKPValue.hpp> 6 : 7 : BEGIN_CS_NAMESPACE 8 : 9 : // -------------------------------------------------------------------------- // 10 : 11 : /// Generic base class for classes that listen for MIDI Note, Control Change and 12 : /// Key Pressure events on a single address and turn on an LED with a 13 : /// brightness/duty cycle proportional to the MIDI value. 14 : /// 15 : /// @tparam Type 16 : /// The type of MIDI messages to listen for: 17 : /// - @ref MIDIMessageType::NoteOn 18 : /// - @ref MIDIMessageType::ControlChange 19 : /// - @ref MIDIMessageType::KeyPressure 20 : template <MIDIMessageType Type> 21 : class NoteCCKPLEDPWM 22 : : public MatchingMIDIInputElement<Type, TwoByteMIDIMatcher> { 23 : public: 24 : using Matcher = TwoByteMIDIMatcher; 25 : using Parent = MatchingMIDIInputElement<Type, Matcher>; 26 : 27 : /// @param ledPin 28 : /// The PWM pin with the LED connected. 29 : /// @param address 30 : /// The address to listen to. 31 1 : NoteCCKPLEDPWM(pin_t ledPin, MIDIAddress address) 32 1 : : Parent(address), ledPin(ledPin) {} 33 : 34 : private: 35 3 : void handleUpdate(typename Matcher::Result match) override { 36 3 : auto value = AH::increaseBitDepth<8, 7, uint8_t, uint8_t>(match.value); 37 3 : AH::ExtIO::analogWrite(ledPin, value); 38 3 : } 39 : 40 : public: 41 : /// Set the pinmode of the LED to OUTPUT. 42 1 : void begin() override { 43 1 : AH::ExtIO::pinMode(ledPin, OUTPUT); 44 1 : AH::ExtIO::digitalWrite(ledPin, LOW); 45 1 : } 46 : 47 : /// Turn off the LED. 48 0 : void reset() override { AH::ExtIO::digitalWrite(ledPin, LOW); } 49 : 50 : private: 51 : pin_t ledPin; 52 : }; 53 : 54 : /// Class that listens for MIDI Note events on a single address and turns 55 : /// on an LED with a brightness/duty cycle proportional to the velocity. 56 : /// @ingroup midi-input-elements-leds 57 : using NoteLEDPWM = NoteCCKPLEDPWM<MIDIMessageType::NoteOn>; 58 : 59 : /// Class that listens for MIDI Control Change events on a single address and 60 : /// turns on an LED with a brightness/duty cycle proportional to the value. 61 : /// @ingroup midi-input-elements-leds 62 : using CCLEDPWM = NoteCCKPLEDPWM<MIDIMessageType::ControlChange>; 63 : 64 : /// Class that listens for MIDI Key Pressure events on a single address and 65 : /// turns on an LED with a brightness/duty cycle proportional to the pressure. 66 : /// @ingroup midi-input-elements-leds 67 : using KPLEDPWM = NoteCCKPLEDPWM<MIDIMessageType::KeyPressure>; 68 : 69 : // -------------------------------------------------------------------------- // 70 : 71 : namespace Bankable { 72 : 73 : /// Generic base class for classes that listen for MIDI Note, Control Change and 74 : /// Key Pressure events on a single address and turn on an LED with a 75 : /// brightness/duty cycle proportional to the MIDI value. 76 : /// 77 : /// @tparam Type 78 : /// The type of MIDI messages to listen for: 79 : /// - @ref MIDIMessageType::NoteOn 80 : /// - @ref MIDIMessageType::ControlChange 81 : /// - @ref MIDIMessageType::KeyPressure 82 : /// @tparam BankSize 83 : /// The number of banks. 84 : template <MIDIMessageType Type, uint8_t BankSize> 85 : class NoteCCKPLEDPWM : public NoteCCKPValue<Type, BankSize> { 86 : public: 87 : using Parent = NoteCCKPValue<Type, BankSize>; 88 : using Matcher = typename Parent::Matcher; 89 : 90 : /// Constructor. 91 : /// 92 : /// @param config 93 : /// The bank configuration to use. 94 : /// @param ledPin 95 : /// The PWM pin with the LED connected. 96 : /// @param address 97 : /// The base address to listen to. 98 1 : NoteCCKPLEDPWM(BankConfig<BankSize> config, pin_t ledPin, 99 : MIDIAddress address) 100 1 : : Parent(config, address), ledPin(ledPin) {} 101 : 102 : protected: 103 3 : void handleUpdate(typename Matcher::Result match) override { 104 3 : bool newdirty = Parent::handleUpdateImpl(match); 105 3 : if (newdirty) 106 2 : display(); 107 3 : this->dirty |= newdirty; 108 3 : } 109 : 110 3 : void display() { 111 3 : auto value = AH::increaseBitDepth<8, 7, uint8_t, uint8_t>(getValue()); 112 3 : AH::ExtIO::analogWrite(ledPin, value); 113 3 : } 114 : 115 : public: 116 : /// Set the pinmode of the LED to OUTPUT. 117 1 : void begin() override { 118 1 : AH::ExtIO::pinMode(ledPin, OUTPUT); 119 1 : AH::ExtIO::digitalWrite(ledPin, LOW); 120 1 : } 121 : 122 : /// Reset all values to zero and turn off the LED. 123 0 : void reset() override { 124 0 : Parent::reset(); 125 0 : AH::ExtIO::digitalWrite(ledPin, LOW); 126 0 : } 127 : 128 : using Parent::getValue; 129 : 130 : protected: 131 1 : void onBankSettingChange() override { 132 1 : Parent::onBankSettingChange(); 133 1 : display(); 134 1 : } 135 : 136 : private: 137 : pin_t ledPin; 138 : }; 139 : 140 : /// Class that listens for MIDI Note events on a single address and 141 : /// turns on an LED with a brightness/duty cycle proportional to the velocity. 142 : /// This version listens accross multiple banks. 143 : /// @ingroup BankableMIDIInputElementsLEDs 144 : template <uint8_t BankSize> 145 : using NoteLEDPWM = NoteCCKPLEDPWM<MIDIMessageType::NoteOn, BankSize>; 146 : 147 : /// Class that listens for MIDI Control Change events on a single address and 148 : /// turns on an LED with a brightness/duty cycle proportional to the value. 149 : /// This version listens accross multiple banks. 150 : /// @ingroup BankableMIDIInputElementsLEDs 151 : template <uint8_t BankSize> 152 : using CCLEDPWM = NoteCCKPLEDPWM<MIDIMessageType::ControlChange, BankSize>; 153 : 154 : /// Class that listens for MIDI Key Pressure events on a single address and 155 : /// turns on an LED with a brightness/duty cycle proportional to the pressure. 156 : /// This version listens accross multiple banks. 157 : /// @ingroup BankableMIDIInputElementsLEDs 158 : template <uint8_t BankSize> 159 : using KPLEDPWM = NoteCCKPLEDPWM<MIDIMessageType::KeyPressure, BankSize>; 160 : 161 : } // namespace Bankable 162 : 163 : END_CS_NAMESPACE