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 37 additions and 3 deletions
Showing only changes of commit 6a692687f5 - Show all commits

View File

@ -50,7 +50,7 @@ void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup) {
increment = 2; increment = 2;
button.index1 = index + 1; button.index1 = index + 1;
} }
if (_last_button_index + increment > JOYSTICK_NUM_BUTTONS) { if (_last_button_index + increment > JOYSTICK_NUM_BUTTONS) {
// todo: fail here // todo: fail here
} }
@ -155,6 +155,16 @@ void Joystick::_UpdateButton(Button* button) {
if (button->bouncer.rose()) PressButton(button->index0); if (button->bouncer.rose()) PressButton(button->index0);
else if (button->bouncer.fell()) PressButton(button->index1); else if (button->bouncer.fell()) PressButton(button->index1);
break; break;
case BUTTON_LATCHED_MOMENTARY:
if (button->bouncer.rose()) {
if !(button->pressed) {
PressButton(button->index0);
button->pressed = true;
} else {
ReleaseButton(button->index0);
button->pressed = false;
}
break;
default: default:
if (_debug) { if (_debug) {
Serial.print("DEBUG: Unhandled button type: "); Serial.print("DEBUG: Unhandled button type: ");

View File

@ -22,7 +22,8 @@ enum ButtonType {
BUTTON_PASSTHRU = 0x1, // always use the (debounced) absolute state of the input BUTTON_PASSTHRU = 0x1, // always use the (debounced) absolute state of the input
BUTTON_PULSED = 0x2, // on button press, send an on signal followed immediately by an off signal. BUTTON_PULSED = 0x2, // on button press, send an on signal followed immediately by an off signal.
BUTTON_PULSED_DOUBLE_ACTION = 0x4, // Send a button press twice - once for press and once for release. BUTTON_PULSED_DOUBLE_ACTION = 0x4, // Send a button press twice - once for press and once for release.
BUTTON_PULSED_DOUBLE_ACTION_SPLIT = 0x8 // Send two separate button presses - one button on press, another on release. BUTTON_PULSED_DOUBLE_ACTION_SPLIT = 0x8, // Send two separate button presses - one button on press, another on release.
BUTTON_LATCHED_MOMENTARY = 0x10
}; };
struct JoyReport { struct JoyReport {
@ -34,7 +35,8 @@ struct Button {
ButtonType type; ButtonType type;
Bounce bouncer; Bounce bouncer;
uint8_t index0; uint8_t index0;
uint8_t index1; uint8_t index1; // only used by BUTTON_PULSED_DOUBLE_ACTION_SPLIT
bool pressed = false; // only used by BUTTON_LATCHED_MOMENTARY
bool inverted = false; // if true, send button press on release and vice versa. bool inverted = false; // if true, send button press on release and vice versa.
}; };

View File

@ -0,0 +1,22 @@
// An example sketch using the joystick library.
// In this example, we have 3 toggle switches and 2 momentary pushbuttons.
// Each button is configured in a different one of the available behaviors.
#include <Bounce2.h>
#include <Joystick.h>
bool debug = false;
Joystick joystick(debug);
void setup() {
joystick.AddButton(9, BUTTON_PASSTHRU);
joystick.AddButton(10, BUTTON_LATCHED_MOMENTARY);
joystick.AddButton(11, BUTTON_PULSED);
joystick.AddButton(12, BUTTON_PULSED_DOUBLE_ACTION);
joystick.AddButton(13, BUTTON_PULSED_DOUBLE_ACTION_SPLIT);
joystick.Init();
}
void loop() {
joystick.Update();
}