diff --git a/Joystick.cpp b/Joystick.cpp index 439c3c5..2335471 100644 --- a/Joystick.cpp +++ b/Joystick.cpp @@ -2,13 +2,24 @@ #include bool operator ==(JoyReport a, JoyReport b){ - return a.axis == b.axis && a.button == b.button; + 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); } +uint8_t _InvertSignal(uint8_t signal) { + if (signal == HIGH) return LOW; + else return HIGH; +} + Joystick::Joystick(bool debug) { _debug = debug; _num_buttons = 0; @@ -33,11 +44,15 @@ void Joystick::Init() { void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup) { if (pullup) pinMode(pin, INPUT_PULLUP); else pinMode(pin, INPUT); - - _buttons[_num_buttons].pin = pin; - _buttons[_num_buttons].type = type; - _buttons[_num_buttons].last_state = digitalRead(pin); - _buttons[_num_buttons].pullup = pullup; + + 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; + + _buttons[_num_buttons] = button; _num_buttons++; if (type & _BUTTON_PULSED_TYPES) _have_pulsed_button = true; @@ -91,15 +106,21 @@ void Joystick::PressButton(uint8_t button) { uint8_t byte = button / 8; uint8_t bit = button % 8; _joyReport.button[byte] |= 1 << bit; - if (_debug) Serial.println("DEBUG: Button press recorded."); + if (_debug) { + Serial.print("DEBUG: Button press recorded for button "); + Serial.println(button); + } } void Joystick::ReleaseButton(uint8_t button) { if (button >= JOYSTICK_NUM_BUTTONS) return; uint8_t byte = button / 8; uint8_t bit = button % 8; - _joyReport.button[byte] |= 0 << bit; - if (_debug) Serial.println("DEBUG: Button release recorded."); + _joyReport.button[byte] &= ~(1 << bit); + if (_debug) { + Serial.print("DEBUG: Button release recorded for button "); + Serial.println(button); + } } void Joystick::ReleaseAllButtons() { @@ -111,25 +132,35 @@ void Joystick::ReleaseAllButtons() { void Joystick::_ReleasePulsedButtons() { for (uint8_t i = 0; i < _num_buttons; i++) { - if (_buttons[i].type & _BUTTON_PULSED_TYPES) - ReleaseButton(i); + if (_buttons[i].type & _BUTTON_PULSED_TYPES) ReleaseButton(i); } if (_debug) Serial.println("DEBUG: Pulse button release recorded."); } void Joystick::Write() { - Serial.write((uint8_t *)&_joyReport, sizeof(JoyReport)); + if (!_debug) { + Serial.write((uint8_t *)&_joyReport, sizeof(JoyReport)); + } + else { + Serial.print("DEBUG: Writing data: "); + for (uint8_t i=0; i < JOYSTICK_NUM_AXES; i++) { + Serial.print(_joyReport.axis[i]); + Serial.print(" "); + } + for (uint8_t i=0; i < JOYSTICK_NUM_BYTES; i++) { + Serial.print(_joyReport.button[i]); + Serial.print(" "); + } + Serial.println(); + } 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) { - if (value == LOW) value = HIGH; - else value = LOW; - } + if (button.pullup) value = _InvertSignal(value); switch (button.type) { case BUTTON_MAINTAINED: