62 lines
1.2 KiB
C++
62 lines
1.2 KiB
C++
|
#include "Reader.h"
|
||
|
|
||
|
#include <Mux.h>
|
||
|
#include <Bounce2.h>
|
||
|
|
||
|
|
||
|
Reader::Reader(bool inverted) {
|
||
|
this->inverted = inverted;
|
||
|
}
|
||
|
|
||
|
bool Reader::On() {
|
||
|
bool state = bouncer.rose();
|
||
|
if (inverted) state = bouncer.fell();
|
||
|
return state;
|
||
|
}
|
||
|
|
||
|
uint8_t Reader::getMode(bool pullup) {
|
||
|
if (pullup) return INPUT_PULLUP;
|
||
|
return INPUT;
|
||
|
}
|
||
|
|
||
|
|
||
|
DirectReader::DirectReader(uint8_t pin, bool inverted) : Reader(inverted) {
|
||
|
uint8_t mode = getMode(inverted);
|
||
|
this->bouncer.attach(pin)
|
||
|
}
|
||
|
|
||
|
bool DirectReader::Update() {
|
||
|
return bouncer.update();
|
||
|
}
|
||
|
|
||
|
|
||
|
MuxReader::MuxReader(uint8_t channel, Mux* mux, bool inverted) : Reader(inverted) {
|
||
|
uint8_t mode = getMode(inverted);
|
||
|
this->bouncer.attach(mux->signalPin().pin, mode);
|
||
|
this->channel = channel;
|
||
|
}
|
||
|
|
||
|
bool MuxReader::Update() {
|
||
|
mux->channel(channel);
|
||
|
return bouncer.update();
|
||
|
}
|
||
|
|
||
|
|
||
|
MatrixReader::MatrixReader(uint8_t row, uint8_t col, bool inverted) : Reader(inverted) {
|
||
|
uint8_t mode = getMode(inverted);
|
||
|
this->bouncer.attach(row, mode);
|
||
|
this->col = col;
|
||
|
}
|
||
|
|
||
|
bool MatrixReader::Update() {
|
||
|
digitalWrite(col, LOW);
|
||
|
delayMicroseconds(10);
|
||
|
|
||
|
bool changed = bouncer.update();
|
||
|
|
||
|
digitalWrite(col, HIGH);
|
||
|
delayMicroseconds(10);
|
||
|
|
||
|
return changed;
|
||
|
}
|