diff --git a/Joystick.cpp b/Joystick.cpp index f7aee16..b7707e9 100644 --- a/Joystick.cpp +++ b/Joystick.cpp @@ -1,6 +1,6 @@ #include "Joystick.h" -#include #include +#include using namespace admux; @@ -40,6 +40,17 @@ void Joystick::Init() { } 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; if (pullup) mode = INPUT_PULLUP; else mode = INPUT; @@ -65,6 +76,8 @@ void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup) { _virtual_buttons += increment; if (type & _BUTTON_PULSED_TYPES) _have_pulsed_button = true; + + return &button; } void Joystick::AddAxis(uint8_t pin) { @@ -72,18 +85,14 @@ 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 Joystick::AddMux(uint8_t signal_pin, Pinset addr_pins, bool pullup) { + uint8_t mux_id = _num_mux; uint8_t mode = INPUT_PULLUP; if (!pullup) mode = INPUT; - Mux mux(Pin(signal_pin, mode, PinType::Digital), pins); - _mux[_num_mux] = mux; + Mux mux(Pin(signal_pin, mode, PinType::Digital), addr_pins); + _mux[mux_id] = &mux; _num_mux++; + return mux_id; } void Joystick::Update() { @@ -161,7 +170,7 @@ 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); + _mux[button->mux_id]->channel(button->mux_channel); } bool changed = button->bouncer.update(); if (!changed) return; diff --git a/Joystick.h b/Joystick.h index 568bd13..24008ee 100644 --- a/Joystick.h +++ b/Joystick.h @@ -1,9 +1,9 @@ #ifndef _JOYSTICK_H_ #define _JOYSTICK_H_ +#include #include #include -#include 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 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); + 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. 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, // 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); + uint8_t AddMux(uint8_t signal_pin, Pinset addr_pins, bool pullup=true); private: void SetAxis(uint8_t axis, int16_t value); @@ -83,7 +84,9 @@ class Joystick { void _ReleasePulsedButtons(); void _UpdateButton(uint8_t index); void _UpdateAxis(uint8_t index); - + + Button* _BuildButton(uint8_t pin, ButtonType type, bool pullup); + Button _buttons[JOYSTICK_NUM_BUTTONS]; uint8_t _num_buttons; uint8_t _virtual_buttons; // a single user-defined button can have multiple virtual buttons. @@ -95,7 +98,7 @@ class Joystick { JoyReport _joyReport; bool _debug; - Mux _mux[MAX_MUX]; + Mux *_mux[MAX_MUX]; uint8_t _num_mux; }; diff --git a/Readme.md b/Readme.md index a733af4..ae8e941 100644 --- a/Readme.md +++ b/Readme.md @@ -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. -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 ` 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`. diff --git a/examples/multiplexer/multiplexer.ino b/examples/multiplexer/multiplexer.ino index 259e3e2..cdac23f 100644 --- a/examples/multiplexer/multiplexer.ino +++ b/examples/multiplexer/multiplexer.ino @@ -7,6 +7,7 @@ // would require as few as 10 inputs in this scenario, for example. This is just a demo :) #include +#include bool debug = false; Joystick joystick(debug); @@ -28,17 +29,17 @@ void setup() { joystick.AddButton(A0, BUTTON_PASSTHRU); // 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 - 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.AddButton(A1, BUTTON_PASSTHRU, true, true, mux, 2); - joystick.AddButton(A1, BUTTON_PASSTHRU, true, true, mux, 3); - joystick.AddButton(A1, BUTTON_PASSTHRU, true, true, mux, 4); - joystick.AddButton(A1, BUTTON_PASSTHRU, true, true, mux, 5); - joystick.AddButton(A1, BUTTON_PASSTHRU, true, true, mux, 6); - joystick.AddButton(A1, BUTTON_PASSTHRU, true, true, mux, 7); - joystick.AddButton(A1, BUTTON_PASSTHRU, true, true, mux, 8); + joystick.AddMuxButton(A1, mux, 0, BUTTON_PASSTHRU); + joystick.AddMuxButton(A1, mux, 1, BUTTON_PASSTHRU); + joystick.AddMuxButton(A1, mux, 2, BUTTON_PASSTHRU); + joystick.AddMuxButton(A1, mux, 3, BUTTON_PASSTHRU); + joystick.AddMuxButton(A1, mux, 4, BUTTON_PASSTHRU); + joystick.AddMuxButton(A1, mux, 5, BUTTON_PASSTHRU); + joystick.AddMuxButton(A1, mux, 6, BUTTON_PASSTHRU); + joystick.AddMuxButton(A1, mux, 7, BUTTON_PASSTHRU); joystick.Init(); }