diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d6c2e45 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.elf +*.hex diff --git a/Joystick.cpp b/Joystick.cpp index 57c76b5..5b4c99a 100644 --- a/Joystick.cpp +++ b/Joystick.cpp @@ -17,16 +17,13 @@ bool operator !=(JoyReport a, JoyReport b){ Joystick::Joystick(bool debug) { _debug = debug; - _num_buttons = 0; + _virtual_buttons = 0; _num_axes = 0; _have_pulsed_button = false; for (uint8_t i=0; i < JOYSTICK_NUM_AXES; i++) { _joyReport.axis[i] = 0; } - for (uint8_t i=0; i < JOYSTICK_NUM_BYTES; i++) { - _joyReport.button[i] = 0; - } } void Joystick::Init() { @@ -41,10 +38,23 @@ 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; + + if (type == BUTTON_PULSED_DOUBLE_ACTION_SPLIT) { + increment = 2; + button.vbutton1 = _virtual_buttons + 1; + } + + if (_virtual_buttons + increment > JOYSTICK_NUM_BUTTONS) { + // todo: fail here + } _buttons[_num_buttons] = button; _num_buttons++; + _virtual_buttons += increment; if (type & _BUTTON_PULSED_TYPES) _have_pulsed_button = true; } @@ -57,11 +67,11 @@ void Joystick::AddAxis(uint8_t pin) { void Joystick::Update() { JoyReport oldReport = _joyReport; - for (int i = 0; i < _num_buttons; i++) { + for (uint8_t i = 0; i < _num_buttons; i++) { _UpdateButton(i); } - for (int i = 0; i < _num_axes; i++) { + for (uint8_t i = 0; i < _num_axes; i++) { _UpdateAxis(i); } @@ -100,8 +110,10 @@ void Joystick::ReleaseAllButtons() { } void Joystick::_ReleasePulsedButtons() { - for (uint8_t i = 0; i < _num_buttons; i++) { - if (_buttons[i].type & _BUTTON_PULSED_TYPES) ReleaseButton(i); + for (uint8_t i = 0; i < _num_buttons; i++ ) { + Button button = _buttons[i]; + if (button.type & _BUTTON_PULSED_TYPES) ReleaseButton(button.vbutton0); + if (button.type & BUTTON_PULSED_DOUBLE_ACTION_SPLIT) ReleaseButton(button.vbutton1); } } @@ -124,20 +136,39 @@ void Joystick::Write() { delay(250); } -void Joystick::_UpdateButton(uint8_t index) { - Button* button = &_buttons[index]; +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_LATCHED: - if (button->bouncer.rose()) PressButton(index); - else if (button->bouncer.fell()) ReleaseButton(index); + case BUTTON_PASSTHRU: + if (on) PressButton(button->vbutton0); + else ReleaseButton(button->vbutton0); break; case BUTTON_PULSED: - if (button->bouncer.rose()) PressButton(index); + if (on) PressButton(button->vbutton0); break; case BUTTON_PULSED_DOUBLE_ACTION: - if (changed) PressButton(index); + PressButton(button->vbutton0); + break; + case BUTTON_PULSED_DOUBLE_ACTION_SPLIT: + if (on) PressButton(button->vbutton0); + else PressButton(button->vbutton1); + break; + case BUTTON_LATCHED_MOMENTARY: + if (on) { + if (!button->pressed) { + PressButton(button->vbutton0); + button->pressed = true; + } else { + ReleaseButton(button->vbutton0); + button->pressed = false; + } + } break; default: if (_debug) { diff --git a/Joystick.h b/Joystick.h index d1d3af6..9802925 100644 --- a/Joystick.h +++ b/Joystick.h @@ -16,9 +16,11 @@ #define JOYSTICK_NUM_BYTES (JOYSTICK_NUM_BUTTONS+7)/8 enum ButtonType { - BUTTON_LATCHED = 0x1, - BUTTON_PULSED = 0x2, - BUTTON_PULSED_DOUBLE_ACTION = 0x4 + 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_LATCHED_MOMENTARY = 0x10 }; struct JoyReport { @@ -29,6 +31,10 @@ struct JoyReport { struct Button { ButtonType type; Bounce bouncer; + uint8_t vbutton0; + uint8_t vbutton1; // 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 operator ==(JoyReport a, JoyReport b); @@ -40,10 +46,10 @@ class Joystick { void Init(); void Update(); - void AddButton(uint8_t pin, ButtonType type, bool pullup=false); + void AddButton(uint8_t pin, ButtonType type, bool pullup=true); void AddAxis(uint8_t pin); // Axes don't actually work yet! - // These functions are deprecated and may become private in a future + // Public access to these functions is deprecated and they may become private in a future // version. Prefer the above API instead. void SetAxis(uint8_t axis, int16_t value); void PressButton(uint8_t button); @@ -58,6 +64,7 @@ class Joystick { Button _buttons[JOYSTICK_NUM_BUTTONS]; uint8_t _num_buttons; + uint8_t _virtual_buttons; // a single user-defined button can have multiple virtual buttons. bool _have_pulsed_button; uint8_t _axes[JOYSTICK_NUM_AXES]; @@ -68,6 +75,6 @@ class Joystick { }; // Internal use only. -#define _BUTTON_PULSED_TYPES (BUTTON_PULSED | BUTTON_PULSED_DOUBLE_ACTION) +#define _BUTTON_PULSED_TYPES (BUTTON_PULSED | BUTTON_PULSED_DOUBLE_ACTION | BUTTON_PULSED_DOUBLE_ACTION_SPLIT) #endif diff --git a/Readme.md b/Readme.md index 845e609..3d12ea6 100644 --- a/Readme.md +++ b/Readme.md @@ -2,7 +2,7 @@ This is a library that builds and sends USB HID Joystick reports, making it easy to build USB Joysticks with Arduino. ## Dependencies -* Your Arduino's USB communication chip be programmed with the arduino-big-joystick firmware (or similar). See for more info. +* Your Arduino's USB communication chip must be programmed with the arduino-big-joystick firmware (or similar). See for more info. * The Bounce2 library, available at . ## Installation diff --git a/examples/type_test/Makefile b/examples/type_test/Makefile new file mode 100644 index 0000000..d1058d4 --- /dev/null +++ b/examples/type_test/Makefile @@ -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} 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(); +}