#include "Joystick.h" #include #include using std::list; bool operator ==(JoyReport a, JoyReport b){ for (uint8_t i=0; i < JOYSTICK_NUM_AXES; i++) { if (a.axis[i] != b.axis[i]) return false; } for (uint8_t i=0; i < JOYSTICK_NUM_BYTES; i++) { if (a.button[i] != b.button[i]) return false; } return true; } bool operator !=(JoyReport a, JoyReport b){ return !(a == b); } Joystick::Joystick(bool debug) { _debug = debug; _last_button_index = 0; _num_axes = 0; _have_pulsed_button = false; for (uint8_t i=0; i < JOYSTICK_NUM_AXES; i++) { _joyReport.axis[i] = 0; } } void Joystick::Init() { Serial.begin(115200); delay(100); } void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup) { uint8_t mode; if (pullup) mode = INPUT_PULLUP; else mode = INPUT; Button button; button.type = type; button.bouncer.attach(pin, mode); uint8_t index = _last_button_index + 1; uint8_t increment = 1; button.index0 = index; if (type == BUTTON_PULSED_DOUBLE_ACTION_SPLIT) { 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; } void Joystick::AddAxis(uint8_t pin) { _axes[_num_axes] = pin; _num_axes++; } void Joystick::Update() { JoyReport oldReport = _joyReport; for (list