Compare commits

...

9 Commits

6 changed files with 92 additions and 22 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.elf
*.hex

View File

@ -17,16 +17,13 @@ bool operator !=(JoyReport a, JoyReport b){
Joystick::Joystick(bool debug) { Joystick::Joystick(bool debug) {
_debug = debug; _debug = debug;
_num_buttons = 0; _virtual_buttons = 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() {
@ -41,10 +38,23 @@ void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup) {
Button button; Button button;
button.type = type; button.type = type;
button.inverted = pullup;
button.bouncer.attach(pin, mode); 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; _buttons[_num_buttons] = button;
_num_buttons++; _num_buttons++;
_virtual_buttons += increment;
if (type & _BUTTON_PULSED_TYPES) _have_pulsed_button = true; if (type & _BUTTON_PULSED_TYPES) _have_pulsed_button = true;
} }
@ -57,11 +67,11 @@ 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 (uint8_t i = 0; i < _num_buttons; i++) {
_UpdateButton(i); _UpdateButton(i);
} }
for (int i = 0; i < _num_axes; i++) { for (uint8_t i = 0; i < _num_axes; i++) {
_UpdateAxis(i); _UpdateAxis(i);
} }
@ -101,7 +111,9 @@ void Joystick::ReleaseAllButtons() {
void Joystick::_ReleasePulsedButtons() { void Joystick::_ReleasePulsedButtons() {
for (uint8_t i = 0; i < _num_buttons; i++ ) { for (uint8_t i = 0; i < _num_buttons; i++ ) {
if (_buttons[i].type & _BUTTON_PULSED_TYPES) ReleaseButton(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); delay(250);
} }
void Joystick::_UpdateButton(uint8_t index) { void Joystick::_UpdateButton(uint8_t button_num) {
Button* button = &_buttons[index]; Button *button = &_buttons[button_num];
bool changed = button->bouncer.update(); bool changed = button->bouncer.update();
if (!changed) return;
bool on = button->bouncer.rose();
if (button->inverted) on = button->bouncer.fell();
switch (button->type) { switch (button->type) {
case BUTTON_LATCHED: case BUTTON_PASSTHRU:
if (button->bouncer.rose()) PressButton(index); if (on) PressButton(button->vbutton0);
else if (button->bouncer.fell()) ReleaseButton(index); else ReleaseButton(button->vbutton0);
break; break;
case BUTTON_PULSED: case BUTTON_PULSED:
if (button->bouncer.rose()) PressButton(index); if (on) PressButton(button->vbutton0);
break; break;
case BUTTON_PULSED_DOUBLE_ACTION: 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; break;
default: default:
if (_debug) { if (_debug) {

View File

@ -16,9 +16,11 @@
#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.
BUTTON_LATCHED_MOMENTARY = 0x10
}; };
struct JoyReport { struct JoyReport {
@ -29,6 +31,10 @@ struct JoyReport {
struct Button { struct Button {
ButtonType type; ButtonType type;
Bounce bouncer; 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); bool operator ==(JoyReport a, JoyReport b);
@ -40,10 +46,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);
@ -58,6 +64,7 @@ class Joystick {
Button _buttons[JOYSTICK_NUM_BUTTONS]; Button _buttons[JOYSTICK_NUM_BUTTONS];
uint8_t _num_buttons; uint8_t _num_buttons;
uint8_t _virtual_buttons; // a single user-defined button can have multiple virtual buttons.
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

View File

@ -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}

View 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();
}