From bcfd67b50df1cfc2fa324fecee9b59776a6aeb01 Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Mon, 1 Nov 2021 22:19:05 +0000 Subject: [PATCH] Fix behavior when using pull-up resistors. --- Joystick.cpp | 19 ++++++++++++------- examples/type_test/Makefile | 8 ++++++++ examples/type_test/type_test.ino | 2 +- 3 files changed, 21 insertions(+), 8 deletions(-) create mode 100644 examples/type_test/Makefile diff --git a/Joystick.cpp b/Joystick.cpp index 97fcb0e..5b4c99a 100644 --- a/Joystick.cpp +++ b/Joystick.cpp @@ -38,6 +38,7 @@ void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup) { Button button; button.type = type; + button.inverted = pullup; button.bouncer.attach(pin, mode); uint8_t increment = 1; button.vbutton0 = _virtual_buttons; @@ -138,24 +139,28 @@ void Joystick::Write() { void Joystick::_UpdateButton(uint8_t button_num) { Button *button = &_buttons[button_num]; bool changed = button->bouncer.update(); + if (!changed) return; + + bool on = button->bouncer.rose(); + if (button->inverted) on = button->bouncer.fell(); switch (button->type) { case BUTTON_PASSTHRU: - if (button->bouncer.rose()) PressButton(button->vbutton0); - else if (button->bouncer.fell()) ReleaseButton(button->vbutton0); + if (on) PressButton(button->vbutton0); + else ReleaseButton(button->vbutton0); break; case BUTTON_PULSED: - if (button->bouncer.rose()) PressButton(button->vbutton0); + if (on) PressButton(button->vbutton0); break; case BUTTON_PULSED_DOUBLE_ACTION: - if (changed) PressButton(button->vbutton0); + PressButton(button->vbutton0); break; case BUTTON_PULSED_DOUBLE_ACTION_SPLIT: - if (button->bouncer.rose()) PressButton(button->vbutton0); - else if (button->bouncer.fell()) PressButton(button->vbutton1); + if (on) PressButton(button->vbutton0); + else PressButton(button->vbutton1); break; case BUTTON_LATCHED_MOMENTARY: - if (button->bouncer.rose()) { + if (on) { if (!button->pressed) { PressButton(button->vbutton0); button->pressed = true; diff --git a/examples/type_test/Makefile b/examples/type_test/Makefile new file mode 100644 index 0000000..d1058d4 --- /dev/null +++ b/examples/type_test/Makefile @@ -0,0 +1,8 @@ +TARGET_BOARD=arduino:avr:uno +COM_PORT=/dev/ttyACM0 + +build: + arduino-cli compile -b ${TARGET_BOARD} + +upload: + arduino-cli upload -b ${TARGET_BOARD} -p ${COM_PORT} diff --git a/examples/type_test/type_test.ino b/examples/type_test/type_test.ino index 14199bb..03f6e35 100644 --- a/examples/type_test/type_test.ino +++ b/examples/type_test/type_test.ino @@ -5,7 +5,7 @@ #include #include -bool debug = true; +bool debug = false; Joystick joystick(debug); void setup() {