Add support for multiplexers. This introduces a new dependency, but c'est la vie.

This commit is contained in:
2021-11-01 23:12:30 +00:00
parent da37d16da7
commit d6e0648abf
3 changed files with 74 additions and 13 deletions

View File

@ -3,6 +3,9 @@
#include <Arduino.h>
#include <Bounce2.h>
#include <Mux.h>
using namespace admux;
// If you're using the arduino-big-joystick firmware, these numbers can't be
// changed. If you're writing your own Report Descriptor, tune these to match by
@ -15,6 +18,10 @@
#endif
#define JOYSTICK_NUM_BYTES (JOYSTICK_NUM_BUTTONS+7)/8
#ifndef MAX_MUX
#define MAX_MUX 3
#endif
enum ButtonType {
BUTTON_PASSTHRU = 0x1, // always use the (debounced) absolute state of the input
BUTTON_PULSED = 0x2, // on button press, send an on signal followed immediately by an off signal.
@ -35,6 +42,11 @@ struct Button {
uint8_t vbutton1; // only used by BUTTON_PULSED_DOUBLE_ACTION_SPLIT
bool pressed = false; // only used by BUTTON_LATCHED_MOMENTARY
bool inverted = false; // if true, send button press on release and vice versa.
// todo: this should probably be abstracted out from this struct...
bool mux;
uint8_t mux_id;
uint8_t mux_channel;
};
bool operator ==(JoyReport a, JoyReport b);
@ -45,19 +57,29 @@ class Joystick {
Joystick(bool debug=false);
void Init();
void Update();
// Add a button to the joystick.
// Button types are documented in the ButtonType enum.
// If `pullup` is true, your button should connect the pin to ground. (also be sure that your board supports INPUT_PULLUP on that pin)
// If `pullup` is false, your button should connect the pin to VCC.
// Mux parameters are ignored unless `mux` is true.
void AddButton(uint8_t pin, ButtonType type, bool pullup=true, bool mux=false, uint8_t mux_id = 0; uint8_t mux_channel = 0);
// Add an analog axis to the joystick. THIS METHOD IS NOT CURRENTLY TESTED OR SUPPORTED. It might work, but probably not.
void AddAxis(uint8_t pin);
// Add control for a multiplexer. To add a button that's connected via the multiplexer,
// pass the mux's signal pin as the pin parameter to AddButton(), along with the mux* parameters.
// mux_id is the array index of the mux (in the order you added them via AddMux())
uint8_t AddMux(uint8_t signal_pin, uint8_t addr_pins[], uint8_t addr_width);
void AddButton(uint8_t pin, ButtonType type, bool pullup=true);
void AddAxis(uint8_t pin); // Axes don't actually work yet!
// Public access to these functions is deprecated and they may become private in a future
// version. Prefer the above API instead.
private:
void SetAxis(uint8_t axis, int16_t value);
void PressButton(uint8_t button);
void ReleaseButton(uint8_t button);
void ReleaseAllButtons();
void Write();
private:
void _ReleasePulsedButtons();
void _UpdateButton(uint8_t index);
void _UpdateAxis(uint8_t index);
@ -72,6 +94,9 @@ class Joystick {
JoyReport _joyReport;
bool _debug;
Mux _mux[MAX_MUX];
uint8_t _num_mux;
};
// Internal use only.