Add new button type, clean up pull-up resistor logic. #1

Merged
annabunches merged 8 commits from new-button-behaviors into main 2021-11-01 22:29:42 +00:00
3 changed files with 21 additions and 8 deletions
Showing only changes of commit bcfd67b50d - Show all commits

View File

@ -38,6 +38,7 @@ void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup) {
Button button; Button button;
button.type = type; button.type = type;
button.inverted = pullup;
button.bouncer.attach(pin, mode); button.bouncer.attach(pin, mode);
uint8_t increment = 1; uint8_t increment = 1;
button.vbutton0 = _virtual_buttons; button.vbutton0 = _virtual_buttons;
@ -138,24 +139,28 @@ void Joystick::Write() {
void Joystick::_UpdateButton(uint8_t button_num) { void Joystick::_UpdateButton(uint8_t button_num) {
Button *button = &_buttons[button_num]; Button *button = &_buttons[button_num];
bool changed = button->bouncer.update(); bool changed = button->bouncer.update();
if (!changed) return;
bool on = button->bouncer.rose();
if (button->inverted) on = button->bouncer.fell();
switch (button->type) { switch (button->type) {
case BUTTON_PASSTHRU: case BUTTON_PASSTHRU:
if (button->bouncer.rose()) PressButton(button->vbutton0); if (on) PressButton(button->vbutton0);
else if (button->bouncer.fell()) ReleaseButton(button->vbutton0); else ReleaseButton(button->vbutton0);
break; break;
case BUTTON_PULSED: case BUTTON_PULSED:
if (button->bouncer.rose()) PressButton(button->vbutton0); if (on) PressButton(button->vbutton0);
break; break;
case BUTTON_PULSED_DOUBLE_ACTION: case BUTTON_PULSED_DOUBLE_ACTION:
if (changed) PressButton(button->vbutton0); PressButton(button->vbutton0);
break; break;
case BUTTON_PULSED_DOUBLE_ACTION_SPLIT: case BUTTON_PULSED_DOUBLE_ACTION_SPLIT:
if (button->bouncer.rose()) PressButton(button->vbutton0); if (on) PressButton(button->vbutton0);
else if (button->bouncer.fell()) PressButton(button->vbutton1); else PressButton(button->vbutton1);
break; break;
case BUTTON_LATCHED_MOMENTARY: case BUTTON_LATCHED_MOMENTARY:
if (button->bouncer.rose()) { if (on) {
if (!button->pressed) { if (!button->pressed) {
PressButton(button->vbutton0); PressButton(button->vbutton0);
button->pressed = true; 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 <Bounce2.h>
#include <Joystick.h> #include <Joystick.h>
bool debug = true; bool debug = false;
Joystick joystick(debug); Joystick joystick(debug);
void setup() { void setup() {