Add logic to handle button presses.
This commit is contained in:
50
Joystick.cpp
50
Joystick.cpp
@ -37,6 +37,7 @@ void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup) {
|
||||
_buttons[_num_buttons].pin = pin;
|
||||
_buttons[_num_buttons].type = type;
|
||||
_buttons[_num_buttons].last_state = digitalRead(pin);
|
||||
_buttons[_num_buttons].pullup = pullup;
|
||||
_num_buttons++;
|
||||
|
||||
if (type & _BUTTON_PULSED_TYPES) _have_pulsed_button = true;
|
||||
@ -63,23 +64,19 @@ void Joystick::Update() {
|
||||
JoyReport oldReport = _joyReport;
|
||||
|
||||
for (int i = 0; i < _num_buttons; i++) {
|
||||
uint8_t value = digitalRead(_buttons[i].pin);
|
||||
// TODO: handle button according to ButtonType
|
||||
_UpdateButton(_buttons[i], i);
|
||||
}
|
||||
|
||||
for (int i = 0; i < _num_axes; i++) {
|
||||
uint16_t value = analogRead(_axes[i]);
|
||||
// TODO: convert raw analog value to something sensible for
|
||||
// HID Analog data.
|
||||
_UpdateAxis(i);
|
||||
}
|
||||
|
||||
if (_joyReport != oldReport) {
|
||||
Write();
|
||||
}
|
||||
|
||||
if (_have_pulsed_button) {
|
||||
_ReleasePulsedButtons();
|
||||
Write();
|
||||
if (_have_pulsed_button) {
|
||||
_ReleasePulsedButtons();
|
||||
Write();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -125,3 +122,36 @@ void Joystick::Write() {
|
||||
Serial.write((uint8_t *)&_joyReport, sizeof(JoyReport));
|
||||
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");
|
||||
}
|
||||
|
Reference in New Issue
Block a user