2021-11-08 01:37:30 +00:00
|
|
|
#include "Button.h"
|
|
|
|
#include "Joystick.h"
|
|
|
|
#include <Encoder.h>
|
|
|
|
#include <Mux.h>
|
|
|
|
|
|
|
|
using namespace admux;
|
|
|
|
|
|
|
|
Button::Button(uint8_t vbutton) {
|
|
|
|
this->vbutton = vbutton;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::ReleaseButtons(Joystick* js) {
|
|
|
|
js->ReleaseButton(vbutton);
|
|
|
|
}
|
|
|
|
|
2021-11-13 05:56:21 +00:00
|
|
|
// 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) {
|
2021-11-08 01:37:30 +00:00
|
|
|
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);
|
2021-11-13 05:56:21 +00:00
|
|
|
delayMicroseconds(500);
|
2021-11-08 01:37:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bouncer.update();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SwitchButton::On() {
|
|
|
|
bool state = bouncer.rose();
|
|
|
|
if (inverted) state = bouncer.fell();
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-13 05:56:21 +00:00
|
|
|
PassthruButton::PassthruButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux, bool analog_only) : SwitchButton(pin, vbutton, pullup, mux, analog_only) {
|
2021-11-08 01:37:30 +00:00
|
|
|
this->type = BUTTON_PASSTHRU;
|
|
|
|
}
|
|
|
|
|
2021-11-09 06:11:36 +00:00
|
|
|
bool PassthruButton::Update(Joystick* js) {
|
|
|
|
if (!BouncerUpdate()) return false;
|
2021-11-08 01:37:30 +00:00
|
|
|
if (On()) js->PressButton(vbutton);
|
|
|
|
else js->ReleaseButton(vbutton);
|
2021-11-09 06:11:36 +00:00
|
|
|
return true;
|
2021-11-08 01:37:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-13 05:56:21 +00:00
|
|
|
LatchedButton::LatchedButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux, bool analog_only) : SwitchButton(pin, vbutton, pullup, mux, analog_only) {
|
2021-11-08 01:37:30 +00:00
|
|
|
this->type = BUTTON_LATCHED_MOMENTARY;
|
|
|
|
this->pressed = false;
|
|
|
|
}
|
|
|
|
|
2021-11-09 06:11:36 +00:00
|
|
|
bool LatchedButton::Update(Joystick* js) {
|
|
|
|
if (!BouncerUpdate()) return false;
|
2021-11-08 01:37:30 +00:00
|
|
|
|
|
|
|
if (On()) {
|
|
|
|
if (!pressed) {
|
|
|
|
js->PressButton(vbutton);
|
|
|
|
pressed = true;
|
|
|
|
} else {
|
|
|
|
js->ReleaseButton(vbutton);
|
|
|
|
pressed = false;
|
|
|
|
}
|
|
|
|
}
|
2021-11-09 06:11:36 +00:00
|
|
|
|
|
|
|
return true;
|
2021-11-08 01:37:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-13 05:56:21 +00:00
|
|
|
PulsedButton::PulsedButton(uint8_t pin, uint8_t vbutton, bool double_action, bool split, bool pullup, Mux* mux, bool analog_only) : SwitchButton(pin, vbutton, pullup, mux, analog_only) {
|
2021-11-08 01:37:30 +00:00
|
|
|
if (double_action) {
|
|
|
|
if (split) {
|
|
|
|
this->type = BUTTON_PULSED_DOUBLE_ACTION_SPLIT;
|
|
|
|
this->vbutton2 = vbutton + 1;
|
|
|
|
} else {
|
|
|
|
this->type = BUTTON_PULSED_DOUBLE_ACTION;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this->type = BUTTON_PULSED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-09 06:11:36 +00:00
|
|
|
bool PulsedButton::Update(Joystick* js) {
|
|
|
|
if (!BouncerUpdate()) return false;
|
2021-11-08 01:37:30 +00:00
|
|
|
|
|
|
|
switch(type) {
|
|
|
|
case BUTTON_PULSED:
|
|
|
|
if (On()) js->PressButton(vbutton);
|
|
|
|
break;
|
|
|
|
case BUTTON_PULSED_DOUBLE_ACTION:
|
|
|
|
js->PressButton(vbutton);
|
|
|
|
break;
|
|
|
|
case BUTTON_PULSED_DOUBLE_ACTION_SPLIT:
|
|
|
|
if (On()) js->PressButton(vbutton);
|
|
|
|
else js->PressButton(vbutton2);
|
|
|
|
break;
|
|
|
|
}
|
2021-11-09 06:11:36 +00:00
|
|
|
|
|
|
|
return true;
|
2021-11-08 01:37:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
this->vbutton2 = vbutton + 1;
|
|
|
|
this->encoder = new Encoder(pin1, pin2);
|
|
|
|
this->last_value = encoder->read();
|
|
|
|
}
|
|
|
|
|
2021-11-09 06:11:36 +00:00
|
|
|
bool EncoderButton::Update(Joystick* js) {
|
|
|
|
bool changed = false;
|
2021-11-08 01:37:30 +00:00
|
|
|
long new_value = encoder->read();
|
2021-11-09 06:11:36 +00:00
|
|
|
if (new_value > last_value) {
|
|
|
|
js->PressButton(vbutton);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
else if (new_value < last_value) {
|
|
|
|
js->PressButton(vbutton2);
|
|
|
|
changed = true;
|
|
|
|
}
|
2021-11-08 01:37:30 +00:00
|
|
|
last_value = new_value;
|
2021-11-09 06:11:36 +00:00
|
|
|
return changed;
|
2021-11-08 01:37:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EncoderButton::ReleaseButtons(Joystick* js) {
|
|
|
|
Button::ReleaseButtons(js);
|
|
|
|
js->ReleaseButton(vbutton2);
|
|
|
|
}
|