arduino-joystick/Button.cpp

181 lines
4.2 KiB
C++
Raw Normal View History

#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);
}
// 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) {
this->type = BUTTON_PASSTHRU;
}
bool PassthruButton::Update(Joystick* js) {
if (!BouncerUpdate()) return false;
if (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) {
this->type = BUTTON_LATCHED_MOMENTARY;
this->pressed = false;
}
bool LatchedButton::Update(Joystick* js) {
if (!BouncerUpdate()) return false;
if (On()) {
if (!pressed) {
js->PressButton(vbutton);
pressed = true;
} else {
js->ReleaseButton(vbutton);
pressed = false;
}
}
return true;
}
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) {
this->release_time1 = 0;
this->release_time2 = 0;
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;
}
}
bool PulsedButton::Update(Joystick* js) {
if (release_time1 != 0 && millis() >= release_time1) {
js->ReleaseButton(vbutton);
release_time1 = 0;
}
if (release_time2 != 0 && millis() >= release_time2) {
js->ReleaseButton(vbutton2);
release_time2 = 0;
}
if (!BouncerUpdate()) return false;
switch(type) {
case BUTTON_PULSED:
if (On()) js->PressButton(vbutton);
break;
case BUTTON_PULSED_DOUBLE_ACTION:
js->PressButton(vbutton);
release_time1 = millis() + 250;
break;
case BUTTON_PULSED_DOUBLE_ACTION_SPLIT:
if (On()) {
js->PressButton(vbutton);
release_time1 = millis() + 250;
}
else {
js->PressButton(vbutton2);
release_time2 = millis() + 250;
}
break;
}
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;
this->vbutton2 = vbutton + 1;
this->encoder = new Encoder(pin1, pin2);
this->last_value = encoder->read();
this->release_time1 = 0;
this->release_time2 = 0;
}
bool EncoderButton::Update(Joystick* js) {
if (release_time1 != 0 && millis() >= release_time1) {
js->ReleaseButton(vbutton);
release_time1 = 0;
}
if (release_time2 != 0 && millis() >= release_time2) {
js->ReleaseButton(vbutton2);
release_time2 = 0;
}
bool changed = false;
long new_value = encoder->read();
if (new_value > last_value) {
js->PressButton(vbutton);
changed = true;
release_time1 = millis() + 250;
}
else if (new_value < last_value) {
js->PressButton(vbutton2);
changed = true;
release_time2 = millis() + 250;
}
last_value = new_value;
return changed;
}
void EncoderButton::ReleaseButtons(Joystick* js) {
Button::ReleaseButtons(js);
js->ReleaseButton(vbutton2);
}