Rename variables for clarity, fix enumeration bug.
This commit is contained in:
31
Joystick.cpp
31
Joystick.cpp
@ -17,7 +17,7 @@ bool operator !=(JoyReport a, JoyReport b){
|
||||
|
||||
Joystick::Joystick(bool debug) {
|
||||
_debug = debug;
|
||||
_last_button_index = 0;
|
||||
_virtual_buttons = 0;
|
||||
_num_axes = 0;
|
||||
_have_pulsed_button = false;
|
||||
|
||||
@ -39,22 +39,21 @@ void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup) {
|
||||
Button button;
|
||||
button.type = type;
|
||||
button.bouncer.attach(pin, mode);
|
||||
uint8_t index = _last_button_index + 1;
|
||||
uint8_t increment = 1;
|
||||
button.index0 = index;
|
||||
button.vbutton0 = _virtual_buttons;
|
||||
|
||||
if (type == BUTTON_PULSED_DOUBLE_ACTION_SPLIT) {
|
||||
increment = 2;
|
||||
button.index1 = index + 1;
|
||||
button.vbutton1 = _virtual_buttons + 1;
|
||||
}
|
||||
|
||||
if (_last_button_index + increment > JOYSTICK_NUM_BUTTONS) {
|
||||
if (_virtual_buttons + increment > JOYSTICK_NUM_BUTTONS) {
|
||||
// todo: fail here
|
||||
}
|
||||
|
||||
_buttons[_num_buttons] = button;
|
||||
_num_buttons++;
|
||||
_last_button_index += increment;
|
||||
_virtual_buttons += increment;
|
||||
|
||||
if (type & _BUTTON_PULSED_TYPES) _have_pulsed_button = true;
|
||||
}
|
||||
@ -112,8 +111,8 @@ void Joystick::ReleaseAllButtons() {
|
||||
void Joystick::_ReleasePulsedButtons() {
|
||||
for (uint8_t i = 0; i < _num_buttons; i++ ) {
|
||||
Button button = _buttons[i];
|
||||
if (button.type & _BUTTON_PULSED_TYPES) ReleaseButton(button.index0);
|
||||
if (button.type & BUTTON_PULSED_DOUBLE_ACTION_SPLIT) ReleaseButton(button.index1);
|
||||
if (button.type & _BUTTON_PULSED_TYPES) ReleaseButton(button.vbutton0);
|
||||
if (button.type & BUTTON_PULSED_DOUBLE_ACTION_SPLIT) ReleaseButton(button.vbutton1);
|
||||
}
|
||||
}
|
||||
|
||||
@ -142,26 +141,26 @@ void Joystick::_UpdateButton(uint8_t button_num) {
|
||||
|
||||
switch (button->type) {
|
||||
case BUTTON_PASSTHRU:
|
||||
if (button->bouncer.rose()) PressButton(button->index0);
|
||||
else if (button->bouncer.fell()) ReleaseButton(button->index0);
|
||||
if (button->bouncer.rose()) PressButton(button->vbutton0);
|
||||
else if (button->bouncer.fell()) ReleaseButton(button->vbutton0);
|
||||
break;
|
||||
case BUTTON_PULSED:
|
||||
if (button->bouncer.rose()) PressButton(button->index0);
|
||||
if (button->bouncer.rose()) PressButton(button->vbutton0);
|
||||
break;
|
||||
case BUTTON_PULSED_DOUBLE_ACTION:
|
||||
if (changed) PressButton(button->index0);
|
||||
if (changed) PressButton(button->vbutton0);
|
||||
break;
|
||||
case BUTTON_PULSED_DOUBLE_ACTION_SPLIT:
|
||||
if (button->bouncer.rose()) PressButton(button->index0);
|
||||
else if (button->bouncer.fell()) PressButton(button->index1);
|
||||
if (button->bouncer.rose()) PressButton(button->vbutton0);
|
||||
else if (button->bouncer.fell()) PressButton(button->vbutton1);
|
||||
break;
|
||||
case BUTTON_LATCHED_MOMENTARY:
|
||||
if (button->bouncer.rose()) {
|
||||
if (!button->pressed) {
|
||||
PressButton(button->index0);
|
||||
PressButton(button->vbutton0);
|
||||
button->pressed = true;
|
||||
} else {
|
||||
ReleaseButton(button->index0);
|
||||
ReleaseButton(button->vbutton0);
|
||||
button->pressed = false;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user