Add the ability to use a custom delay time for button presses.
This commit is contained in:
parent
70d7bdd3b2
commit
aef35f96a0
18
Button.cpp
18
Button.cpp
|
@ -48,7 +48,8 @@ bool LatchedButton::Update(Joystick* js) {
|
|||
}
|
||||
|
||||
|
||||
PulsedButton::PulsedButton(uint8_t vbutton, Reader* reader, bool double_action, bool split) : Button(vbutton, reader) {
|
||||
PulsedButton::PulsedButton(uint8_t vbutton, Reader* reader, uint8_t release_delay, bool double_action, bool split) : Button(vbutton, reader) {
|
||||
this->release_delay = release_delay;
|
||||
this->release_time1 = 0;
|
||||
this->release_time2 = 0;
|
||||
if (double_action) {
|
||||
|
@ -80,21 +81,21 @@ bool PulsedButton::Update(Joystick* js) {
|
|||
case BUTTON_PULSED:
|
||||
if (reader->On()) {
|
||||
js->PressButton(vbutton);
|
||||
release_time1 = millis() + 250;
|
||||
release_time1 = millis() + release_delay;
|
||||
}
|
||||
break;
|
||||
case BUTTON_PULSED_DOUBLE_ACTION:
|
||||
js->PressButton(vbutton);
|
||||
release_time1 = millis() + 250;
|
||||
release_time1 = millis() + release_delay;
|
||||
break;
|
||||
case BUTTON_PULSED_DOUBLE_ACTION_SPLIT:
|
||||
if (reader->On()) {
|
||||
js->PressButton(vbutton);
|
||||
release_time1 = millis() + 250;
|
||||
release_time1 = millis() + release_delay;
|
||||
}
|
||||
else {
|
||||
js->PressButton(vbutton2);
|
||||
release_time2 = millis() + 250;
|
||||
release_time2 = millis() + release_delay;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
@ -103,11 +104,12 @@ bool PulsedButton::Update(Joystick* js) {
|
|||
}
|
||||
|
||||
|
||||
EncoderButton::EncoderButton(uint8_t pin1, uint8_t pin2, uint8_t vbutton, int8_t tick_threshold) : Button(vbutton, NULL) {
|
||||
EncoderButton::EncoderButton(uint8_t pin1, uint8_t pin2, uint8_t vbutton, int8_t tick_threshold, uint8_t release_delay) : 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_delay = release_delay;
|
||||
this->release_time1 = 0;
|
||||
this->release_time2 = 0;
|
||||
this->ticks = 0;
|
||||
|
@ -135,13 +137,13 @@ bool EncoderButton::Update(Joystick* js) {
|
|||
if (ticks >= tick_threshold) {
|
||||
js->PressButton(vbutton);
|
||||
changed = true;
|
||||
release_time1 = millis() + 250;
|
||||
release_time1 = millis() + release_delay;
|
||||
ticks = 0;
|
||||
}
|
||||
else if (ticks <= tick_threshold * -1) {
|
||||
js->PressButton(vbutton2);
|
||||
changed = true;
|
||||
release_time2 = millis() + 250;
|
||||
release_time2 = millis() + release_delay;
|
||||
ticks = 0;
|
||||
}
|
||||
|
||||
|
|
6
Button.h
6
Button.h
|
@ -50,12 +50,13 @@ class LatchedButton : public Button {
|
|||
|
||||
class PulsedButton : public Button {
|
||||
public:
|
||||
PulsedButton(uint8_t vbutton, Reader* reader, bool double_action = false, bool split = false);
|
||||
PulsedButton(uint8_t vbutton, Reader* reader, uint8_t release_delay, bool double_action = false, bool split = false);
|
||||
bool Update(Joystick* js);
|
||||
|
||||
protected:
|
||||
bool double_action;
|
||||
bool split;
|
||||
uint8_t release_delay;
|
||||
unsigned long release_time1;
|
||||
unsigned long release_time2;
|
||||
uint8_t vbutton2;
|
||||
|
@ -63,13 +64,14 @@ class PulsedButton : public Button {
|
|||
|
||||
class EncoderButton : public Button {
|
||||
public:
|
||||
EncoderButton(uint8_t pin1, uint8_t pin2, uint8_t vbutton, int8_t tick_threshold);
|
||||
EncoderButton(uint8_t pin1, uint8_t pin2, uint8_t vbutton, int8_t tick_threshold, uint8_t release_delay);
|
||||
bool Update(Joystick* js);
|
||||
|
||||
protected:
|
||||
Encoder* encoder;
|
||||
long last_value;
|
||||
uint8_t vbutton2;
|
||||
uint8_t release_delay;
|
||||
unsigned long release_time1;
|
||||
unsigned long release_time2;
|
||||
int8_t ticks;
|
||||
|
|
11
Joystick.cpp
11
Joystick.cpp
|
@ -21,11 +21,12 @@ bool operator !=(JoyReport a, JoyReport b){
|
|||
return !(a == b);
|
||||
}
|
||||
|
||||
Joystick::Joystick(bool debug) {
|
||||
Joystick::Joystick(uint8_t release_delay, bool debug) {
|
||||
_debug = debug;
|
||||
_virtual_buttons = 0;
|
||||
_num_axes = 0;
|
||||
_num_buttons = 0;
|
||||
this->release_delay = release_delay;
|
||||
|
||||
for (uint8_t i=0; i < JOYSTICK_NUM_AXES; i++) {
|
||||
_joyReport.axis[i] = 0;
|
||||
|
@ -58,7 +59,7 @@ void Joystick::AddEncoder(uint8_t pin1, uint8_t pin2, int8_t tick_threshold, But
|
|||
switch (type) {
|
||||
case ENCODER_PULSED_SPLIT:
|
||||
// add an encoder button. _BuildButton() doesn't do everything we need, however...
|
||||
button = new EncoderButton(pin1, pin2, _virtual_buttons, tick_threshold);
|
||||
button = new EncoderButton(pin1, pin2, _virtual_buttons, tick_threshold, release_delay);
|
||||
_buttons[_num_buttons] = button;
|
||||
_num_buttons++;
|
||||
_virtual_buttons += 2;
|
||||
|
@ -157,15 +158,15 @@ void Joystick::_addButton(ButtonType type, Reader* reader) {
|
|||
_virtual_buttons++;
|
||||
break;
|
||||
case BUTTON_PULSED:
|
||||
button = new PulsedButton(_virtual_buttons, reader, false, false);
|
||||
button = new PulsedButton(_virtual_buttons, reader, release_delay, false, false);
|
||||
_virtual_buttons++;
|
||||
break;
|
||||
case BUTTON_PULSED_DOUBLE_ACTION:
|
||||
button = new PulsedButton(_virtual_buttons, reader, true, false);
|
||||
button = new PulsedButton(_virtual_buttons, reader, release_delay, true, false);
|
||||
_virtual_buttons++;
|
||||
break;
|
||||
case BUTTON_PULSED_DOUBLE_ACTION_SPLIT:
|
||||
button = new PulsedButton(_virtual_buttons, reader, true, true);
|
||||
button = new PulsedButton(_virtual_buttons, reader, release_delay, true, true);
|
||||
_virtual_buttons += 2;
|
||||
break;
|
||||
case BUTTON_LATCHED_MOMENTARY:
|
||||
|
|
|
@ -29,7 +29,7 @@ bool operator !=(JoyReport a, JoyReport b);
|
|||
|
||||
class Joystick {
|
||||
public:
|
||||
Joystick(bool debug=false);
|
||||
Joystick(uint8_t release_delay = 250, bool debug=false);
|
||||
void Init();
|
||||
void Update();
|
||||
|
||||
|
@ -78,6 +78,7 @@ class Joystick {
|
|||
JoyReport _joyReport;
|
||||
|
||||
bool _debug;
|
||||
uint8_t release_delay;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -42,7 +42,7 @@ using namespace admux;
|
|||
|
||||
|
||||
bool debug = true;
|
||||
Joystick js(debug);
|
||||
Joystick js(250, debug);
|
||||
|
||||
void setup() {
|
||||
js.Init();
|
||||
|
|
Loading…
Reference in New Issue
Block a user