51 lines
1.2 KiB
C++
51 lines
1.2 KiB
C++
#pragma once
|
|
|
|
#include <AH/Hardware/Button.hpp>
|
|
#include <Def/Def.hpp>
|
|
#include <MIDI_Outputs/Abstract/MIDIOutputElement.hpp>
|
|
|
|
BEGIN_CS_NAMESPACE
|
|
|
|
template <class Sender>
|
|
class MIDIButtonLatched : public MIDIOutputElement {
|
|
protected:
|
|
MIDIButtonLatched(pin_t pin, MIDIAddress address, const Sender &sender)
|
|
: button(pin), address(address), sender(sender) {}
|
|
|
|
public:
|
|
void begin() final override { button.begin(); }
|
|
void update() final override {
|
|
AH::Button::State state = button.update();
|
|
if (state == AH::Button::Falling)
|
|
toggleState();
|
|
}
|
|
|
|
bool toggleState() {
|
|
setState(!getState());
|
|
return getState();
|
|
}
|
|
|
|
bool getState() const { return state; }
|
|
|
|
void setState(bool state) {
|
|
this->state = state;
|
|
state ? sender.sendOn(address) : sender.sendOff(address);
|
|
}
|
|
|
|
void invert() { button.invert(); }
|
|
AH::Button::State getButtonState() const { return button.getState(); }
|
|
|
|
MIDIAddress getAddress() const { return this->address; }
|
|
void setAddressUnsafe(MIDIAddress address) { this->address = address; }
|
|
|
|
private:
|
|
AH::Button button;
|
|
MIDIAddress address;
|
|
bool state = false;
|
|
|
|
public:
|
|
Sender sender;
|
|
};
|
|
|
|
END_CS_NAMESPACE
|