Add BUTTON_LATCHED_MOMENTARY type, to allow a pushbutton to mimic the behavior of a toggle switch.
This commit is contained in:
parent
c37e4a6789
commit
6a692687f5
12
Joystick.cpp
12
Joystick.cpp
|
@ -50,7 +50,7 @@ void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup) {
|
|||
increment = 2;
|
||||
button.index1 = index + 1;
|
||||
}
|
||||
|
||||
|
||||
if (_last_button_index + increment > JOYSTICK_NUM_BUTTONS) {
|
||||
// todo: fail here
|
||||
}
|
||||
|
@ -155,6 +155,16 @@ void Joystick::_UpdateButton(Button* button) {
|
|||
if (button->bouncer.rose()) PressButton(button->index0);
|
||||
else if (button->bouncer.fell()) PressButton(button->index1);
|
||||
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:
|
||||
if (_debug) {
|
||||
Serial.print("DEBUG: Unhandled button type: ");
|
||||
|
|
|
@ -22,7 +22,8 @@ enum ButtonType {
|
|||
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_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 {
|
||||
|
@ -34,7 +35,8 @@ struct Button {
|
|||
ButtonType type;
|
||||
Bounce bouncer;
|
||||
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.
|
||||
};
|
||||
|
||||
|
|
22
examples/type_test/type_test.ino
Normal file
22
examples/type_test/type_test.ino
Normal 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();
|
||||
}
|
Loading…
Reference in New Issue
Block a user