Fix behavior when using pull-up resistors.

This commit is contained in:
Anna Rose 2021-11-01 22:19:05 +00:00
parent 3fd595c7c2
commit bcfd67b50d
3 changed files with 21 additions and 8 deletions

View File

@ -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;

View File

@ -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}

View File

@ -5,7 +5,7 @@
#include <Bounce2.h>
#include <Joystick.h>
bool debug = true;
bool debug = false;
Joystick joystick(debug);
void setup() {