Compare commits
No commits in common. "e8a1235a95fe613d89bd4ff7e6e1a88a4a4c1eb9" and "a69c2d3364eb75d9c9183a108747eb1f28c4a28d" have entirely different histories.
e8a1235a95
...
a69c2d3364
41
Button.cpp
41
Button.cpp
|
@ -13,8 +13,7 @@ void Button::ReleaseButtons(Joystick* js) {
|
||||||
js->ReleaseButton(vbutton);
|
js->ReleaseButton(vbutton);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: make analog_only work... how to handle that with debouncer?
|
SwitchButton::SwitchButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux) : Button(vbutton) {
|
||||||
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;
|
||||||
|
@ -33,7 +32,6 @@ 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();
|
||||||
|
@ -46,25 +44,24 @@ bool SwitchButton::On() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
PassthruButton::PassthruButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux, bool analog_only) : SwitchButton(pin, vbutton, pullup, mux, analog_only) {
|
PassthruButton::PassthruButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux) : SwitchButton(pin, vbutton, pullup, mux) {
|
||||||
this->type = BUTTON_PASSTHRU;
|
this->type = BUTTON_PASSTHRU;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PassthruButton::Update(Joystick* js) {
|
void PassthruButton::Update(Joystick* js) {
|
||||||
if (!BouncerUpdate()) return false;
|
if (!BouncerUpdate()) return;
|
||||||
if (On()) js->PressButton(vbutton);
|
if (On()) js->PressButton(vbutton);
|
||||||
else js->ReleaseButton(vbutton);
|
else js->ReleaseButton(vbutton);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
LatchedButton::LatchedButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux, bool analog_only) : SwitchButton(pin, vbutton, pullup, mux, analog_only) {
|
LatchedButton::LatchedButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux) : SwitchButton(pin, vbutton, pullup, mux) {
|
||||||
this->type = BUTTON_LATCHED_MOMENTARY;
|
this->type = BUTTON_LATCHED_MOMENTARY;
|
||||||
this->pressed = false;
|
this->pressed = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LatchedButton::Update(Joystick* js) {
|
void LatchedButton::Update(Joystick* js) {
|
||||||
if (!BouncerUpdate()) return false;
|
if (!BouncerUpdate()) return;
|
||||||
|
|
||||||
if (On()) {
|
if (On()) {
|
||||||
if (!pressed) {
|
if (!pressed) {
|
||||||
|
@ -75,12 +72,10 @@ bool LatchedButton::Update(Joystick* js) {
|
||||||
pressed = false;
|
pressed = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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) {
|
PulsedButton::PulsedButton(uint8_t pin, uint8_t vbutton, bool double_action, bool split, bool pullup, Mux* mux) : SwitchButton(pin, vbutton, pullup, mux) {
|
||||||
if (double_action) {
|
if (double_action) {
|
||||||
if (split) {
|
if (split) {
|
||||||
this->type = BUTTON_PULSED_DOUBLE_ACTION_SPLIT;
|
this->type = BUTTON_PULSED_DOUBLE_ACTION_SPLIT;
|
||||||
|
@ -93,8 +88,8 @@ PulsedButton::PulsedButton(uint8_t pin, uint8_t vbutton, bool double_action, boo
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PulsedButton::Update(Joystick* js) {
|
void PulsedButton::Update(Joystick* js) {
|
||||||
if (!BouncerUpdate()) return false;
|
if (!BouncerUpdate()) return;
|
||||||
|
|
||||||
switch(type) {
|
switch(type) {
|
||||||
case BUTTON_PULSED:
|
case BUTTON_PULSED:
|
||||||
|
@ -108,8 +103,6 @@ bool PulsedButton::Update(Joystick* js) {
|
||||||
else js->PressButton(vbutton2);
|
else js->PressButton(vbutton2);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PulsedButton::ReleaseButtons(Joystick* js) {
|
void PulsedButton::ReleaseButtons(Joystick* js) {
|
||||||
|
@ -126,19 +119,11 @@ EncoderButton::EncoderButton(uint8_t pin1, uint8_t pin2, uint8_t vbutton) : Butt
|
||||||
this->last_value = encoder->read();
|
this->last_value = encoder->read();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool EncoderButton::Update(Joystick* js) {
|
void EncoderButton::Update(Joystick* js) {
|
||||||
bool changed = false;
|
|
||||||
long new_value = encoder->read();
|
long new_value = encoder->read();
|
||||||
if (new_value > last_value) {
|
if (new_value > last_value) js->PressButton(vbutton);
|
||||||
js->PressButton(vbutton);
|
else if (new_value < last_value) js->PressButton(vbutton2);
|
||||||
changed = true;
|
|
||||||
}
|
|
||||||
else if (new_value < last_value) {
|
|
||||||
js->PressButton(vbutton2);
|
|
||||||
changed = true;
|
|
||||||
}
|
|
||||||
last_value = new_value;
|
last_value = new_value;
|
||||||
return changed;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void EncoderButton::ReleaseButtons(Joystick* js) {
|
void EncoderButton::ReleaseButtons(Joystick* js) {
|
||||||
|
|
19
Button.h
19
Button.h
|
@ -25,7 +25,7 @@ enum ButtonType {
|
||||||
class Button {
|
class Button {
|
||||||
public:
|
public:
|
||||||
Button(uint8_t vbutton);
|
Button(uint8_t vbutton);
|
||||||
virtual bool Update(Joystick* js) = 0;
|
virtual void Update(Joystick* js) = 0;
|
||||||
virtual void ReleaseButtons(Joystick* js);
|
virtual void ReleaseButtons(Joystick* js);
|
||||||
ButtonType type;
|
ButtonType type;
|
||||||
|
|
||||||
|
@ -40,7 +40,8 @@ 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, bool analog_only);
|
SwitchButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux);
|
||||||
|
virtual void Update(Joystick* js) = 0;
|
||||||
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,14 +54,14 @@ 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, bool analog_only = false);
|
PassthruButton(uint8_t pin, uint8_t vbutton, bool pullup = true, Mux* mux = NULL);
|
||||||
bool Update(Joystick* js);
|
void 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, bool analog_only = false);
|
LatchedButton(uint8_t pin, uint8_t vbutton, bool pullup = true, Mux* mux = NULL);
|
||||||
bool Update(Joystick* js);
|
void Update(Joystick* js);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool pressed;
|
bool pressed;
|
||||||
|
@ -68,8 +69,8 @@ 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, bool analog_only = false);
|
PulsedButton(uint8_t pin, uint8_t vbutton, bool double_action = false, bool split = false, bool pullup = true, Mux* mux = NULL);
|
||||||
bool Update(Joystick* js);
|
void Update(Joystick* js);
|
||||||
void ReleaseButtons(Joystick* js);
|
void ReleaseButtons(Joystick* js);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -81,7 +82,7 @@ class PulsedButton : public SwitchButton {
|
||||||
class EncoderButton : public Button {
|
class EncoderButton : public Button {
|
||||||
public:
|
public:
|
||||||
EncoderButton(uint8_t pin1, uint8_t pin2, uint8_t vbutton);
|
EncoderButton(uint8_t pin1, uint8_t pin2, uint8_t vbutton);
|
||||||
bool Update(Joystick* js);
|
void Update(Joystick* js);
|
||||||
void ReleaseButtons(Joystick* js);
|
void ReleaseButtons(Joystick* js);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
27
Joystick.cpp
27
Joystick.cpp
|
@ -2,7 +2,6 @@
|
||||||
#include "Button.h"
|
#include "Button.h"
|
||||||
#include <Mux.h>
|
#include <Mux.h>
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
using namespace admux;
|
using namespace admux;
|
||||||
|
|
||||||
|
@ -40,36 +39,27 @@ void Joystick::Init() {
|
||||||
delay(100);
|
delay(100);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup, Mux* mux, bool analog_only) {
|
void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup, Mux* mux) {
|
||||||
Button *button;
|
Button *button;
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case BUTTON_PASSTHRU:
|
case BUTTON_PASSTHRU:
|
||||||
button = new PassthruButton(pin, _virtual_buttons, pullup, mux, analog_only);
|
button = new PassthruButton(pin, _virtual_buttons, pullup, mux);
|
||||||
_virtual_buttons++;
|
_virtual_buttons++;
|
||||||
break;
|
|
||||||
case BUTTON_PULSED:
|
case BUTTON_PULSED:
|
||||||
button = new PulsedButton(pin, _virtual_buttons, false, false, pullup, mux, analog_only);
|
button = new PulsedButton(pin, _virtual_buttons, false, false, pullup, mux);
|
||||||
_virtual_buttons++;
|
_virtual_buttons++;
|
||||||
break;
|
|
||||||
case BUTTON_PULSED_DOUBLE_ACTION:
|
case BUTTON_PULSED_DOUBLE_ACTION:
|
||||||
button = new PulsedButton(pin, _virtual_buttons, true, false, pullup, mux, analog_only);
|
button = new PulsedButton(pin, _virtual_buttons, true, false, pullup, mux);
|
||||||
_virtual_buttons++;
|
_virtual_buttons++;
|
||||||
break;
|
|
||||||
case BUTTON_PULSED_DOUBLE_ACTION_SPLIT:
|
case BUTTON_PULSED_DOUBLE_ACTION_SPLIT:
|
||||||
button = new PulsedButton(pin, _virtual_buttons, true, true, pullup, mux, analog_only);
|
button = new PulsedButton(pin, _virtual_buttons, true, true, pullup, mux);
|
||||||
_virtual_buttons += 2;
|
_virtual_buttons += 2;
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
_buttons[_num_buttons] = button;
|
_buttons[_num_buttons] = button;
|
||||||
_num_buttons++;
|
_num_buttons++;
|
||||||
if (_debug) {
|
|
||||||
char buffer[100];
|
|
||||||
sprintf(buffer, "Added button %d of type %d", _num_buttons - 1, button->type);
|
|
||||||
Serial.println(buffer);
|
|
||||||
}
|
|
||||||
if (type & _BUTTON_PULSED_TYPES) _have_pulsed_button = true;
|
if (type & _BUTTON_PULSED_TYPES) _have_pulsed_button = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,12 +90,7 @@ void Joystick::Update() {
|
||||||
JoyReport oldReport = _joyReport;
|
JoyReport oldReport = _joyReport;
|
||||||
|
|
||||||
for (uint8_t i = 0; i < _num_buttons; i++) {
|
for (uint8_t i = 0; i < _num_buttons; i++) {
|
||||||
bool changed = _buttons[i]->Update(this);
|
_buttons[i]->Update(this);
|
||||||
if (changed && _debug) {
|
|
||||||
char buffer[25];
|
|
||||||
sprintf(buffer, "Button %d changed state.", i);
|
|
||||||
Serial.println(buffer);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: implement this and also refactor it into a class or classes
|
// TODO: implement this and also refactor it into a class or classes
|
||||||
|
|
|
@ -38,9 +38,7 @@ 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.
|
||||||
// Setting `analogOnly` to true indicates your button *must* be read with analog code, such as the A6 and A7 pins
|
void AddButton(uint8_t pin, ButtonType type, bool pullup=true, Mux* mux=NULL);
|
||||||
// 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);
|
||||||
|
@ -48,8 +46,6 @@ class Joystick {
|
||||||
// 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);
|
||||||
|
|
||||||
|
|
||||||
// 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);
|
||||||
|
|
||||||
|
@ -70,7 +66,6 @@ class Joystick {
|
||||||
uint8_t _num_axes;
|
uint8_t _num_axes;
|
||||||
|
|
||||||
JoyReport _joyReport;
|
JoyReport _joyReport;
|
||||||
|
|
||||||
bool _debug;
|
bool _debug;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user