Refactor code substantially, moving buttons into separate classes and using a lot more pointers to conserve memory until it is needed.
This commit is contained in:
parent
346e612f65
commit
a69c2d3364
6 changed files with 285 additions and 185 deletions
63
Joystick.h
63
Joystick.h
|
@ -1,6 +1,7 @@
|
|||
#ifndef _JOYSTICK_H_
|
||||
#define _JOYSTICK_H_
|
||||
|
||||
#include "Button.h"
|
||||
#include <Arduino.h>
|
||||
#include <Bounce2.h>
|
||||
#include <Encoder.h>
|
||||
|
@ -19,44 +20,11 @@ using namespace admux;
|
|||
#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.
|
||||
BUTTON_PULSED_DOUBLE_ACTION = 0x4, // Send a button press twice - once for press and once for release.
|
||||
BUTTON_PULSED_DOUBLE_ACTION_SPLIT = 0x8, // Send two separate button presses - one button on press, another on release.
|
||||
BUTTON_LATCHED_MOMENTARY = 0x10,
|
||||
ENCODER_PULSED_SPLIT = 0x20 // A rotary encoder that should be treated as two different pulsed/momentary buttons, one for each direction
|
||||
};
|
||||
|
||||
struct JoyReport {
|
||||
int16_t axis[JOYSTICK_NUM_AXES];
|
||||
uint8_t button[JOYSTICK_NUM_BYTES];
|
||||
};
|
||||
|
||||
struct Button {
|
||||
ButtonType type;
|
||||
Bounce bouncer;
|
||||
uint8_t vbutton0;
|
||||
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.
|
||||
|
||||
// multiplexer button settings
|
||||
// todo: this should probably be abstracted out from this struct...
|
||||
bool mux;
|
||||
uint8_t mux_id;
|
||||
uint8_t mux_channel;
|
||||
|
||||
// encoder button settings
|
||||
bool encoder_button;
|
||||
Encoder* encoder;
|
||||
long last_enc;
|
||||
};
|
||||
|
||||
bool operator ==(JoyReport a, JoyReport b);
|
||||
bool operator !=(JoyReport a, JoyReport b);
|
||||
|
||||
|
@ -70,35 +38,26 @@ class 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);
|
||||
void AddButton(uint8_t pin, ButtonType type, bool pullup=true, Mux* mux=NULL);
|
||||
|
||||
// Add a rotary encoder. ENCODER button types allow you to treat an encoder as a momentary button or an axis (TODO)
|
||||
void AddEncoder(uint8_t pin0, uint8_t pin1, ButtonType type, bool pullup=true);
|
||||
void AddMuxButton(uint8_t mux_id, uint8_t mux_channel, ButtonType type, bool pullup=true);
|
||||
void AddEncoder(uint8_t pin1, uint8_t pin2, ButtonType type);
|
||||
|
||||
// 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, Pinset addr_pins, bool pullup=true);
|
||||
|
||||
private:
|
||||
void SetAxis(uint8_t axis, int16_t value);
|
||||
void PressButton(uint8_t button);
|
||||
void ReleaseButton(uint8_t button);
|
||||
|
||||
private:
|
||||
void SetAxis(uint8_t axis, int16_t value);
|
||||
void ReleaseAllButtons();
|
||||
void Write();
|
||||
|
||||
void _ReleasePulsedButtons();
|
||||
void _UpdateButton(uint8_t index);
|
||||
void _UpdateEncoder(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 _virtual_buttons; // a single user-defined button can have multiple virtual buttons.
|
||||
bool _have_pulsed_button;
|
||||
|
@ -108,12 +67,6 @@ class Joystick {
|
|||
|
||||
JoyReport _joyReport;
|
||||
bool _debug;
|
||||
|
||||
Mux *_mux[MAX_MUX];
|
||||
uint8_t _num_mux;
|
||||
};
|
||||
|
||||
// Internal use only.
|
||||
#define _BUTTON_PULSED_TYPES (BUTTON_PULSED | BUTTON_PULSED_DOUBLE_ACTION | BUTTON_PULSED_DOUBLE_ACTION_SPLIT | ENCODER_PULSED_SPLIT)
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue