From c37e4a6789ee0502372860467fcb616430465d0f Mon Sep 17 00:00:00 2001 From: annabunches Date: Mon, 1 Nov 2021 14:49:53 -0400 Subject: [PATCH 1/8] Add new behavior for a button that sends separate keypresses on press and release. This required a refactor of the button abstraction as well. --- Joystick.cpp | 50 +++++++++++++++++++++++++++++++++----------------- Joystick.h | 23 +++++++++++++++-------- Readme.md | 2 +- 3 files changed, 49 insertions(+), 26 deletions(-) diff --git a/Joystick.cpp b/Joystick.cpp index 57c76b5..738f423 100644 --- a/Joystick.cpp +++ b/Joystick.cpp @@ -1,5 +1,8 @@ #include "Joystick.h" #include +#include + +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