133 lines
3.1 KiB
C++
133 lines
3.1 KiB
C++
|
#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);
|
||
|
}
|
||
|
|
||
|
SwitchButton::SwitchButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux) : 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);
|
||
|
}
|
||
|
|
||
|
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) : SwitchButton(pin, vbutton, pullup, mux) {
|
||
|
this->type = BUTTON_PASSTHRU;
|
||
|
}
|
||
|
|
||
|
void PassthruButton::Update(Joystick* js) {
|
||
|
if (!BouncerUpdate()) return;
|
||
|
if (On()) js->PressButton(vbutton);
|
||
|
else js->ReleaseButton(vbutton);
|
||
|
}
|
||
|
|
||
|
|
||
|
LatchedButton::LatchedButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux) : SwitchButton(pin, vbutton, pullup, mux) {
|
||
|
this->type = BUTTON_LATCHED_MOMENTARY;
|
||
|
this->pressed = false;
|
||
|
}
|
||
|
|
||
|
void LatchedButton::Update(Joystick* js) {
|
||
|
if (!BouncerUpdate()) return;
|
||
|
|
||
|
if (On()) {
|
||
|
if (!pressed) {
|
||
|
js->PressButton(vbutton);
|
||
|
pressed = true;
|
||
|
} else {
|
||
|
js->ReleaseButton(vbutton);
|
||
|
pressed = false;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
PulsedButton::PulsedButton(uint8_t pin, uint8_t vbutton, bool double_action, bool split, bool pullup, Mux* mux) : SwitchButton(pin, vbutton, pullup, mux) {
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void PulsedButton::Update(Joystick* js) {
|
||
|
if (!BouncerUpdate()) return;
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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();
|
||
|
}
|
||
|
|
||
|
void EncoderButton::Update(Joystick* js) {
|
||
|
long new_value = encoder->read();
|
||
|
if (new_value > last_value) js->PressButton(vbutton);
|
||
|
else if (new_value < last_value) js->PressButton(vbutton2);
|
||
|
last_value = new_value;
|
||
|
}
|
||
|
|
||
|
void EncoderButton::ReleaseButtons(Joystick* js) {
|
||
|
Button::ReleaseButtons(js);
|
||
|
js->ReleaseButton(vbutton2);
|
||
|
}
|