Factor pin-reading logic into a new set of classes.

This commit is contained in:
2021-11-22 19:12:44 +00:00
parent a4f00a0a23
commit 8ebc1a5523
6 changed files with 182 additions and 109 deletions

View File

@ -1,5 +1,6 @@
#include "Joystick.h"
#include "Button.h"
#include "Reader.h"
#include <Mux.h>
#include <Arduino.h>
#include <stdio.h>
@ -39,36 +40,16 @@ void Joystick::Init() {
delay(100);
}
void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup, Mux* mux, bool analog_only) {
Button *button;
switch (type) {
case BUTTON_PASSTHRU:
button = new PassthruButton(pin, _virtual_buttons, pullup, mux, analog_only);
_virtual_buttons++;
break;
case BUTTON_PULSED:
button = new PulsedButton(pin, _virtual_buttons, false, false, pullup, mux, analog_only);
_virtual_buttons++;
break;
case BUTTON_PULSED_DOUBLE_ACTION:
button = new PulsedButton(pin, _virtual_buttons, true, false, pullup, mux, analog_only);
_virtual_buttons++;
break;
case BUTTON_PULSED_DOUBLE_ACTION_SPLIT:
button = new PulsedButton(pin, _virtual_buttons, true, true, pullup, mux, analog_only);
_virtual_buttons += 2;
break;
default:
return;
}
void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup) {
_addButton(type, new DirectReader(pin, pullup));
}
_buttons[_num_buttons] = button;
_num_buttons++;
if (_debug) {
char buffer[100];
sprintf(buffer, "Added button %d of type %d", _num_buttons - 1, button->type);
Serial.println(buffer);
}
void Joystick::AddMuxButton(uint8_t channel, Mux* mux, ButtonType type, bool pullup) {
_addButton(type, new MuxReader(channel, mux, pullup));
}
void Joystick::AddMatrixButton(uint8_t row, uint8_t col, ButtonType type, bool pullup) {
_addButton(type, new MatrixReader(row, col, pullup));
}
void Joystick::AddEncoder(uint8_t pin1, uint8_t pin2, ButtonType type) {
@ -164,3 +145,34 @@ void Joystick::Write() {
void Joystick::_UpdateAxis(uint8_t index) {
if (_debug) Serial.println("STUB: Joystick::_UpdateAxis");
}
void Joystick::_addButton(ButtonType type, Reader* reader) {
switch (type) {
case BUTTON_PASSTHRU:
button = new PassthruButton(_virtual_buttons, reader);
_virtual_buttons++;
break;
case BUTTON_PULSED:
button = new PulsedButton(_virtual_buttons, reader, false, false);
_virtual_buttons++;
break;
case BUTTON_PULSED_DOUBLE_ACTION:
button = new PulsedButton(_virtual_buttons, reader, true, false);
_virtual_buttons++;
break;
case BUTTON_PULSED_DOUBLE_ACTION_SPLIT:
button = new PulsedButton(_virtual_buttons, reader, true, true);
_virtual_buttons += 2;
break;
default:
return;
}
_buttons[_num_buttons] = button;
_num_buttons++;
if (_debug) {
char buffer[100];
sprintf(buffer, "Added button %d of type %d", _num_buttons - 1, button->type);
Serial.println(buffer);
}
}