Add support for multiplexers #2
31
Joystick.cpp
31
Joystick.cpp
|
@ -1,6 +1,6 @@
|
||||||
#include "Joystick.h"
|
#include "Joystick.h"
|
||||||
#include <Arduino.h>
|
|
||||||
#include <Mux.h>
|
#include <Mux.h>
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
using namespace admux;
|
using namespace admux;
|
||||||
|
|
||||||
|
@ -40,6 +40,17 @@ void Joystick::Init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup) {
|
void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup) {
|
||||||
|
_BuildButton(pin, type, pullup);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Joystick::AddMuxButton(uint8_t pin, uint8_t mux_id, uint8_t mux_channel, ButtonType type, bool pullup) {
|
||||||
|
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;
|
uint8_t mode;
|
||||||
if (pullup) mode = INPUT_PULLUP;
|
if (pullup) mode = INPUT_PULLUP;
|
||||||
else mode = INPUT;
|
else mode = INPUT;
|
||||||
|
@ -65,6 +76,8 @@ void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup) {
|
||||||
_virtual_buttons += increment;
|
_virtual_buttons += increment;
|
||||||
|
|
||||||
if (type & _BUTTON_PULSED_TYPES) _have_pulsed_button = true;
|
if (type & _BUTTON_PULSED_TYPES) _have_pulsed_button = true;
|
||||||
|
|
||||||
|
return &button;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Joystick::AddAxis(uint8_t pin) {
|
void Joystick::AddAxis(uint8_t pin) {
|
||||||
|
@ -72,18 +85,14 @@ void Joystick::AddAxis(uint8_t pin) {
|
||||||
_num_axes++;
|
_num_axes++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Joystick::AddMux(uint8_t signal_pin, uint8_t addr_pins[], uint8_t addr_width, bool pullup=true) {
|
uint8_t Joystick::AddMux(uint8_t signal_pin, Pinset addr_pins, bool pullup) {
|
||||||
Pinset pins;
|
uint8_t mux_id = _num_mux;
|
||||||
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;
|
uint8_t mode = INPUT_PULLUP;
|
||||||
if (!pullup) mode = INPUT;
|
if (!pullup) mode = INPUT;
|
||||||
Mux mux(Pin(signal_pin, mode, PinType::Digital), pins);
|
Mux mux(Pin(signal_pin, mode, PinType::Digital), addr_pins);
|
||||||
_mux[_num_mux] = mux;
|
_mux[mux_id] = &mux;
|
||||||
_num_mux++;
|
_num_mux++;
|
||||||
|
return mux_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Joystick::Update() {
|
void Joystick::Update() {
|
||||||
|
@ -161,7 +170,7 @@ void Joystick::Write() {
|
||||||
void Joystick::_UpdateButton(uint8_t button_num) {
|
void Joystick::_UpdateButton(uint8_t button_num) {
|
||||||
Button *button = &_buttons[button_num];
|
Button *button = &_buttons[button_num];
|
||||||
if (button->mux) {
|
if (button->mux) {
|
||||||
_mux[button->mux_id].channel(button->mux_channel);
|
_mux[button->mux_id]->channel(button->mux_channel);
|
||||||
}
|
}
|
||||||
bool changed = button->bouncer.update();
|
bool changed = button->bouncer.update();
|
||||||
if (!changed) return;
|
if (!changed) return;
|
||||||
|
|
11
Joystick.h
11
Joystick.h
|
@ -1,9 +1,9 @@
|
||||||
#ifndef _JOYSTICK_H_
|
#ifndef _JOYSTICK_H_
|
||||||
#define _JOYSTICK_H_
|
#define _JOYSTICK_H_
|
||||||
|
|
||||||
|
#include <Mux.h>
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <Bounce2.h>
|
#include <Bounce2.h>
|
||||||
#include <Mux.h>
|
|
||||||
|
|
||||||
using namespace admux;
|
using namespace admux;
|
||||||
|
|
||||||
|
@ -63,7 +63,8 @@ class Joystick {
|
||||||
// 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 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.
|
// If `pullup` is false, your button should connect the pin to VCC.
|
||||||
// Mux parameters are ignored unless `mux` is true.
|
// 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);
|
void AddButton(uint8_t pin, ButtonType type, bool pullup=true);
|
||||||
|
void AddMuxButton(uint8_t pin, uint8_t mux_id, uint8_t mux_channel, ButtonType type, bool pullup=true);
|
||||||
|
|
||||||
// Add an analog axis to the joystick. THIS METHOD IS NOT CURRENTLY TESTED OR SUPPORTED. It might work, but probably not.
|
// 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);
|
void AddAxis(uint8_t pin);
|
||||||
|
@ -71,7 +72,7 @@ class Joystick {
|
||||||
// Add control for a multiplexer. To add a button that's connected via the multiplexer,
|
// 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.
|
// 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())
|
// 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);
|
uint8_t AddMux(uint8_t signal_pin, Pinset addr_pins, bool pullup=true);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void SetAxis(uint8_t axis, int16_t value);
|
void SetAxis(uint8_t axis, int16_t value);
|
||||||
|
@ -84,6 +85,8 @@ class Joystick {
|
||||||
void _UpdateButton(uint8_t index);
|
void _UpdateButton(uint8_t index);
|
||||||
void _UpdateAxis(uint8_t index);
|
void _UpdateAxis(uint8_t index);
|
||||||
|
|
||||||
|
Button* _BuildButton(uint8_t pin, ButtonType type, bool pullup);
|
||||||
|
|
||||||
Button _buttons[JOYSTICK_NUM_BUTTONS];
|
Button _buttons[JOYSTICK_NUM_BUTTONS];
|
||||||
uint8_t _num_buttons;
|
uint8_t _num_buttons;
|
||||||
uint8_t _virtual_buttons; // a single user-defined button can have multiple virtual buttons.
|
uint8_t _virtual_buttons; // a single user-defined button can have multiple virtual buttons.
|
||||||
|
@ -95,7 +98,7 @@ class Joystick {
|
||||||
JoyReport _joyReport;
|
JoyReport _joyReport;
|
||||||
bool _debug;
|
bool _debug;
|
||||||
|
|
||||||
Mux _mux[MAX_MUX];
|
Mux *_mux[MAX_MUX];
|
||||||
uint8_t _num_mux;
|
uint8_t _num_mux;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -26,4 +26,4 @@ This is a library that builds and sends USB HID Joystick reports, making it easy
|
||||||
|
|
||||||
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.
|
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`.
|
Call the `AddMux()` method and pass it the pins the multiplexer is connected to. You'll need to `#include <Mux.h>` to access `admux::Pinset`. `AddMux()` 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`.
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
// would require as few as 10 inputs in this scenario, for example. This is just a demo :)
|
// would require as few as 10 inputs in this scenario, for example. This is just a demo :)
|
||||||
|
|
||||||
#include <Joystick.h>
|
#include <Joystick.h>
|
||||||
|
#include <Mux.h>
|
||||||
|
|
||||||
bool debug = false;
|
bool debug = false;
|
||||||
Joystick joystick(debug);
|
Joystick joystick(debug);
|
||||||
|
@ -28,17 +29,17 @@ void setup() {
|
||||||
joystick.AddButton(A0, BUTTON_PASSTHRU);
|
joystick.AddButton(A0, BUTTON_PASSTHRU);
|
||||||
|
|
||||||
// to get more room for our inputs, we add an 8-bit multiplexer
|
// to get more room for our inputs, we add an 8-bit multiplexer
|
||||||
uint8_t mux = joystick.AddMux(A1, {A2, A3, A4, A5}, 4);
|
uint8_t mux = joystick.AddMux(A1, admux::Pinset(A2, A3, A4, A5), 4);
|
||||||
|
|
||||||
// now we can add the rest of the buttons
|
// now we can add the rest of the buttons
|
||||||
joystick.AddButton(A1, BUTTON_PASSTHRU, true, true, mux, 1); // the last parameter is which channel/pin the input is attached to on the multiplexer
|
joystick.AddMuxButton(A1, mux, 0, BUTTON_PASSTHRU);
|
||||||
joystick.AddButton(A1, BUTTON_PASSTHRU, true, true, mux, 2);
|
joystick.AddMuxButton(A1, mux, 1, BUTTON_PASSTHRU);
|
||||||
joystick.AddButton(A1, BUTTON_PASSTHRU, true, true, mux, 3);
|
joystick.AddMuxButton(A1, mux, 2, BUTTON_PASSTHRU);
|
||||||
joystick.AddButton(A1, BUTTON_PASSTHRU, true, true, mux, 4);
|
joystick.AddMuxButton(A1, mux, 3, BUTTON_PASSTHRU);
|
||||||
joystick.AddButton(A1, BUTTON_PASSTHRU, true, true, mux, 5);
|
joystick.AddMuxButton(A1, mux, 4, BUTTON_PASSTHRU);
|
||||||
joystick.AddButton(A1, BUTTON_PASSTHRU, true, true, mux, 6);
|
joystick.AddMuxButton(A1, mux, 5, BUTTON_PASSTHRU);
|
||||||
joystick.AddButton(A1, BUTTON_PASSTHRU, true, true, mux, 7);
|
joystick.AddMuxButton(A1, mux, 6, BUTTON_PASSTHRU);
|
||||||
joystick.AddButton(A1, BUTTON_PASSTHRU, true, true, mux, 8);
|
joystick.AddMuxButton(A1, mux, 7, BUTTON_PASSTHRU);
|
||||||
|
|
||||||
joystick.Init();
|
joystick.Init();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user