For pulsed buttons, implement per-button 'timers' that independently end the pulse instead of having a 250ms delay between every single update...

This commit is contained in:
2021-11-13 22:48:12 +00:00
parent 3b69b7e960
commit 1a4916fd0e
4 changed files with 43 additions and 25 deletions

View File

@ -25,7 +25,6 @@ Joystick::Joystick(bool debug) {
_virtual_buttons = 0;
_num_axes = 0;
_num_buttons = 0;
_have_pulsed_button = false;
for (uint8_t i=0; i < JOYSTICK_NUM_AXES; i++) {
_joyReport.axis[i] = 0;
@ -70,7 +69,6 @@ void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup, Mux* mux, bo
sprintf(buffer, "Added button %d of type %d", _num_buttons - 1, button->type);
Serial.println(buffer);
}
if (type & _BUTTON_PULSED_TYPES) _have_pulsed_button = true;
}
void Joystick::AddEncoder(uint8_t pin1, uint8_t pin2, ButtonType type) {
@ -115,10 +113,6 @@ void Joystick::Update() {
if (_joyReport != oldReport) {
Write();
if (_have_pulsed_button) {
_ReleasePulsedButtons();
Write();
}
}
}
@ -147,18 +141,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) button->ReleaseButtons(this);
}
}
void Joystick::Write() {
if (!_debug) {
Serial.write((uint8_t *)&_joyReport, sizeof(JoyReport));
}
else {
if (_debug) {
Serial.print("DEBUG: Writing data: ");
for (uint8_t i=0; i < JOYSTICK_NUM_AXES; i++) {
Serial.print(_joyReport.axis[i]);
@ -169,8 +153,10 @@ void Joystick::Write() {
Serial.print(" ");
}
Serial.println();
return;
}
delay(250);
Serial.write((uint8_t *)&_joyReport, sizeof(JoyReport));
}