From e8a1235a95fe613d89bd4ff7e6e1a88a4a4c1eb9 Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Sat, 13 Nov 2021 05:56:21 +0000 Subject: [PATCH] Add analog_only option, though this is a stub for now. --- Button.cpp | 10 ++++++---- Button.h | 8 ++++---- Joystick.cpp | 10 +++++----- Joystick.h | 7 +++++-- 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/Button.cpp b/Button.cpp index 889f86b..e326f65 100644 --- a/Button.cpp +++ b/Button.cpp @@ -13,7 +13,8 @@ void Button::ReleaseButtons(Joystick* js) { js->ReleaseButton(vbutton); } -SwitchButton::SwitchButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux) : Button(vbutton) { +// TODO: make analog_only work... how to handle that with debouncer? +SwitchButton::SwitchButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux, bool analog_only) : Button(vbutton) { this->mux = mux; uint8_t mode = INPUT; @@ -32,6 +33,7 @@ SwitchButton::SwitchButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux) bool SwitchButton::BouncerUpdate() { if (mux != NULL) { mux->channel(channel_id); + delayMicroseconds(500); } return bouncer.update(); @@ -44,7 +46,7 @@ bool SwitchButton::On() { } -PassthruButton::PassthruButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux) : SwitchButton(pin, vbutton, pullup, mux) { +PassthruButton::PassthruButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux, bool analog_only) : SwitchButton(pin, vbutton, pullup, mux, analog_only) { this->type = BUTTON_PASSTHRU; } @@ -56,7 +58,7 @@ bool PassthruButton::Update(Joystick* js) { } -LatchedButton::LatchedButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux) : SwitchButton(pin, vbutton, pullup, mux) { +LatchedButton::LatchedButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux, bool analog_only) : SwitchButton(pin, vbutton, pullup, mux, analog_only) { this->type = BUTTON_LATCHED_MOMENTARY; this->pressed = false; } @@ -78,7 +80,7 @@ bool LatchedButton::Update(Joystick* js) { } -PulsedButton::PulsedButton(uint8_t pin, uint8_t vbutton, bool double_action, bool split, bool pullup, Mux* mux) : SwitchButton(pin, vbutton, pullup, mux) { +PulsedButton::PulsedButton(uint8_t pin, uint8_t vbutton, bool double_action, bool split, bool pullup, Mux* mux, bool analog_only) : SwitchButton(pin, vbutton, pullup, mux, analog_only) { if (double_action) { if (split) { this->type = BUTTON_PULSED_DOUBLE_ACTION_SPLIT; diff --git a/Button.h b/Button.h index 25e0acf..7857f16 100644 --- a/Button.h +++ b/Button.h @@ -40,7 +40,7 @@ class Button { // and the multiplexer logic will be automatically invoked by Update() class SwitchButton : public Button { public: - SwitchButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux); + SwitchButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux, bool analog_only); bool BouncerUpdate(); // returns true if the pin's status has changed bool On(); @@ -53,13 +53,13 @@ class SwitchButton : public Button { class PassthruButton : public SwitchButton { public: - PassthruButton(uint8_t pin, uint8_t vbutton, bool pullup = true, Mux* mux = NULL); + PassthruButton(uint8_t pin, uint8_t vbutton, bool pullup = true, Mux* mux = NULL, bool analog_only = false); bool Update(Joystick* js); }; class LatchedButton : public SwitchButton { public: - LatchedButton(uint8_t pin, uint8_t vbutton, bool pullup = true, Mux* mux = NULL); + LatchedButton(uint8_t pin, uint8_t vbutton, bool pullup = true, Mux* mux = NULL, bool analog_only = false); bool Update(Joystick* js); protected: @@ -68,7 +68,7 @@ class LatchedButton : public SwitchButton { class PulsedButton : public SwitchButton { public: - PulsedButton(uint8_t pin, uint8_t vbutton, bool double_action = false, bool split = false, bool pullup = true, Mux* mux = NULL); + PulsedButton(uint8_t pin, uint8_t vbutton, bool double_action = false, bool split = false, bool pullup = true, Mux* mux = NULL, bool analog_only = false); bool Update(Joystick* js); void ReleaseButtons(Joystick* js); diff --git a/Joystick.cpp b/Joystick.cpp index 6383abf..9a04bb1 100644 --- a/Joystick.cpp +++ b/Joystick.cpp @@ -40,23 +40,23 @@ void Joystick::Init() { delay(100); } -void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup, Mux* mux) { +void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup, Mux* mux, bool analog_only) { Button *button; switch (type) { case BUTTON_PASSTHRU: - button = new PassthruButton(pin, _virtual_buttons, pullup, mux); + button = new PassthruButton(pin, _virtual_buttons, pullup, mux, analog_only); _virtual_buttons++; break; case BUTTON_PULSED: - button = new PulsedButton(pin, _virtual_buttons, false, false, pullup, mux); + button = new PulsedButton(pin, _virtual_buttons, false, false, pullup, mux, analog_only); _virtual_buttons++; break; case BUTTON_PULSED_DOUBLE_ACTION: - button = new PulsedButton(pin, _virtual_buttons, true, false, pullup, mux); + button = new PulsedButton(pin, _virtual_buttons, true, false, pullup, mux, analog_only); _virtual_buttons++; break; case BUTTON_PULSED_DOUBLE_ACTION_SPLIT: - button = new PulsedButton(pin, _virtual_buttons, true, true, pullup, mux); + button = new PulsedButton(pin, _virtual_buttons, true, true, pullup, mux, analog_only); _virtual_buttons += 2; break; default: diff --git a/Joystick.h b/Joystick.h index 1313043..59eb8f6 100644 --- a/Joystick.h +++ b/Joystick.h @@ -38,7 +38,9 @@ class Joystick { // Button types are documented in the ButtonType enum. // If `pullup` is true, your button should connect the pin to ground. (also be sure that your board supports INPUT_PULLUP on that pin) // If `pullup` is false, your button should connect the pin to VCC. - void AddButton(uint8_t pin, ButtonType type, bool pullup=true, Mux* mux=NULL); + // Setting `analogOnly` to true indicates your button *must* be read with analog code, such as the A6 and A7 pins + // on the Arduino Nano. + void AddButton(uint8_t pin, ButtonType type, bool pullup=true, Mux* mux=NULL, bool analog_only=false); // 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); @@ -50,7 +52,6 @@ class Joystick { // These members should not be used by end users; todo: remember how friend classes work void PressButton(uint8_t button); void ReleaseButton(uint8_t button); - bool _debug; private: void SetAxis(uint8_t axis, int16_t value); @@ -69,6 +70,8 @@ class Joystick { uint8_t _num_axes; JoyReport _joyReport; + + bool _debug; }; #endif