A number of logic fixes. Buttons are now released correctly and the button segment of the new API works.

This commit is contained in:
Anna Rose 2015-11-11 23:51:02 -05:00
parent 9ddb1a9529
commit 763e55d518

View File

@ -2,13 +2,24 @@
#include <Arduino.h> #include <Arduino.h>
bool operator ==(JoyReport a, JoyReport b){ 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){ 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;
@ -34,10 +45,14 @@ void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup) {
if (pullup) pinMode(pin, INPUT_PULLUP); if (pullup) pinMode(pin, INPUT_PULLUP);
else pinMode(pin, INPUT); else pinMode(pin, INPUT);
_buttons[_num_buttons].pin = pin; Button button;
_buttons[_num_buttons].type = type; button.pin = pin;
_buttons[_num_buttons].last_state = digitalRead(pin); button.type = type;
_buttons[_num_buttons].pullup = pullup; if (pullup) button.last_state = _InvertSignal(digitalRead(pin));
else button.last_state = digitalRead(pin);
button.pullup = pullup;
_buttons[_num_buttons] = button;
_num_buttons++; _num_buttons++;
if (type & _BUTTON_PULSED_TYPES) _have_pulsed_button = true; 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 byte = button / 8;
uint8_t bit = button % 8; uint8_t bit = button % 8;
_joyReport.button[byte] |= 1 << bit; _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) { void Joystick::ReleaseButton(uint8_t button) {
if (button >= JOYSTICK_NUM_BUTTONS) return; if (button >= JOYSTICK_NUM_BUTTONS) return;
uint8_t byte = button / 8; uint8_t byte = button / 8;
uint8_t bit = button % 8; uint8_t bit = button % 8;
_joyReport.button[byte] |= 0 << bit; _joyReport.button[byte] &= ~(1 << bit);
if (_debug) Serial.println("DEBUG: Button release recorded."); if (_debug) {
Serial.print("DEBUG: Button release recorded for button ");
Serial.println(button);
}
} }
void Joystick::ReleaseAllButtons() { void Joystick::ReleaseAllButtons() {
@ -111,25 +132,35 @@ 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++) {
if (_buttons[i].type & _BUTTON_PULSED_TYPES) if (_buttons[i].type & _BUTTON_PULSED_TYPES) ReleaseButton(i);
ReleaseButton(i);
} }
if (_debug) Serial.println("DEBUG: Pulse button release recorded."); if (_debug) Serial.println("DEBUG: Pulse button release recorded.");
} }
void Joystick::Write() { void Joystick::Write() {
if (!_debug) {
Serial.write((uint8_t *)&_joyReport, sizeof(JoyReport)); 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); delay(250);
} }
void Joystick::_UpdateButton(Button& button, uint8_t index) { void Joystick::_UpdateButton(Button& button, uint8_t index) {
uint8_t value = digitalRead(button.pin); uint8_t value = digitalRead(button.pin);
// Treat pullup-resistor inputs as basically backwards. // Treat pullup-resistor inputs as basically backwards.
if (button.pullup) { if (button.pullup) value = _InvertSignal(value);
if (value == LOW) value = HIGH;
else value = LOW;
}
switch (button.type) { switch (button.type) {
case BUTTON_MAINTAINED: case BUTTON_MAINTAINED: