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
59
Reader.h
Normal file
59
Reader.h
Normal file
|
@ -0,0 +1,59 @@
|
|||
// "Readers" represent different methods for reading the state of a pin.
|
||||
// They also include debouncing logic using the Bounce2 library, when appropriate.
|
||||
|
||||
#ifndef _READER_H_
|
||||
#define _READER_H_
|
||||
|
||||
#include <Bounce2.h>
|
||||
#include <Mux.h>
|
||||
|
||||
// The abstract base class - a very simple interface
|
||||
class Reader {
|
||||
public:
|
||||
Reader(bool inverted);
|
||||
|
||||
virtual bool Update() = 0; // should return true if the state of the pin has changed
|
||||
bool On();
|
||||
|
||||
protected:
|
||||
uint8_t getMode(bool pullup);
|
||||
|
||||
Bounce bouncer;
|
||||
bool inverted;
|
||||
};
|
||||
|
||||
|
||||
// A standard reader; the button is connected directly to a switch
|
||||
class DirectReader {
|
||||
public:
|
||||
DirectReader(uint8_t pin, bool inverted = true);
|
||||
bool Update();
|
||||
bool On();
|
||||
};
|
||||
|
||||
|
||||
// The button is connected to a Multiplexer channel
|
||||
class MuxReader {
|
||||
public:
|
||||
MuxReader(uint8_t channel, Mux* mux, bool inverted = true);
|
||||
bool Update();
|
||||
bool On();
|
||||
|
||||
protected:
|
||||
Mux * mux;
|
||||
};
|
||||
|
||||
|
||||
// The button is connected via a scan matrix
|
||||
// "row" should always be the input, with "col" the selecter.
|
||||
class MatrixReader {
|
||||
public:
|
||||
MatrixReader(uint8_t row, uint8_t col, bool inverted = true);
|
||||
bool Update();
|
||||
bool On();
|
||||
|
||||
protected:
|
||||
uint8_t col;
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue