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 49 additions and 26 deletions
Showing only changes of commit c37e4a6789 - Show all commits

View File

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

View File

@ -3,6 +3,9 @@
#include <Arduino.h> #include <Arduino.h>
#include <Bounce2.h> #include <Bounce2.h>
#include <list>
using std::list;
// If you're using the arduino-big-joystick firmware, these numbers can't be // 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 // 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 #define JOYSTICK_NUM_BYTES (JOYSTICK_NUM_BUTTONS+7)/8
enum ButtonType { enum ButtonType {
BUTTON_LATCHED = 0x1, BUTTON_PASSTHRU = 0x1, // always use the (debounced) absolute state of the input
BUTTON_PULSED = 0x2, BUTTON_PULSED = 0x2, // on button press, send an on signal followed immediately by an off signal.
BUTTON_PULSED_DOUBLE_ACTION = 0x4 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 { struct JoyReport {
@ -29,6 +33,9 @@ struct JoyReport {
struct Button { struct Button {
ButtonType type; ButtonType type;
Bounce bouncer; 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); bool operator ==(JoyReport a, JoyReport b);
@ -40,10 +47,10 @@ class Joystick {
void Init(); void Init();
void Update(); 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! 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. // version. Prefer the above API instead.
void SetAxis(uint8_t axis, int16_t value); void SetAxis(uint8_t axis, int16_t value);
void PressButton(uint8_t button); void PressButton(uint8_t button);
@ -56,8 +63,8 @@ class Joystick {
void _UpdateButton(uint8_t index); void _UpdateButton(uint8_t index);
void _UpdateAxis(uint8_t index); void _UpdateAxis(uint8_t index);
Button _buttons[JOYSTICK_NUM_BUTTONS]; list<Button> _buttons;
uint8_t _num_buttons; uint8_t _last_button_index;
bool _have_pulsed_button; bool _have_pulsed_button;
uint8_t _axes[JOYSTICK_NUM_AXES]; uint8_t _axes[JOYSTICK_NUM_AXES];
@ -68,6 +75,6 @@ class Joystick {
}; };
// Internal use only. // 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 #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. This is a library that builds and sends USB HID Joystick reports, making it easy to build USB Joysticks with Arduino.
## Dependencies ## 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>. * The Bounce2 library, available at <https://github.com/thomasfredericks/Bounce2>.
## Installation ## Installation