Factor pin-reading logic into a new set of classes.

This commit is contained in:
2021-11-22 19:12:44 +00:00
parent a4f00a0a23
commit 8ebc1a5523
6 changed files with 182 additions and 109 deletions

View File

@ -5,60 +5,28 @@
using namespace admux;
Button::Button(uint8_t vbutton) {
Button::Button(uint8_t vbutton, Reader* reader) {
this->vbutton = vbutton;
this->reader = reader;
}
void Button::ReleaseButtons(Joystick* js) {
js->ReleaseButton(vbutton);
}
// TODO: make analog_only work... how to handle that with debouncer?
SwitchButton::SwitchButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux, bool analog_only) : Button(vbutton) {
this->mux = mux;
uint8_t mode = INPUT;
if (pullup) {
this->inverted = true;
mode = INPUT_PULLUP;
}
if (mux == NULL) this->bouncer.attach(pin, mode);
else {
this->bouncer.attach(mux->signalPin().pin, mode);
this->channel_id = pin;
}
}
bool SwitchButton::BouncerUpdate() {
if (mux != NULL) {
mux->channel(channel_id);
delayMicroseconds(500);
}
return bouncer.update();
}
bool SwitchButton::On() {
bool state = bouncer.rose();
if (inverted) state = bouncer.fell();
return state;
}
PassthruButton::PassthruButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux, bool analog_only) : SwitchButton(pin, vbutton, pullup, mux, analog_only) {
PassthruButton::PassthruButton(uint8_t vbutton, Reader* reader) : Button(vbutton, reader) {
this->type = BUTTON_PASSTHRU;
}
bool PassthruButton::Update(Joystick* js) {
if (!BouncerUpdate()) return false;
if (On()) js->PressButton(vbutton);
if (!reader->Update()) return false;
if (reader->On()) js->PressButton(vbutton);
else js->ReleaseButton(vbutton);
return true;
}
LatchedButton::LatchedButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux, bool analog_only) : SwitchButton(pin, vbutton, pullup, mux, analog_only) {
LatchedButton::LatchedButton(uint8_t vbutton, Reader* reader) : Button(vbutton, reader) {
this->type = BUTTON_LATCHED_MOMENTARY;
this->pressed = false;
}
@ -131,12 +99,6 @@ bool PulsedButton::Update(Joystick* js) {
return true;
}
void PulsedButton::ReleaseButtons(Joystick* js) {
Button::ReleaseButtons(js);
if (type == BUTTON_PULSED_DOUBLE_ACTION_SPLIT) js->ReleaseButton(vbutton2);
}
EncoderButton::EncoderButton(uint8_t pin1, uint8_t pin2, uint8_t vbutton) : Button(vbutton) {
this->type = ENCODER_PULSED_SPLIT;
@ -173,8 +135,3 @@ bool EncoderButton::Update(Joystick* js) {
last_value = new_value;
return changed;
}
void EncoderButton::ReleaseButtons(Joystick* js) {
Button::ReleaseButtons(js);
js->ReleaseButton(vbutton2);
}