Factor pin-reading logic into a new set of classes.

This commit is contained in:
Anna Rose Wiggins 2021-11-22 19:12:44 +00:00
parent a4f00a0a23
commit 8ebc1a5523
6 changed files with 182 additions and 109 deletions

61
Reader.cpp Normal file
View file

@ -0,0 +1,61 @@
#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;
}