2021-11-08 01:37:30 +00:00
|
|
|
#include "Button.h"
|
|
|
|
#include "Joystick.h"
|
|
|
|
#include <Encoder.h>
|
|
|
|
#include <Mux.h>
|
|
|
|
|
|
|
|
using namespace admux;
|
|
|
|
|
2021-11-22 19:12:44 +00:00
|
|
|
Button::Button(uint8_t vbutton, Reader* reader) {
|
2021-11-08 01:37:30 +00:00
|
|
|
this->vbutton = vbutton;
|
2021-11-22 19:12:44 +00:00
|
|
|
this->reader = reader;
|
2021-11-08 01:37:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Button::ReleaseButtons(Joystick* js) {
|
|
|
|
js->ReleaseButton(vbutton);
|
|
|
|
}
|
|
|
|
|
2021-11-22 19:12:44 +00:00
|
|
|
PassthruButton::PassthruButton(uint8_t vbutton, Reader* reader) : Button(vbutton, reader) {
|
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) {
|
2021-11-22 19:12:44 +00:00
|
|
|
if (!reader->Update()) return false;
|
|
|
|
if (reader->On()) js->PressButton(vbutton);
|
2021-11-08 01:37:30 +00:00
|
|
|
else js->ReleaseButton(vbutton);
|
2021-11-09 06:11:36 +00:00
|
|
|
return true;
|
2021-11-08 01:37:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-22 19:12:44 +00:00
|
|
|
LatchedButton::LatchedButton(uint8_t vbutton, Reader* reader) : Button(vbutton, reader) {
|
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) {
|
2021-11-22 20:10:08 +00:00
|
|
|
if (!reader->Update()) return false;
|
2021-11-08 01:37:30 +00:00
|
|
|
|
2021-11-22 20:10:08 +00:00
|
|
|
if (reader->On()) {
|
2021-11-08 01:37:30 +00:00
|
|
|
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-22 20:10:08 +00:00
|
|
|
PulsedButton::PulsedButton(uint8_t vbutton, Reader* reader, bool double_action, bool split) : Button(vbutton, reader) {
|
2021-11-13 22:48:12 +00:00
|
|
|
this->release_time1 = 0;
|
|
|
|
this->release_time2 = 0;
|
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) {
|
2021-11-13 22:48:12 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-11-22 20:10:08 +00:00
|
|
|
if (!reader->Update()) return false;
|
2021-11-08 01:37:30 +00:00
|
|
|
|
|
|
|
switch(type) {
|
|
|
|
case BUTTON_PULSED:
|
2021-11-22 20:10:08 +00:00
|
|
|
if (reader->On()) js->PressButton(vbutton);
|
2021-11-08 01:37:30 +00:00
|
|
|
break;
|
|
|
|
case BUTTON_PULSED_DOUBLE_ACTION:
|
|
|
|
js->PressButton(vbutton);
|
2021-11-13 22:48:12 +00:00
|
|
|
release_time1 = millis() + 250;
|
2021-11-08 01:37:30 +00:00
|
|
|
break;
|
|
|
|
case BUTTON_PULSED_DOUBLE_ACTION_SPLIT:
|
2021-11-22 20:10:08 +00:00
|
|
|
if (reader->On()) {
|
2021-11-13 22:48:12 +00:00
|
|
|
js->PressButton(vbutton);
|
|
|
|
release_time1 = millis() + 250;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
js->PressButton(vbutton2);
|
|
|
|
release_time2 = millis() + 250;
|
|
|
|
}
|
2021-11-08 01:37:30 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-11-09 06:11:36 +00:00
|
|
|
|
|
|
|
return true;
|
2021-11-08 01:37:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-22 20:10:08 +00:00
|
|
|
EncoderButton::EncoderButton(uint8_t pin1, uint8_t pin2, uint8_t vbutton) : Button(vbutton, NULL) {
|
2021-11-08 01:37:30 +00:00
|
|
|
this->type = ENCODER_PULSED_SPLIT;
|
|
|
|
this->vbutton2 = vbutton + 1;
|
|
|
|
this->encoder = new Encoder(pin1, pin2);
|
|
|
|
this->last_value = encoder->read();
|
2021-11-13 22:48:12 +00:00
|
|
|
this->release_time1 = 0;
|
|
|
|
this->release_time2 = 0;
|
2021-11-08 01:37:30 +00:00
|
|
|
}
|
|
|
|
|
2021-11-09 06:11:36 +00:00
|
|
|
bool EncoderButton::Update(Joystick* js) {
|
2021-11-13 22:48:12 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-11-09 06:11:36 +00:00
|
|
|
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;
|
2021-11-13 22:48:12 +00:00
|
|
|
release_time1 = millis() + 250;
|
2021-11-09 06:11:36 +00:00
|
|
|
}
|
|
|
|
else if (new_value < last_value) {
|
|
|
|
js->PressButton(vbutton2);
|
|
|
|
changed = true;
|
2021-11-13 22:48:12 +00:00
|
|
|
release_time2 = millis() + 250;
|
2021-11-09 06:11:36 +00:00
|
|
|
}
|
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
|
|
|
}
|