LCOV - code coverage report
Current view: top level - src/MIDI_Inputs/LEDs - NoteCCKPLED.hpp (source / functions) Hit Total Coverage
Test: 169c36a3797bc662d84b5726f34a3f37d3c58247 Lines: 30 35 85.7 %
Date: 2024-11-09 15:32:27 Functions: 8 10 80.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.15