Refactor code substantially, moving buttons into separate classes and using a lot more pointers to conserve memory until it is needed.
This commit is contained in:
152
Joystick.cpp
152
Joystick.cpp
@ -1,4 +1,5 @@
|
||||
#include "Joystick.h"
|
||||
#include "Button.h"
|
||||
#include <Mux.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
@ -22,7 +23,6 @@ Joystick::Joystick(bool debug) {
|
||||
_debug = debug;
|
||||
_virtual_buttons = 0;
|
||||
_num_axes = 0;
|
||||
_num_mux = 0;
|
||||
_num_buttons = 0;
|
||||
_have_pulsed_button = false;
|
||||
|
||||
@ -39,21 +39,39 @@ void Joystick::Init() {
|
||||
delay(100);
|
||||
}
|
||||
|
||||
void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup) {
|
||||
_BuildButton(pin, type, pullup);
|
||||
void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup, Mux* mux) {
|
||||
Button *button;
|
||||
switch (type) {
|
||||
case BUTTON_PASSTHRU:
|
||||
button = new PassthruButton(pin, _virtual_buttons, pullup, mux);
|
||||
_virtual_buttons++;
|
||||
case BUTTON_PULSED:
|
||||
button = new PulsedButton(pin, _virtual_buttons, false, false, pullup, mux);
|
||||
_virtual_buttons++;
|
||||
case BUTTON_PULSED_DOUBLE_ACTION:
|
||||
button = new PulsedButton(pin, _virtual_buttons, true, false, pullup, mux);
|
||||
_virtual_buttons++;
|
||||
case BUTTON_PULSED_DOUBLE_ACTION_SPLIT:
|
||||
button = new PulsedButton(pin, _virtual_buttons, true, true, pullup, mux);
|
||||
_virtual_buttons += 2;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
_buttons[_num_buttons] = button;
|
||||
_num_buttons++;
|
||||
if (type & _BUTTON_PULSED_TYPES) _have_pulsed_button = true;
|
||||
}
|
||||
|
||||
void Joystick::AddEncoder(uint8_t pin0, uint8_t pin1, ButtonType type, bool pullup) {
|
||||
void Joystick::AddEncoder(uint8_t pin1, uint8_t pin2, ButtonType type) {
|
||||
Button *button;
|
||||
switch (type) {
|
||||
case ENCODER_PULSED_SPLIT:
|
||||
// add an encoder button. _BuildButton() doesn't do everything we need, however...
|
||||
Button *button = _BuildButton(pin0, type, pullup);
|
||||
|
||||
// ... so we set up the encoder stuff here. button->pin and button->bouncer will be
|
||||
// ignored
|
||||
button->encoder_button = true;
|
||||
button->encoder = new Encoder(pin0, pin1);
|
||||
long last_enc = button->encoder->read();
|
||||
button = new EncoderButton(pin1, pin2, _virtual_buttons);
|
||||
_buttons[_num_buttons] = button;
|
||||
_num_buttons++;
|
||||
_virtual_buttons += 2;
|
||||
break;
|
||||
default:
|
||||
if (_debug) {
|
||||
@ -63,70 +81,19 @@ void Joystick::AddEncoder(uint8_t pin0, uint8_t pin1, ButtonType type, bool pull
|
||||
}
|
||||
}
|
||||
|
||||
void Joystick::AddMuxButton(uint8_t mux_id, uint8_t mux_channel, ButtonType type, bool pullup) {
|
||||
uint8_t pin = _mux[mux_id]->signalPin().pin;
|
||||
Button *button = _BuildButton(pin, type, pullup);
|
||||
button->mux = true;
|
||||
button->mux_id = mux_id;
|
||||
button->mux_channel = mux_channel;
|
||||
}
|
||||
|
||||
Button* Joystick::_BuildButton(uint8_t pin, ButtonType type, bool pullup) {
|
||||
uint8_t mode;
|
||||
if (pullup) mode = INPUT_PULLUP;
|
||||
else mode = INPUT;
|
||||
|
||||
Button button;
|
||||
button.type = type;
|
||||
button.inverted = pullup;
|
||||
button.bouncer.attach(pin, mode);
|
||||
uint8_t increment = 1;
|
||||
button.vbutton0 = _virtual_buttons;
|
||||
|
||||
if (type & (BUTTON_PULSED_DOUBLE_ACTION_SPLIT | ENCODER_PULSED_SPLIT)) {
|
||||
increment = 2;
|
||||
button.vbutton1 = _virtual_buttons + 1;
|
||||
}
|
||||
|
||||
if (_virtual_buttons + increment > JOYSTICK_NUM_BUTTONS) {
|
||||
// todo: fail here
|
||||
}
|
||||
|
||||
_buttons[_num_buttons] = button;
|
||||
_num_buttons++;
|
||||
_virtual_buttons += increment;
|
||||
|
||||
if (type & _BUTTON_PULSED_TYPES) _have_pulsed_button = true;
|
||||
|
||||
return &button;
|
||||
}
|
||||
|
||||
void Joystick::AddAxis(uint8_t pin) {
|
||||
_axes[_num_axes] = pin;
|
||||
_num_axes++;
|
||||
}
|
||||
|
||||
uint8_t Joystick::AddMux(uint8_t signal_pin, Pinset addr_pins, bool pullup) {
|
||||
uint8_t mux_id = _num_mux;
|
||||
uint8_t mode = INPUT_PULLUP;
|
||||
if (!pullup) mode = INPUT;
|
||||
Mux mux(Pin(signal_pin, mode, PinType::Digital), addr_pins);
|
||||
_mux[mux_id] = &mux;
|
||||
_num_mux++;
|
||||
return mux_id;
|
||||
}
|
||||
|
||||
void Joystick::Update() {
|
||||
JoyReport oldReport = _joyReport;
|
||||
|
||||
for (uint8_t i = 0; i < _num_buttons; i++) {
|
||||
if (_buttons[i].type == ENCODER_PULSED_SPLIT) { // todo: make this check for any encoder type
|
||||
_UpdateEncoder(i);
|
||||
} else {
|
||||
_UpdateButton(i);
|
||||
}
|
||||
_buttons[i]->Update(this);
|
||||
}
|
||||
|
||||
// TODO: implement this and also refactor it into a class or classes
|
||||
for (uint8_t i = 0; i < _num_axes; i++) {
|
||||
_UpdateAxis(i);
|
||||
}
|
||||
@ -167,9 +134,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.vbutton0);
|
||||
if (button.type & BUTTON_PULSED_DOUBLE_ACTION_SPLIT) ReleaseButton(button.vbutton1);
|
||||
Button* button = _buttons[i];
|
||||
if (button->type & _BUTTON_PULSED_TYPES) button->ReleaseButtons(this);
|
||||
}
|
||||
}
|
||||
|
||||
@ -192,59 +158,7 @@ void Joystick::Write() {
|
||||
delay(250);
|
||||
}
|
||||
|
||||
// todo: bite the bullet and use inheritance here, this is getting out of hand
|
||||
void Joystick::_UpdateButton(uint8_t button_num) {
|
||||
Button *button = &_buttons[button_num];
|
||||
if (button->mux) {
|
||||
_mux[button->mux_id]->channel(button->mux_channel);
|
||||
}
|
||||
bool changed = button->bouncer.update();
|
||||
if (!changed) return;
|
||||
|
||||
bool on = button->bouncer.rose();
|
||||
if (button->inverted) on = button->bouncer.fell();
|
||||
|
||||
switch (button->type) {
|
||||
case BUTTON_PASSTHRU:
|
||||
if (on) PressButton(button->vbutton0);
|
||||
else ReleaseButton(button->vbutton0);
|
||||
break;
|
||||
case BUTTON_PULSED:
|
||||
if (on) PressButton(button->vbutton0);
|
||||
break;
|
||||
case BUTTON_PULSED_DOUBLE_ACTION:
|
||||
PressButton(button->vbutton0);
|
||||
break;
|
||||
case BUTTON_PULSED_DOUBLE_ACTION_SPLIT:
|
||||
if (on) PressButton(button->vbutton0);
|
||||
else PressButton(button->vbutton1);
|
||||
break;
|
||||
case BUTTON_LATCHED_MOMENTARY:
|
||||
if (on) {
|
||||
if (!button->pressed) {
|
||||
PressButton(button->vbutton0);
|
||||
button->pressed = true;
|
||||
} else {
|
||||
ReleaseButton(button->vbutton0);
|
||||
button->pressed = false;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (_debug) {
|
||||
Serial.print("DEBUG: Unhandled button type: ");
|
||||
Serial.println(button->type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Joystick::_UpdateEncoder(uint8_t index) {
|
||||
Button *button = &_buttons[index];
|
||||
long new_value = button->encoder->read();
|
||||
if (new_value > button->last_enc) PressButton(button->vbutton0);
|
||||
else if (new_value < button->last_enc) PressButton(button->vbutton1);
|
||||
button->last_enc = new_value;
|
||||
}
|
||||
|
||||
void Joystick::_UpdateAxis(uint8_t index) {
|
||||
if (_debug) Serial.println("STUB: Joystick::_UpdateAxis");
|
||||
|
Reference in New Issue
Block a user