From aef35f96a0cb9f18b9364afd132356f1fb644b63 Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Wed, 22 Dec 2021 22:07:57 +0000 Subject: [PATCH] Add the ability to use a custom delay time for button presses. --- Button.cpp | 18 ++++++++++-------- Button.h | 6 ++++-- Joystick.cpp | 11 ++++++----- Joystick.h | 3 ++- example/example.ino | 2 +- 5 files changed, 23 insertions(+), 17 deletions(-) diff --git a/Button.cpp b/Button.cpp index b47ed9c..646b3ed 100644 --- a/Button.cpp +++ b/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; } diff --git a/Button.h b/Button.h index 2354215..3009798 100644 --- a/Button.h +++ b/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; diff --git a/Joystick.cpp b/Joystick.cpp index 8f49789..71916eb 100644 --- a/Joystick.cpp +++ b/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: diff --git a/Joystick.h b/Joystick.h index 941227a..7819258 100644 --- a/Joystick.h +++ b/Joystick.h @@ -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 diff --git a/example/example.ino b/example/example.ino index a0ad7d0..c184ee6 100644 --- a/example/example.ino +++ b/example/example.ino @@ -42,7 +42,7 @@ using namespace admux; bool debug = true; -Joystick js(debug); +Joystick js(250, debug); void setup() { js.Init();