diff --git a/Button.cpp b/Button.cpp index e326f65..82482cd 100644 --- a/Button.cpp +++ b/Button.cpp @@ -81,6 +81,8 @@ bool LatchedButton::Update(Joystick* js) { 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) { + this->release_time1 = 0; + this->release_time2 = 0; if (double_action) { if (split) { this->type = BUTTON_PULSED_DOUBLE_ACTION_SPLIT; @@ -94,6 +96,16 @@ PulsedButton::PulsedButton(uint8_t pin, uint8_t vbutton, bool double_action, boo } bool PulsedButton::Update(Joystick* js) { + if (release_time1 != 0 && millis() >= release_time1) { + js->ReleaseButton(vbutton); + release_time1 = 0; + } + + if (release_time2 != 0 && millis() >= release_time2) { + js->ReleaseButton(vbutton2); + release_time2 = 0; + } + if (!BouncerUpdate()) return false; switch(type) { @@ -102,10 +114,17 @@ bool PulsedButton::Update(Joystick* js) { break; case BUTTON_PULSED_DOUBLE_ACTION: js->PressButton(vbutton); + release_time1 = millis() + 250; break; case BUTTON_PULSED_DOUBLE_ACTION_SPLIT: - if (On()) js->PressButton(vbutton); - else js->PressButton(vbutton2); + if (On()) { + js->PressButton(vbutton); + release_time1 = millis() + 250; + } + else { + js->PressButton(vbutton2); + release_time2 = millis() + 250; + } break; } @@ -124,18 +143,32 @@ EncoderButton::EncoderButton(uint8_t pin1, uint8_t pin2, uint8_t vbutton) : Butt this->vbutton2 = vbutton + 1; this->encoder = new Encoder(pin1, pin2); this->last_value = encoder->read(); + this->release_time1 = 0; + this->release_time2 = 0; } bool EncoderButton::Update(Joystick* js) { + if (release_time1 != 0 && millis() >= release_time1) { + js->ReleaseButton(vbutton); + release_time1 = 0; + } + + if (release_time2 != 0 && millis() >= release_time2) { + js->ReleaseButton(vbutton2); + release_time2 = 0; + } + bool changed = false; long new_value = encoder->read(); if (new_value > last_value) { js->PressButton(vbutton); changed = true; + release_time1 = millis() + 250; } else if (new_value < last_value) { js->PressButton(vbutton2); changed = true; + release_time2 = millis() + 250; } last_value = new_value; return changed; diff --git a/Button.h b/Button.h index 7857f16..bf1912b 100644 --- a/Button.h +++ b/Button.h @@ -75,6 +75,8 @@ class PulsedButton : public SwitchButton { protected: bool double_action; bool split; + unsigned long release_time1; + unsigned long release_time2; uint8_t vbutton2; }; @@ -88,9 +90,8 @@ class EncoderButton : public Button { Encoder* encoder; long last_value; uint8_t vbutton2; + unsigned long release_time1; + unsigned long release_time2; }; -// Internal use only. -#define _BUTTON_PULSED_TYPES (BUTTON_PULSED | BUTTON_PULSED_DOUBLE_ACTION | BUTTON_PULSED_DOUBLE_ACTION_SPLIT | ENCODER_PULSED_SPLIT) - #endif diff --git a/Joystick.cpp b/Joystick.cpp index 9a04bb1..29bf0e3 100644 --- a/Joystick.cpp +++ b/Joystick.cpp @@ -25,7 +25,6 @@ Joystick::Joystick(bool debug) { _virtual_buttons = 0; _num_axes = 0; _num_buttons = 0; - _have_pulsed_button = false; for (uint8_t i=0; i < JOYSTICK_NUM_AXES; i++) { _joyReport.axis[i] = 0; @@ -70,7 +69,6 @@ void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup, Mux* mux, bo 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; } void Joystick::AddEncoder(uint8_t pin1, uint8_t pin2, ButtonType type) { @@ -115,10 +113,6 @@ void Joystick::Update() { if (_joyReport != oldReport) { Write(); - if (_have_pulsed_button) { - _ReleasePulsedButtons(); - Write(); - } } } @@ -147,18 +141,8 @@ void Joystick::ReleaseAllButtons() { } } -void Joystick::_ReleasePulsedButtons() { - for (uint8_t i = 0; i < _num_buttons; i++ ) { - Button* button = _buttons[i]; - if (button->type & _BUTTON_PULSED_TYPES) button->ReleaseButtons(this); - } -} - void Joystick::Write() { - if (!_debug) { - Serial.write((uint8_t *)&_joyReport, sizeof(JoyReport)); - } - else { + if (_debug) { Serial.print("DEBUG: Writing data: "); for (uint8_t i=0; i < JOYSTICK_NUM_AXES; i++) { Serial.print(_joyReport.axis[i]); @@ -169,8 +153,10 @@ void Joystick::Write() { Serial.print(" "); } Serial.println(); + return; } - delay(250); + + Serial.write((uint8_t *)&_joyReport, sizeof(JoyReport)); } diff --git a/Joystick.h b/Joystick.h index 59eb8f6..983c80c 100644 --- a/Joystick.h +++ b/Joystick.h @@ -58,13 +58,11 @@ class Joystick { void ReleaseAllButtons(); void Write(); - void _ReleasePulsedButtons(); void _UpdateAxis(uint8_t index); Button* _buttons[JOYSTICK_NUM_BUTTONS]; uint8_t _num_buttons; uint8_t _virtual_buttons; // a single user-defined button can have multiple virtual buttons. - bool _have_pulsed_button; uint8_t _axes[JOYSTICK_NUM_AXES]; uint8_t _num_axes;