Add logic to handle button presses.

This commit is contained in:
Anna Rose 2015-11-11 00:10:45 -05:00
parent c814b85d59
commit 9ddb1a9529
2 changed files with 52 additions and 17 deletions

View File

@ -37,6 +37,7 @@ void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup) {
_buttons[_num_buttons].pin = pin; _buttons[_num_buttons].pin = pin;
_buttons[_num_buttons].type = type; _buttons[_num_buttons].type = type;
_buttons[_num_buttons].last_state = digitalRead(pin); _buttons[_num_buttons].last_state = digitalRead(pin);
_buttons[_num_buttons].pullup = pullup;
_num_buttons++; _num_buttons++;
if (type & _BUTTON_PULSED_TYPES) _have_pulsed_button = true; if (type & _BUTTON_PULSED_TYPES) _have_pulsed_button = true;
@ -63,23 +64,19 @@ 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++) {
uint8_t value = digitalRead(_buttons[i].pin); _UpdateButton(_buttons[i], i);
// TODO: handle button according to ButtonType
} }
for (int i = 0; i < _num_axes; i++) { for (int i = 0; i < _num_axes; i++) {
uint16_t value = analogRead(_axes[i]); _UpdateAxis(i);
// TODO: convert raw analog value to something sensible for
// HID Analog data.
} }
if (_joyReport != oldReport) { if (_joyReport != oldReport) {
Write(); Write();
} if (_have_pulsed_button) {
_ReleasePulsedButtons();
if (_have_pulsed_button) { Write();
_ReleasePulsedButtons(); }
Write();
} }
} }
@ -125,3 +122,36 @@ void Joystick::Write() {
Serial.write((uint8_t *)&_joyReport, sizeof(JoyReport)); Serial.write((uint8_t *)&_joyReport, sizeof(JoyReport));
delay(250); 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;
}
switch (button.type) {
case BUTTON_MAINTAINED:
if (value == HIGH) PressButton(index);
else ReleaseButton(index);
break;
case BUTTON_PULSED:
if (value == HIGH && button.last_state == LOW) PressButton(index);
break;
case BUTTON_PULSED_DOUBLE_ACTION:
if (value != button.last_state) PressButton(index);
break;
default:
if (_debug) {
Serial.print("DEBUG: Unhandled button type: ");
Serial.println(button.type);
}
}
button.last_state = value;
}
void Joystick::_UpdateAxis(uint8_t index) {
if (_debug) Serial.println("STUB: Joystick::_UpdateAxis");
}

View File

@ -20,10 +20,17 @@ enum ButtonType {
BUTTON_PULSED_DOUBLE_ACTION = 0x4 BUTTON_PULSED_DOUBLE_ACTION = 0x4
}; };
typedef struct JoyReport { struct JoyReport {
int16_t axis[JOYSTICK_NUM_AXES]; int16_t axis[JOYSTICK_NUM_AXES];
uint8_t button[JOYSTICK_NUM_BYTES]; uint8_t button[JOYSTICK_NUM_BYTES];
} ; };
struct Button {
uint8_t pin;
ButtonType type;
uint8_t last_state;
bool pullup;
};
bool operator ==(JoyReport a, JoyReport b); bool operator ==(JoyReport a, JoyReport b);
bool operator !=(JoyReport a, JoyReport b); bool operator !=(JoyReport a, JoyReport b);
@ -47,12 +54,10 @@ class Joystick {
private: private:
void _ReleasePulsedButtons(); void _ReleasePulsedButtons();
void _UpdateButton(Button& button, uint8_t index);
void _UpdateAxis(uint8_t index);
struct { Button _buttons[JOYSTICK_NUM_BUTTONS];
uint8_t pin;
ButtonType type;
uint8_t last_state;
} _buttons[JOYSTICK_NUM_BUTTONS];
uint8_t _num_buttons; uint8_t _num_buttons;
bool _have_pulsed_button; bool _have_pulsed_button;