Add analog_only option, though this is a stub for now.

This commit is contained in:
Anna Rose 2021-11-13 05:56:21 +00:00
parent e09c21edf1
commit e8a1235a95
4 changed files with 20 additions and 15 deletions

View File

@ -13,7 +13,8 @@ void Button::ReleaseButtons(Joystick* js) {
js->ReleaseButton(vbutton); js->ReleaseButton(vbutton);
} }
SwitchButton::SwitchButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux) : Button(vbutton) { // TODO: make analog_only work... how to handle that with debouncer?
SwitchButton::SwitchButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux, bool analog_only) : Button(vbutton) {
this->mux = mux; this->mux = mux;
uint8_t mode = INPUT; uint8_t mode = INPUT;
@ -32,6 +33,7 @@ SwitchButton::SwitchButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux)
bool SwitchButton::BouncerUpdate() { bool SwitchButton::BouncerUpdate() {
if (mux != NULL) { if (mux != NULL) {
mux->channel(channel_id); mux->channel(channel_id);
delayMicroseconds(500);
} }
return bouncer.update(); return bouncer.update();
@ -44,7 +46,7 @@ bool SwitchButton::On() {
} }
PassthruButton::PassthruButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux) : SwitchButton(pin, vbutton, pullup, mux) { PassthruButton::PassthruButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux, bool analog_only) : SwitchButton(pin, vbutton, pullup, mux, analog_only) {
this->type = BUTTON_PASSTHRU; this->type = BUTTON_PASSTHRU;
} }
@ -56,7 +58,7 @@ bool PassthruButton::Update(Joystick* js) {
} }
LatchedButton::LatchedButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux) : SwitchButton(pin, vbutton, pullup, mux) { LatchedButton::LatchedButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux, bool analog_only) : SwitchButton(pin, vbutton, pullup, mux, analog_only) {
this->type = BUTTON_LATCHED_MOMENTARY; this->type = BUTTON_LATCHED_MOMENTARY;
this->pressed = false; this->pressed = false;
} }
@ -78,7 +80,7 @@ bool LatchedButton::Update(Joystick* js) {
} }
PulsedButton::PulsedButton(uint8_t pin, uint8_t vbutton, bool double_action, bool split, bool pullup, Mux* mux) : SwitchButton(pin, vbutton, pullup, mux) { PulsedButton::PulsedButton(uint8_t pin, uint8_t vbutton, bool double_action, bool split, bool pullup, Mux* mux, bool analog_only) : SwitchButton(pin, vbutton, pullup, mux, analog_only) {
if (double_action) { if (double_action) {
if (split) { if (split) {
this->type = BUTTON_PULSED_DOUBLE_ACTION_SPLIT; this->type = BUTTON_PULSED_DOUBLE_ACTION_SPLIT;

View File

@ -40,7 +40,7 @@ class Button {
// and the multiplexer logic will be automatically invoked by Update() // and the multiplexer logic will be automatically invoked by Update()
class SwitchButton : public Button { class SwitchButton : public Button {
public: public:
SwitchButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux); SwitchButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux, bool analog_only);
bool BouncerUpdate(); // returns true if the pin's status has changed bool BouncerUpdate(); // returns true if the pin's status has changed
bool On(); bool On();
@ -53,13 +53,13 @@ class SwitchButton : public Button {
class PassthruButton : public SwitchButton { class PassthruButton : public SwitchButton {
public: public:
PassthruButton(uint8_t pin, uint8_t vbutton, bool pullup = true, Mux* mux = NULL); PassthruButton(uint8_t pin, uint8_t vbutton, bool pullup = true, Mux* mux = NULL, bool analog_only = false);
bool Update(Joystick* js); bool Update(Joystick* js);
}; };
class LatchedButton : public SwitchButton { class LatchedButton : public SwitchButton {
public: public:
LatchedButton(uint8_t pin, uint8_t vbutton, bool pullup = true, Mux* mux = NULL); LatchedButton(uint8_t pin, uint8_t vbutton, bool pullup = true, Mux* mux = NULL, bool analog_only = false);
bool Update(Joystick* js); bool Update(Joystick* js);
protected: protected:
@ -68,7 +68,7 @@ class LatchedButton : public SwitchButton {
class PulsedButton : public SwitchButton { class PulsedButton : public SwitchButton {
public: public:
PulsedButton(uint8_t pin, uint8_t vbutton, bool double_action = false, bool split = false, bool pullup = true, Mux* mux = NULL); PulsedButton(uint8_t pin, uint8_t vbutton, bool double_action = false, bool split = false, bool pullup = true, Mux* mux = NULL, bool analog_only = false);
bool Update(Joystick* js); bool Update(Joystick* js);
void ReleaseButtons(Joystick* js); void ReleaseButtons(Joystick* js);

View File

@ -40,23 +40,23 @@ void Joystick::Init() {
delay(100); delay(100);
} }
void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup, Mux* mux) { void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup, Mux* mux, bool analog_only) {
Button *button; Button *button;
switch (type) { switch (type) {
case BUTTON_PASSTHRU: case BUTTON_PASSTHRU:
button = new PassthruButton(pin, _virtual_buttons, pullup, mux); button = new PassthruButton(pin, _virtual_buttons, pullup, mux, analog_only);
_virtual_buttons++; _virtual_buttons++;
break; break;
case BUTTON_PULSED: case BUTTON_PULSED:
button = new PulsedButton(pin, _virtual_buttons, false, false, pullup, mux); button = new PulsedButton(pin, _virtual_buttons, false, false, pullup, mux, analog_only);
_virtual_buttons++; _virtual_buttons++;
break; break;
case BUTTON_PULSED_DOUBLE_ACTION: case BUTTON_PULSED_DOUBLE_ACTION:
button = new PulsedButton(pin, _virtual_buttons, true, false, pullup, mux); button = new PulsedButton(pin, _virtual_buttons, true, false, pullup, mux, analog_only);
_virtual_buttons++; _virtual_buttons++;
break; break;
case BUTTON_PULSED_DOUBLE_ACTION_SPLIT: case BUTTON_PULSED_DOUBLE_ACTION_SPLIT:
button = new PulsedButton(pin, _virtual_buttons, true, true, pullup, mux); button = new PulsedButton(pin, _virtual_buttons, true, true, pullup, mux, analog_only);
_virtual_buttons += 2; _virtual_buttons += 2;
break; break;
default: default:

View File

@ -38,7 +38,9 @@ class Joystick {
// Button types are documented in the ButtonType enum. // 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 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.
void AddButton(uint8_t pin, ButtonType type, bool pullup=true, Mux* mux=NULL); // Setting `analogOnly` to true indicates your button *must* be read with analog code, such as the A6 and A7 pins
// on the Arduino Nano.
void AddButton(uint8_t pin, ButtonType type, bool pullup=true, Mux* mux=NULL, bool analog_only=false);
// Add a rotary encoder. ENCODER button types allow you to treat an encoder as a momentary button or an axis (TODO) // 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 pin1, uint8_t pin2, ButtonType type); void AddEncoder(uint8_t pin1, uint8_t pin2, ButtonType type);
@ -50,7 +52,6 @@ class Joystick {
// These members should not be used by end users; todo: remember how friend classes work // These members should not be used by end users; todo: remember how friend classes work
void PressButton(uint8_t button); void PressButton(uint8_t button);
void ReleaseButton(uint8_t button); void ReleaseButton(uint8_t button);
bool _debug;
private: private:
void SetAxis(uint8_t axis, int16_t value); void SetAxis(uint8_t axis, int16_t value);
@ -69,6 +70,8 @@ class Joystick {
uint8_t _num_axes; uint8_t _num_axes;
JoyReport _joyReport; JoyReport _joyReport;
bool _debug;
}; };
#endif #endif