Rework the joystick code to use the Bounce2 library.

This commit is contained in:
Anna Rose 2015-11-13 00:45:38 -05:00
parent 9ae0777c6b
commit f0cfcdc90d
3 changed files with 19 additions and 31 deletions

View File

@ -15,11 +15,6 @@ bool operator !=(JoyReport a, JoyReport b){
return !(a == b); return !(a == b);
} }
uint8_t _InvertSignal(uint8_t signal) {
if (signal == HIGH) return LOW;
else return HIGH;
}
Joystick::Joystick(bool debug) { Joystick::Joystick(bool debug) {
_debug = debug; _debug = debug;
_num_buttons = 0; _num_buttons = 0;
@ -42,15 +37,13 @@ void Joystick::Init() {
} }
void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup) { void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup) {
if (pullup) pinMode(pin, INPUT_PULLUP); uint8_t mode;
else pinMode(pin, INPUT); if (pullup) mode = INPUT_PULLUP;
else mode = INPUT;
Button button; Button button;
button.pin = pin;
button.type = type; button.type = type;
if (pullup) button.last_state = _InvertSignal(digitalRead(pin)); button.bouncer.attach(pin, mode);
else button.last_state = digitalRead(pin);
button.pullup = pullup;
_buttons[_num_buttons] = button; _buttons[_num_buttons] = button;
_num_buttons++; _num_buttons++;
@ -79,7 +72,7 @@ void Joystick::Update() {
JoyReport oldReport = _joyReport; JoyReport oldReport = _joyReport;
for (int i = 0; i < _num_buttons; i++) { for (int i = 0; i < _num_buttons; i++) {
_UpdateButton(_buttons[i], i); _UpdateButton(i);
} }
for (int i = 0; i < _num_axes; i++) { for (int i = 0; i < _num_axes; i++) {
@ -155,21 +148,20 @@ void Joystick::Write() {
delay(250); delay(250);
} }
void Joystick::_UpdateButton(Button& button, uint8_t index) { void Joystick::_UpdateButton(uint8_t index) {
uint8_t value = digitalRead(button.pin); Button button = _buttons[index];
// Treat pullup-resistor inputs as basically backwards. bool changed = button.debouncer.update();
if (button.pullup) value = _InvertSignal(value);
switch (button.type) { switch (button.type) {
case BUTTON_LATCHED: case BUTTON_LATCHED:
if (value == HIGH && button.last_state != HIGH) PressButton(index); if (button.debouncer.rose()) PressButton(index);
else if (value == LOW && button.last_state != LOW) ReleaseButton(index); else if (button.debouncer.fell()) ReleaseButton(index);
break; break;
case BUTTON_PULSED: case BUTTON_PULSED:
if (value == HIGH && button.last_state == LOW) PressButton(index); if button.debouncer.rose() PressButton(index);
break; break;
case BUTTON_PULSED_DOUBLE_ACTION: case BUTTON_PULSED_DOUBLE_ACTION:
if (value != button.last_state) PressButton(index); if (changed) PressButton(index);
break; break;
default: default:
if (_debug) { if (_debug) {
@ -177,8 +169,6 @@ void Joystick::_UpdateButton(Button& button, uint8_t index) {
Serial.println(button.type); Serial.println(button.type);
} }
} }
button.last_state = value;
} }
void Joystick::_UpdateAxis(uint8_t index) { void Joystick::_UpdateAxis(uint8_t index) {

View File

@ -2,6 +2,7 @@
#define _JOYSTICK_H_ #define _JOYSTICK_H_
#include <Arduino.h> #include <Arduino.h>
#include <Bounce2.h>
// 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
@ -26,10 +27,8 @@ struct JoyReport {
}; };
struct Button { struct Button {
uint8_t pin;
ButtonType type; ButtonType type;
uint8_t last_state; Bounce bouncher;
bool pullup;
}; };
bool operator ==(JoyReport a, JoyReport b); bool operator ==(JoyReport a, JoyReport b);
@ -54,7 +53,7 @@ class Joystick {
private: private:
void _ReleasePulsedButtons(); void _ReleasePulsedButtons();
void _UpdateButton(Button& button, uint8_t index); void _UpdateButton(uint8_t index);
void _UpdateAxis(uint8_t index); void _UpdateAxis(uint8_t index);
Button _buttons[JOYSTICK_NUM_BUTTONS]; Button _buttons[JOYSTICK_NUM_BUTTONS];

View File

@ -1,7 +1,6 @@
This is a simple library that builds and sends USB HID Joystick reports. This is a library that builds and sends USB HID Joystick reports, making it easy to build USB Joysticks with Arduino.
This requires that your Arduino's USB communication chip be programmed Dependencies:
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>.