Compare commits

..

No commits in common. "da37d16da7a0970ed11470ed41f7780da2c58fbf" and "9234060532fcb0d108c3103be0cc73437c38b78f" have entirely different histories.

6 changed files with 22 additions and 92 deletions

2
.gitignore vendored
View File

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

View File

@ -17,13 +17,16 @@ bool operator !=(JoyReport a, JoyReport b){
Joystick::Joystick(bool debug) { Joystick::Joystick(bool debug) {
_debug = debug; _debug = debug;
_virtual_buttons = 0; _num_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() {
@ -38,23 +41,10 @@ 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;
} }
@ -67,11 +57,11 @@ void Joystick::AddAxis(uint8_t pin) {
void Joystick::Update() { void Joystick::Update() {
JoyReport oldReport = _joyReport; JoyReport oldReport = _joyReport;
for (uint8_t i = 0; i < _num_buttons; i++) { for (int i = 0; i < _num_buttons; i++) {
_UpdateButton(i); _UpdateButton(i);
} }
for (uint8_t i = 0; i < _num_axes; i++) { for (int i = 0; i < _num_axes; i++) {
_UpdateAxis(i); _UpdateAxis(i);
} }
@ -111,9 +101,7 @@ 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++) {
Button button = _buttons[i]; if (_buttons[i].type & _BUTTON_PULSED_TYPES) ReleaseButton(i);
if (button.type & _BUTTON_PULSED_TYPES) ReleaseButton(button.vbutton0);
if (button.type & BUTTON_PULSED_DOUBLE_ACTION_SPLIT) ReleaseButton(button.vbutton1);
} }
} }
@ -136,39 +124,20 @@ void Joystick::Write() {
delay(250); delay(250);
} }
void Joystick::_UpdateButton(uint8_t button_num) { void Joystick::_UpdateButton(uint8_t index) {
Button *button = &_buttons[button_num]; Button* button = &_buttons[index];
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_PASSTHRU: case BUTTON_LATCHED:
if (on) PressButton(button->vbutton0); if (button->bouncer.rose()) PressButton(index);
else ReleaseButton(button->vbutton0); else if (button->bouncer.fell()) ReleaseButton(index);
break; break;
case BUTTON_PULSED: case BUTTON_PULSED:
if (on) PressButton(button->vbutton0); if (button->bouncer.rose()) PressButton(index);
break; break;
case BUTTON_PULSED_DOUBLE_ACTION: case BUTTON_PULSED_DOUBLE_ACTION:
PressButton(button->vbutton0); if (changed) PressButton(index);
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,11 +16,9 @@
#define JOYSTICK_NUM_BYTES (JOYSTICK_NUM_BUTTONS+7)/8 #define JOYSTICK_NUM_BYTES (JOYSTICK_NUM_BUTTONS+7)/8
enum ButtonType { enum ButtonType {
BUTTON_PASSTHRU = 0x1, // always use the (debounced) absolute state of the input BUTTON_LATCHED = 0x1,
BUTTON_PULSED = 0x2, // on button press, send an on signal followed immediately by an off signal. BUTTON_PULSED = 0x2,
BUTTON_PULSED_DOUBLE_ACTION = 0x4, // Send a button press twice - once for press and once for release. BUTTON_PULSED_DOUBLE_ACTION = 0x4
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 {
@ -31,10 +29,6 @@ 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);
@ -46,10 +40,10 @@ class Joystick {
void Init(); void Init();
void Update(); void Update();
void AddButton(uint8_t pin, ButtonType type, bool pullup=true); void AddButton(uint8_t pin, ButtonType type, bool pullup=false);
void AddAxis(uint8_t pin); // Axes don't actually work yet! void AddAxis(uint8_t pin); // Axes don't actually work yet!
// Public access to these functions is deprecated and they may become private in a future // These functions are deprecated and 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);
@ -64,7 +58,6 @@ 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];
@ -75,6 +68,6 @@ class Joystick {
}; };
// Internal use only. // Internal use only.
#define _BUTTON_PULSED_TYPES (BUTTON_PULSED | BUTTON_PULSED_DOUBLE_ACTION | BUTTON_PULSED_DOUBLE_ACTION_SPLIT) #define _BUTTON_PULSED_TYPES (BUTTON_PULSED | BUTTON_PULSED_DOUBLE_ACTION)
#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 must 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 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

@ -1,8 +0,0 @@
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

@ -1,22 +0,0 @@
// 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();
}