diff --git a/Joystick.cpp b/Joystick.cpp index 1bef20d..53cb368 100644 --- a/Joystick.cpp +++ b/Joystick.cpp @@ -15,11 +15,6 @@ bool operator !=(JoyReport a, JoyReport b){ return !(a == b); } -uint8_t _InvertSignal(uint8_t signal) { - if (signal == HIGH) return LOW; - else return HIGH; -} - Joystick::Joystick(bool debug) { _debug = debug; _num_buttons = 0; @@ -42,15 +37,13 @@ void Joystick::Init() { } void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup) { - if (pullup) pinMode(pin, INPUT_PULLUP); - else pinMode(pin, INPUT); + uint8_t mode; + if (pullup) mode = INPUT_PULLUP; + else mode = INPUT; Button button; - button.pin = pin; button.type = type; - if (pullup) button.last_state = _InvertSignal(digitalRead(pin)); - else button.last_state = digitalRead(pin); - button.pullup = pullup; + button.bouncer.attach(pin, mode); _buttons[_num_buttons] = button; _num_buttons++; @@ -79,7 +72,7 @@ void Joystick::Update() { JoyReport oldReport = _joyReport; for (int i = 0; i < _num_buttons; i++) { - _UpdateButton(_buttons[i], i); + _UpdateButton(i); } for (int i = 0; i < _num_axes; i++) { @@ -155,21 +148,20 @@ void Joystick::Write() { delay(250); } -void Joystick::_UpdateButton(Button& button, uint8_t index) { - uint8_t value = digitalRead(button.pin); - // Treat pullup-resistor inputs as basically backwards. - if (button.pullup) value = _InvertSignal(value); +void Joystick::_UpdateButton(uint8_t index) { + Button button = _buttons[index]; + bool changed = button.debouncer.update(); switch (button.type) { case BUTTON_LATCHED: - if (value == HIGH && button.last_state != HIGH) PressButton(index); - else if (value == LOW && button.last_state != LOW) ReleaseButton(index); + if (button.debouncer.rose()) PressButton(index); + else if (button.debouncer.fell()) ReleaseButton(index); break; case BUTTON_PULSED: - if (value == HIGH && button.last_state == LOW) PressButton(index); + if button.debouncer.rose() PressButton(index); break; case BUTTON_PULSED_DOUBLE_ACTION: - if (value != button.last_state) PressButton(index); + if (changed) PressButton(index); break; default: if (_debug) { @@ -177,8 +169,6 @@ void Joystick::_UpdateButton(Button& button, uint8_t index) { Serial.println(button.type); } } - - button.last_state = value; } void Joystick::_UpdateAxis(uint8_t index) { diff --git a/Joystick.h b/Joystick.h index f8fb0b4..fad975e 100644 --- a/Joystick.h +++ b/Joystick.h @@ -2,6 +2,7 @@ #define _JOYSTICK_H_ #include +#include // 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 @@ -26,10 +27,8 @@ struct JoyReport { }; struct Button { - uint8_t pin; ButtonType type; - uint8_t last_state; - bool pullup; + Bounce bouncher; }; bool operator ==(JoyReport a, JoyReport b); @@ -54,7 +53,7 @@ class Joystick { private: void _ReleasePulsedButtons(); - void _UpdateButton(Button& button, uint8_t index); + void _UpdateButton(uint8_t index); void _UpdateAxis(uint8_t index); Button _buttons[JOYSTICK_NUM_BUTTONS]; diff --git a/Readme.md b/Readme.md index 714e8da..d65b616 100644 --- a/Readme.md +++ b/Readme.md @@ -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 -with the arduino-big-joystick firmware (or similar). - -See for more info. +Dependencies: +* Your Arduino's USB communication chip be programmed with the arduino-big-joystick firmware (or similar). See for more info. +* The Bounce2 library, available at .