Add new behavior for a button that sends separate keypresses on press and release. This required a refactor of the button abstraction as well.

This commit is contained in:
2021-11-01 14:49:53 -04:00
parent 9234060532
commit c37e4a6789
3 changed files with 49 additions and 26 deletions

View File

@ -1,5 +1,8 @@
#include "Joystick.h"
#include <Arduino.h>
#include <list>
using std::list;
bool operator ==(JoyReport a, JoyReport b){
for (uint8_t i=0; i < JOYSTICK_NUM_AXES; i++) {
@ -17,16 +20,13 @@ bool operator !=(JoyReport a, JoyReport b){
Joystick::Joystick(bool debug) {
_debug = debug;
_num_buttons = 0;
_last_button_index = 0;
_num_axes = 0;
_have_pulsed_button = false;
for (uint8_t i=0; i < JOYSTICK_NUM_AXES; i++) {
_joyReport.axis[i] = 0;
}
for (uint8_t i=0; i < JOYSTICK_NUM_BYTES; i++) {
_joyReport.button[i] = 0;
}
}
void Joystick::Init() {
@ -42,9 +42,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;
_buttons[_num_buttons] = button;
_num_buttons++;
if (type == BUTTON_PULSED_DOUBLE_ACTION_SPLIT) {
increment = 2;
button.index1 = index + 1;
}
if (_last_button_index + increment > JOYSTICK_NUM_BUTTONS) {
// todo: fail here
}
_buttons.push_back(button);
last_button_index += increment;
if (type & _BUTTON_PULSED_TYPES) _have_pulsed_button = true;
}
@ -57,8 +69,8 @@ void Joystick::AddAxis(uint8_t pin) {
void Joystick::Update() {
JoyReport oldReport = _joyReport;
for (int i = 0; i < _num_buttons; i++) {
_UpdateButton(i);
for (list<Button>::iterator cursor = _buttons.begin(); cursor != _buttons.end(); cursor++) {
_UpdateButton(*cursor);
}
for (int i = 0; i < _num_axes; i++) {
@ -100,8 +112,9 @@ void Joystick::ReleaseAllButtons() {
}
void Joystick::_ReleasePulsedButtons() {
for (uint8_t i = 0; i < _num_buttons; i++) {
if (_buttons[i].type & _BUTTON_PULSED_TYPES) ReleaseButton(i);
for (list<Button>::iterator i = _buttons.begin(); i != buttons.end(); i++) {
if (i->type & _BUTTON_PULSED_TYPES) ReleaseButton(i->index0);
if (i->type & BUTTON_PULSED_DOUBLE_ACTION_SPLIT) ReleaseButton(i->index1);
}
}
@ -124,20 +137,23 @@ void Joystick::Write() {
delay(250);
}
void Joystick::_UpdateButton(uint8_t index) {
Button* button = &_buttons[index];
void Joystick::_UpdateButton(Button* button) {
bool changed = button->bouncer.update();
switch (button->type) {
case BUTTON_LATCHED:
if (button->bouncer.rose()) PressButton(index);
else if (button->bouncer.fell()) ReleaseButton(index);
case BUTTON_PASSTHRU:
if (button->bouncer.rose()) PressButton(button->index0);
else if (button->bouncer.fell()) ReleaseButton(button->index0);
break;
case BUTTON_PULSED:
if (button->bouncer.rose()) PressButton(index);
if (button->bouncer.rose()) PressButton(button->index0);
break;
case BUTTON_PULSED_DOUBLE_ACTION:
if (changed) PressButton(index);
if (changed) PressButton(button->index0);
break;
case BUTTON_PULSED_DOUBLE_ACTION_SPLIT:
if (button->bouncer.rose()) PressButton(button->index0);
else if (button->bouncer.fell()) PressButton(button->index1);
break;
default:
if (_debug) {