From 145e383bc57cf6a1b4282830f33b692c98e27d06 Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Tue, 21 Dec 2021 04:16:07 +0000 Subject: [PATCH] Add some logic to the encoder to only register a button press after a certain number of inputs. --- Button.cpp | 15 +++++++++++++-- Button.h | 4 +++- Joystick.cpp | 4 ++-- Joystick.h | 2 +- example/example.ino | 2 +- 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/Button.cpp b/Button.cpp index bbea283..cb9a057 100644 --- a/Button.cpp +++ b/Button.cpp @@ -103,13 +103,15 @@ bool PulsedButton::Update(Joystick* js) { } -EncoderButton::EncoderButton(uint8_t pin1, uint8_t pin2, uint8_t vbutton) : Button(vbutton, NULL) { +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) { @@ -126,15 +128,24 @@ bool EncoderButton::Update(Joystick* js) { 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 (new_value < last_value) { + else if (ticks < tick_threshold * -1) { js->PressButton(vbutton2); changed = true; release_time2 = millis() + 250; + ticks = 0; } + last_value = new_value; return changed; } diff --git a/Button.h b/Button.h index 464f85b..2354215 100644 --- a/Button.h +++ b/Button.h @@ -63,7 +63,7 @@ class PulsedButton : public Button { class EncoderButton : public Button { public: - EncoderButton(uint8_t pin1, uint8_t pin2, uint8_t vbutton); + EncoderButton(uint8_t pin1, uint8_t pin2, uint8_t vbutton, int8_t tick_threshold); bool Update(Joystick* js); protected: @@ -72,6 +72,8 @@ class EncoderButton : public Button { uint8_t vbutton2; unsigned long release_time1; unsigned long release_time2; + int8_t ticks; + int8_t tick_threshold; }; #endif diff --git a/Joystick.cpp b/Joystick.cpp index 465c8c5..8f49789 100644 --- a/Joystick.cpp +++ b/Joystick.cpp @@ -53,12 +53,12 @@ void Joystick::AddMatrixButton(uint8_t row, uint8_t col, Matrix* matrix, ButtonT _addButton(type, new MatrixReader(row, col, matrix, pullup)); } -void Joystick::AddEncoder(uint8_t pin1, uint8_t pin2, ButtonType type) { +void Joystick::AddEncoder(uint8_t pin1, uint8_t pin2, int8_t tick_threshold, ButtonType type) { Button *button; 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); + button = new EncoderButton(pin1, pin2, _virtual_buttons, tick_threshold); _buttons[_num_buttons] = button; _num_buttons++; _virtual_buttons += 2; diff --git a/Joystick.h b/Joystick.h index e8c5710..941227a 100644 --- a/Joystick.h +++ b/Joystick.h @@ -49,7 +49,7 @@ class Joystick { void AddMatrixButton(uint8_t row, uint8_t col, Matrix* matrix, ButtonType type, bool pullup=true); // Add a rotary encoder. ENCODER button types allow you to treat an encoder as a momentary button or an axis (TODO) - void AddEncoder(uint8_t pin1, uint8_t pin2, ButtonType type); + void AddEncoder(uint8_t pin1, uint8_t pin2, int8_t tick_threshold, ButtonType type); // Add an analog axis to the joystick. THIS METHOD IS NOT CURRENTLY TESTED OR SUPPORTED. It might work, but probably not. void AddAxis(uint8_t pin); diff --git a/example/example.ino b/example/example.ino index 8992979..a0ad7d0 100644 --- a/example/example.ino +++ b/example/example.ino @@ -48,7 +48,7 @@ void setup() { js.Init(); Serial.println("Adding encoder."); - js.AddEncoder(ENCODER, ENCODER_PULSED_SPLIT); + js.AddEncoder(ENCODER, 3, ENCODER_PULSED_SPLIT); // Different types of button programming are available. BUTTON_PASSTHRU simply passes on the button's // real state, and will be the most common, but you can also change how the button works in software like so...