From e09c21edf18cca79ea0494f51945da7c4744f7b4 Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Tue, 9 Nov 2021 06:11:36 +0000 Subject: [PATCH] Add some additionally debugging, fix the 'ole broken switch statement. --- Button.cpp | 31 ++++++++++++++++++++++--------- Button.h | 11 +++++------ Joystick.cpp | 17 ++++++++++++++++- Joystick.h | 4 +++- 4 files changed, 46 insertions(+), 17 deletions(-) diff --git a/Button.cpp b/Button.cpp index 7ded421..889f86b 100644 --- a/Button.cpp +++ b/Button.cpp @@ -48,10 +48,11 @@ PassthruButton::PassthruButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* m this->type = BUTTON_PASSTHRU; } -void PassthruButton::Update(Joystick* js) { - if (!BouncerUpdate()) return; +bool PassthruButton::Update(Joystick* js) { + if (!BouncerUpdate()) return false; if (On()) js->PressButton(vbutton); else js->ReleaseButton(vbutton); + return true; } @@ -60,8 +61,8 @@ LatchedButton::LatchedButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux this->pressed = false; } -void LatchedButton::Update(Joystick* js) { - if (!BouncerUpdate()) return; +bool LatchedButton::Update(Joystick* js) { + if (!BouncerUpdate()) return false; if (On()) { if (!pressed) { @@ -72,6 +73,8 @@ void LatchedButton::Update(Joystick* js) { pressed = false; } } + + return true; } @@ -88,8 +91,8 @@ PulsedButton::PulsedButton(uint8_t pin, uint8_t vbutton, bool double_action, boo } } -void PulsedButton::Update(Joystick* js) { - if (!BouncerUpdate()) return; +bool PulsedButton::Update(Joystick* js) { + if (!BouncerUpdate()) return false; switch(type) { case BUTTON_PULSED: @@ -103,6 +106,8 @@ void PulsedButton::Update(Joystick* js) { else js->PressButton(vbutton2); break; } + + return true; } void PulsedButton::ReleaseButtons(Joystick* js) { @@ -119,11 +124,19 @@ EncoderButton::EncoderButton(uint8_t pin1, uint8_t pin2, uint8_t vbutton) : Butt this->last_value = encoder->read(); } -void EncoderButton::Update(Joystick* js) { +bool EncoderButton::Update(Joystick* js) { + bool changed = false; long new_value = encoder->read(); - if (new_value > last_value) js->PressButton(vbutton); - else if (new_value < last_value) js->PressButton(vbutton2); + if (new_value > last_value) { + js->PressButton(vbutton); + changed = true; + } + else if (new_value < last_value) { + js->PressButton(vbutton2); + changed = true; + } last_value = new_value; + return changed; } void EncoderButton::ReleaseButtons(Joystick* js) { diff --git a/Button.h b/Button.h index 1e522bc..25e0acf 100644 --- a/Button.h +++ b/Button.h @@ -25,7 +25,7 @@ enum ButtonType { class Button { public: Button(uint8_t vbutton); - virtual void Update(Joystick* js) = 0; + virtual bool Update(Joystick* js) = 0; virtual void ReleaseButtons(Joystick* js); ButtonType type; @@ -41,7 +41,6 @@ class Button { class SwitchButton : public Button { public: SwitchButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux); - virtual void Update(Joystick* js) = 0; bool BouncerUpdate(); // returns true if the pin's status has changed bool On(); @@ -55,13 +54,13 @@ class SwitchButton : public Button { class PassthruButton : public SwitchButton { public: PassthruButton(uint8_t pin, uint8_t vbutton, bool pullup = true, Mux* mux = NULL); - void Update(Joystick* js); + bool Update(Joystick* js); }; class LatchedButton : public SwitchButton { public: LatchedButton(uint8_t pin, uint8_t vbutton, bool pullup = true, Mux* mux = NULL); - void Update(Joystick* js); + bool Update(Joystick* js); protected: bool pressed; @@ -70,7 +69,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); - void Update(Joystick* js); + bool Update(Joystick* js); void ReleaseButtons(Joystick* js); protected: @@ -82,7 +81,7 @@ class PulsedButton : public SwitchButton { class EncoderButton : public Button { public: EncoderButton(uint8_t pin1, uint8_t pin2, uint8_t vbutton); - void Update(Joystick* js); + bool Update(Joystick* js); void ReleaseButtons(Joystick* js); protected: diff --git a/Joystick.cpp b/Joystick.cpp index 9069195..6383abf 100644 --- a/Joystick.cpp +++ b/Joystick.cpp @@ -2,6 +2,7 @@ #include "Button.h" #include #include +#include using namespace admux; @@ -45,21 +46,30 @@ void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup, Mux* mux) { case BUTTON_PASSTHRU: button = new PassthruButton(pin, _virtual_buttons, pullup, mux); _virtual_buttons++; + break; case BUTTON_PULSED: button = new PulsedButton(pin, _virtual_buttons, false, false, pullup, mux); _virtual_buttons++; + break; case BUTTON_PULSED_DOUBLE_ACTION: button = new PulsedButton(pin, _virtual_buttons, true, false, pullup, mux); _virtual_buttons++; + break; case BUTTON_PULSED_DOUBLE_ACTION_SPLIT: button = new PulsedButton(pin, _virtual_buttons, true, true, pullup, mux); _virtual_buttons += 2; + break; default: return; } _buttons[_num_buttons] = button; _num_buttons++; + if (_debug) { + char buffer[100]; + sprintf(buffer, "Added button %d of type %d", _num_buttons - 1, button->type); + Serial.println(buffer); + } if (type & _BUTTON_PULSED_TYPES) _have_pulsed_button = true; } @@ -90,7 +100,12 @@ void Joystick::Update() { JoyReport oldReport = _joyReport; for (uint8_t i = 0; i < _num_buttons; i++) { - _buttons[i]->Update(this); + bool changed = _buttons[i]->Update(this); + if (changed && _debug) { + char buffer[25]; + sprintf(buffer, "Button %d changed state.", i); + Serial.println(buffer); + } } // TODO: implement this and also refactor it into a class or classes diff --git a/Joystick.h b/Joystick.h index 95fb4db..1313043 100644 --- a/Joystick.h +++ b/Joystick.h @@ -46,8 +46,11 @@ class Joystick { // 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); + + // 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); @@ -66,7 +69,6 @@ class Joystick { uint8_t _num_axes; JoyReport _joyReport; - bool _debug; }; #endif