Factor pin-reading logic into a new set of classes.
This commit is contained in:
parent
a4f00a0a23
commit
8ebc1a5523
6 changed files with 182 additions and 109 deletions
38
Button.h
38
Button.h
|
@ -2,8 +2,7 @@
|
|||
#define _BUTTON_H_
|
||||
|
||||
#include <Encoder.h>
|
||||
#include <Mux.h>
|
||||
#include <Bounce2.h>
|
||||
#include "Reader.h"
|
||||
|
||||
using namespace admux;
|
||||
|
||||
|
@ -24,53 +23,35 @@ enum ButtonType {
|
|||
// * an update method.
|
||||
class Button {
|
||||
public:
|
||||
Button(uint8_t vbutton);
|
||||
Button(uint8_t vbutton, Reader* reader);
|
||||
virtual bool Update(Joystick* js) = 0;
|
||||
virtual void ReleaseButtons(Joystick* js);
|
||||
ButtonType type;
|
||||
|
||||
protected:
|
||||
uint8_t vbutton;
|
||||
Reader *reader;
|
||||
};
|
||||
|
||||
// An abstract class for a momentary/pushbutton or toggle switch. Special properties:
|
||||
// * needs to be explicitly debounced
|
||||
// * may use the microcontroller's built-in pullup resistor mode to eliminate noise
|
||||
// * may be attached to a multiplexer. In this case, the `pin` value will be treated as a multiplexer channel,
|
||||
// and the multiplexer logic will be automatically invoked by Update()
|
||||
class SwitchButton : public Button {
|
||||
class PassthruButton : public Button {
|
||||
public:
|
||||
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 On();
|
||||
|
||||
protected:
|
||||
Bounce bouncer;
|
||||
bool inverted;
|
||||
Mux *mux;
|
||||
uint8_t channel_id;
|
||||
};
|
||||
|
||||
class PassthruButton : public SwitchButton {
|
||||
public:
|
||||
PassthruButton(uint8_t pin, uint8_t vbutton, bool pullup = true, Mux* mux = NULL, bool analog_only = false);
|
||||
PassthruButton(uint8_t vbutton, Reader* reader);
|
||||
bool Update(Joystick* js);
|
||||
};
|
||||
|
||||
class LatchedButton : public SwitchButton {
|
||||
class LatchedButton : public Button {
|
||||
public:
|
||||
LatchedButton(uint8_t pin, uint8_t vbutton, bool pullup = true, Mux* mux = NULL, bool analog_only = false);
|
||||
LatchedButton(uint8_t vbutton, Reader* reader);
|
||||
bool Update(Joystick* js);
|
||||
|
||||
protected:
|
||||
bool pressed;
|
||||
};
|
||||
|
||||
class PulsedButton : public SwitchButton {
|
||||
class PulsedButton : public Button {
|
||||
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 vbutton, Reader* reader, bool double_action = false, bool split = false);
|
||||
bool Update(Joystick* js);
|
||||
void ReleaseButtons(Joystick* js);
|
||||
|
||||
protected:
|
||||
bool double_action;
|
||||
|
@ -84,7 +65,6 @@ class EncoderButton : public Button {
|
|||
public:
|
||||
EncoderButton(uint8_t pin1, uint8_t pin2, uint8_t vbutton);
|
||||
bool Update(Joystick* js);
|
||||
void ReleaseButtons(Joystick* js);
|
||||
|
||||
protected:
|
||||
Encoder* encoder;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue