diff --git a/Joystick.cpp b/Joystick.cpp index 738f423..9712454 100644 --- a/Joystick.cpp +++ b/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: "); diff --git a/Joystick.h b/Joystick.h index 8d95820..32cc0a4 100644 --- a/Joystick.h +++ b/Joystick.h @@ -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. }; diff --git a/examples/type_test/type_test.ino b/examples/type_test/type_test.ino new file mode 100644 index 0000000..03f6e35 --- /dev/null +++ b/examples/type_test/type_test.ino @@ -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 +#include + +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(); +}