Add support for multiplexers. This introduces a new dependency, but c'est la vie.
This commit is contained in:
27
Joystick.cpp
27
Joystick.cpp
@ -1,5 +1,8 @@
|
||||
#include "Joystick.h"
|
||||
#include <Arduino.h>
|
||||
#include <Mux.h>
|
||||
|
||||
using namespace admux;
|
||||
|
||||
bool operator ==(JoyReport a, JoyReport b){
|
||||
for (uint8_t i=0; i < JOYSTICK_NUM_AXES; i++) {
|
||||
@ -19,11 +22,16 @@ Joystick::Joystick(bool debug) {
|
||||
_debug = debug;
|
||||
_virtual_buttons = 0;
|
||||
_num_axes = 0;
|
||||
_num_mux = 0;
|
||||
_num_buttons = 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() {
|
||||
@ -64,6 +72,20 @@ void Joystick::AddAxis(uint8_t pin) {
|
||||
_num_axes++;
|
||||
}
|
||||
|
||||
void Joystick::AddMux(uint8_t signal_pin, uint8_t addr_pins[], uint8_t addr_width, bool pullup=true) {
|
||||
Pinset pins;
|
||||
for (uint8_t i = 0; i < addr_width; i++) {
|
||||
pins.pins[i] = addr_pins[i];
|
||||
}
|
||||
pins.m_size = addr_width;
|
||||
|
||||
uint8_t mode = INPUT_PULLUP;
|
||||
if (!pullup) mode = INPUT;
|
||||
Mux mux(Pin(signal_pin, mode, PinType::Digital), pins);
|
||||
_mux[_num_mux] = mux;
|
||||
_num_mux++;
|
||||
}
|
||||
|
||||
void Joystick::Update() {
|
||||
JoyReport oldReport = _joyReport;
|
||||
|
||||
@ -138,6 +160,9 @@ void Joystick::Write() {
|
||||
|
||||
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;
|
||||
|
||||
|
Reference in New Issue
Block a user