arduino-joystick/Button.cpp

152 lines
3.4 KiB
C++

#include "Button.h"
#include "Joystick.h"
#include <Encoder.h>
#include <Mux.h>
using namespace admux;
Button::Button(uint8_t vbutton, Reader* reader) {
this->vbutton = vbutton;
this->reader = reader;
}
void Button::ReleaseButtons(Joystick* js) {
js->ReleaseButton(vbutton);
}
PassthruButton::PassthruButton(uint8_t vbutton, Reader* reader) : Button(vbutton, reader) {
this->type = BUTTON_PASSTHRU;
}
bool PassthruButton::Update(Joystick* js) {
if (!reader->Update()) return false;
if (reader->On()) js->PressButton(vbutton);
else js->ReleaseButton(vbutton);
return true;
}
LatchedButton::LatchedButton(uint8_t vbutton, Reader* reader) : Button(vbutton, reader) {
this->type = BUTTON_LATCHED_MOMENTARY;
this->pressed = false;
}
bool LatchedButton::Update(Joystick* js) {
if (!reader->Update()) return false;
if (reader->On()) {
if (!pressed) {
js->PressButton(vbutton);
pressed = true;
} else {
js->ReleaseButton(vbutton);
pressed = false;
}
}
return true;
}
PulsedButton::PulsedButton(uint8_t vbutton, Reader* reader, bool double_action, bool split) : Button(vbutton, reader) {
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 (!reader->Update()) return false;
switch(type) {
case BUTTON_PULSED:
if (reader->On()) {
js->PressButton(vbutton);
release_time1 = millis() + 250;
}
break;
case BUTTON_PULSED_DOUBLE_ACTION:
js->PressButton(vbutton);
release_time1 = millis() + 250;
break;
case BUTTON_PULSED_DOUBLE_ACTION_SPLIT:
if (reader->On()) {
js->PressButton(vbutton);
release_time1 = millis() + 250;
}
else {
js->PressButton(vbutton2);
release_time2 = millis() + 250;
}
break;
}
return true;
}
EncoderButton::EncoderButton(uint8_t pin1, uint8_t pin2, uint8_t vbutton, int8_t tick_threshold) : Button(vbutton, NULL) {
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;
this->ticks = 0;
this->tick_threshold = tick_threshold;
}
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) {
ticks++;
} else if (new_value < last_value) {
ticks--;
}
if (ticks >= tick_threshold) {
js->PressButton(vbutton);
changed = true;
release_time1 = millis() + 250;
ticks = 0;
}
else if (ticks <= tick_threshold * -1) {
js->PressButton(vbutton2);
changed = true;
release_time2 = millis() + 250;
ticks = 0;
}
last_value = new_value;
return changed;
}