Add logic to handle button presses.
This commit is contained in:
parent
c814b85d59
commit
9ddb1a9529
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");
|
||||
}
|
||||
|
|
19
Joystick.h
19
Joystick.h
|
@ -20,10 +20,17 @@ enum ButtonType {
|
|||
BUTTON_PULSED_DOUBLE_ACTION = 0x4
|
||||
};
|
||||
|
||||
typedef struct JoyReport {
|
||||
struct JoyReport {
|
||||
int16_t axis[JOYSTICK_NUM_AXES];
|
||||
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);
|
||||
|
@ -47,12 +54,10 @@ class Joystick {
|
|||
|
||||
private:
|
||||
void _ReleasePulsedButtons();
|
||||
void _UpdateButton(Button& button, uint8_t index);
|
||||
void _UpdateAxis(uint8_t index);
|
||||
|
||||
struct {
|
||||
uint8_t pin;
|
||||
ButtonType type;
|
||||
uint8_t last_state;
|
||||
} _buttons[JOYSTICK_NUM_BUTTONS];
|
||||
Button _buttons[JOYSTICK_NUM_BUTTONS];
|
||||
uint8_t _num_buttons;
|
||||
bool _have_pulsed_button;
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user