Add support for multiplexers #2

Merged
annabunches merged 3 commits from multiplexer-support into main 2021-11-07 03:16:59 +00:00
3 changed files with 74 additions and 13 deletions
Showing only changes of commit d6e0648abf - Show all commits

View File

@ -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;

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);
@ -46,18 +58,28 @@ class Joystick {
void Init();
void Update();
void AddButton(uint8_t pin, ButtonType type, bool pullup=true);
void AddAxis(uint8_t pin); // Axes don't actually work yet!
// 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);
// Public access to these functions is deprecated and they may become private in a future
// version. Prefer the above API instead.
// 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);
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.

View File

@ -2,17 +2,28 @@
This is a library that builds and sends USB HID Joystick reports, making it easy to build USB Joysticks with Arduino.
## Dependencies
* Your Arduino's USB communication chip must be programmed with the arduino-big-joystick firmware (or similar). See <https://github.com/harlequin-tech/arduino-usb> for more info.
* After uploading your sketch, your Arduino's USB communication chip will need to be programmed with the arduino-big-joystick firmware (or similar). See <https://github.com/harlequin-tech/arduino-usb> for more info.
* The Bounce2 library, available at <https://github.com/thomasfredericks/Bounce2>.
* The Analog-Digital Multiplexers library, available at <https://github.com/stechio/arduino-ad-mux-lib>.
## Installation
1. Put the arduino-joystick directory into your Arduino libraries directory.
2. Make sure Bounce2 is installed!
1. Install dependencies as above, or if using `arduino-cli`, with:
arduino-cli lib install Bounce2
arduino-cli lib install "Analog-Digital Multiplexers"
2. Put the arduino-joystick directory into your Arduino libraries directory.
## Usage
1. In your arduino sketch, add the includes:
#include <Bounce2.h>
#include <Joystick.h>
2. Create a Joystick object:
Joystick joystick;
3. Add buttons and axes in setup() with Joystick::AddButton(), and call Joystick::Update() in loop(). That's it!
Joystick joystick(true);
3. Add buttons and axes in setup() with Joystick::AddButton(), and call Joystick::Update() in loop().
4. Upload the sketch, connect to the serial monitor (at 115200 baud) and test the buttons.
5. Set the joystick's `debug` parameter to `false` and re-upload the sketch. The arduino will NOT work in joystick mode with debug set to `true`!
6. Flash the `arduino-big-joystick` firmware onto the USB Controller.
### Advanced usage: multiplexers
If you need more buttons than your board has pins, multiplexers (such as [this one](https://www.sparkfun.com/products/13906) and [this one](https://www.sparkfun.com/products/9056)) are a popular solution. This library supports multiplexers! To use them, you need to do some extra work.
Call the `AddMux()` method and pass it the pins the multiplexer is connected to. It will return a `mux_id` for subsequently passing to `AddButton()`. The `pin` parameter for all multiplexed buttons should be the same as the multiplexer's `signal_pin`.