cs-midi/MIDI_Inputs/LEDs/NoteCCKPLED.hpp

104 lines
2.8 KiB
C++

#pragma once
#include <AH/Hardware/ExtendedInputOutput/ExtendedInputOutput.hpp>
#include <MIDI_Inputs/NoteCCKPValue.hpp>
BEGIN_CS_NAMESPACE
template <MIDIMessageType Type>
class NoteCCKPLED : public MatchingMIDIInputElement<Type, TwoByteMIDIMatcher> {
public:
using Matcher = TwoByteMIDIMatcher;
using Parent = MatchingMIDIInputElement<Type, Matcher>;
NoteCCKPLED(pin_t ledPin, MIDIAddress address)
: Parent(address), ledPin(ledPin) {}
private:
void handleUpdate(typename Matcher::Result match) override {
PinStatus_t state = match.value >= threshold ? HIGH : LOW;
AH::ExtIO::digitalWrite(ledPin, state);
}
public:
void begin() override {
AH::ExtIO::pinMode(ledPin, OUTPUT);
AH::ExtIO::digitalWrite(ledPin, LOW);
}
void reset() override { AH::ExtIO::digitalWrite(ledPin, LOW); }
uint8_t getThreshold() const { return threshold; }
void setThreshold(uint8_t threshold) { this->threshold = threshold; }
private:
pin_t ledPin;
uint8_t threshold = 0x01;
};
using NoteLED = NoteCCKPLED<MIDIMessageType::NoteOn>;
using CCLED = NoteCCKPLED<MIDIMessageType::ControlChange>;
using KPLED = NoteCCKPLED<MIDIMessageType::KeyPressure>;
namespace Bankable {
template <MIDIMessageType Type, uint8_t BankSize>
class NoteCCKPLED : public NoteCCKPValue<Type, BankSize> {
public:
using Parent = NoteCCKPValue<Type, BankSize>;
using Matcher = typename Parent::Matcher;
NoteCCKPLED(BankConfig<BankSize> config, pin_t ledPin, MIDIAddress address)
: Parent(config, address), ledPin(ledPin) {}
protected:
void handleUpdate(typename Matcher::Result match) override {
bool newdirty = Parent::handleUpdateImpl(match);
if (newdirty)
display();
this->dirty |= newdirty;
}
void display() {
PinStatus_t state = getValue() >= threshold ? HIGH : LOW;
AH::ExtIO::digitalWrite(ledPin, state);
}
public:
void begin() override {
AH::ExtIO::pinMode(ledPin, OUTPUT);
AH::ExtIO::digitalWrite(ledPin, LOW);
}
void reset() override {
Parent::reset();
AH::ExtIO::digitalWrite(ledPin, LOW);
}
using Parent::getValue;
uint8_t getThreshold() const { return threshold; }
void setThreshold(uint8_t threshold) { this->threshold = threshold; }
protected:
void onBankSettingChange() override {
Parent::onBankSettingChange();
display();
}
private:
pin_t ledPin;
uint8_t threshold = 0x01;
};
template <uint8_t BankSize>
using NoteLED = NoteCCKPLED<MIDIMessageType::NoteOn, BankSize>;
template <uint8_t BankSize>
using CCLED = NoteCCKPLED<MIDIMessageType::ControlChange, BankSize>;
template <uint8_t BankSize>
using KPLED = NoteCCKPLED<MIDIMessageType::KeyPressure, BankSize>;
} // namespace Bankable
END_CS_NAMESPACE