Add new behavior for a button that sends separate keypresses on press and release. This required a refactor of the button abstraction as well.

This commit is contained in:
Anna Rose 2021-11-01 14:49:53 -04:00
parent 9234060532
commit c37e4a6789
3 changed files with 49 additions and 26 deletions

View File

@ -1,5 +1,8 @@
#include "Joystick.h"
#include <Arduino.h>
#include <list>
using std::list;
bool operator ==(JoyReport a, JoyReport b){
for (uint8_t i=0; i < JOYSTICK_NUM_AXES; i++) {
@ -17,16 +20,13 @@ bool operator !=(JoyReport a, JoyReport b){
Joystick::Joystick(bool debug) {
_debug = debug;
_num_buttons = 0;
_last_button_index = 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() {
@ -42,9 +42,21 @@ void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup) {
Button button;
button.type = type;
button.bouncer.attach(pin, mode);
uint8_t index = _last_button_index + 1;
uint8_t increment = 1;
button.index0 = index;
_buttons[_num_buttons] = button;
_num_buttons++;
if (type == BUTTON_PULSED_DOUBLE_ACTION_SPLIT) {
increment = 2;
button.index1 = index + 1;
}
if (_last_button_index + increment > JOYSTICK_NUM_BUTTONS) {
// todo: fail here
}
_buttons.push_back(button);
last_button_index += increment;
if (type & _BUTTON_PULSED_TYPES) _have_pulsed_button = true;
}
@ -57,8 +69,8 @@ void Joystick::AddAxis(uint8_t pin) {
void Joystick::Update() {
JoyReport oldReport = _joyReport;
for (int i = 0; i < _num_buttons; i++) {
_UpdateButton(i);
for (list<Button>::iterator cursor = _buttons.begin(); cursor != _buttons.end(); cursor++) {
_UpdateButton(*cursor);
}
for (int i = 0; i < _num_axes; i++) {
@ -100,8 +112,9 @@ 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 (list<Button>::iterator i = _buttons.begin(); i != buttons.end(); i++) {
if (i->type & _BUTTON_PULSED_TYPES) ReleaseButton(i->index0);
if (i->type & BUTTON_PULSED_DOUBLE_ACTION_SPLIT) ReleaseButton(i->index1);
}
}
@ -124,20 +137,23 @@ void Joystick::Write() {
delay(250);
}
void Joystick::_UpdateButton(uint8_t index) {
Button* button = &_buttons[index];
void Joystick::_UpdateButton(Button* button) {
bool changed = button->bouncer.update();
switch (button->type) {
case BUTTON_LATCHED:
if (button->bouncer.rose()) PressButton(index);
else if (button->bouncer.fell()) ReleaseButton(index);
case BUTTON_PASSTHRU:
if (button->bouncer.rose()) PressButton(button->index0);
else if (button->bouncer.fell()) ReleaseButton(button->index0);
break;
case BUTTON_PULSED:
if (button->bouncer.rose()) PressButton(index);
if (button->bouncer.rose()) PressButton(button->index0);
break;
case BUTTON_PULSED_DOUBLE_ACTION:
if (changed) PressButton(index);
if (changed) PressButton(button->index0);
break;
case BUTTON_PULSED_DOUBLE_ACTION_SPLIT:
if (button->bouncer.rose()) PressButton(button->index0);
else if (button->bouncer.fell()) PressButton(button->index1);
break;
default:
if (_debug) {

View File

@ -3,6 +3,9 @@
#include <Arduino.h>
#include <Bounce2.h>
#include <list>
using std::list;
// If you're using the arduino-big-joystick firmware, these numbers can't be
// changed. If you're writing your own Report Descriptor, tune these to match by
@ -16,9 +19,10 @@
#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.
};
struct JoyReport {
@ -29,6 +33,9 @@ struct JoyReport {
struct Button {
ButtonType type;
Bounce bouncer;
uint8_t index0;
uint8_t index1;
bool inverted = false; // if true, send button press on release and vice versa.
};
bool operator ==(JoyReport a, JoyReport b);
@ -40,10 +47,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);
@ -56,8 +63,8 @@ class Joystick {
void _UpdateButton(uint8_t index);
void _UpdateAxis(uint8_t index);
Button _buttons[JOYSTICK_NUM_BUTTONS];
uint8_t _num_buttons;
list<Button> _buttons;
uint8_t _last_button_index;
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

View File

@ -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 <https://github.com/harlequin-tech/arduino-usb> for more info.
* Your Arduino's USB communication chip must be programmed with the arduino-big-joystick firmware (or similar). See <https://github.com/harlequin-tech/arduino-usb> for more info.
* The Bounce2 library, available at <https://github.com/thomasfredericks/Bounce2>.
## Installation