arduino-joystick/Matrix.cpp

33 lines
699 B
C++
Raw Normal View History

#include "Matrix.h"
2021-11-24 05:40:28 +00:00
Matrix::Matrix(uint8_t* columns, uint8_t num_columns, bool inverted) {
this->active_pin = 255; // sentinel value, highest possible 8-bit number
this->inverted = inverted;
for (uint8_t i = 0; i < num_columns; i++) {
pinMode(columns[i], OUTPUT);
2021-11-24 05:40:28 +00:00
_disable(columns[i]);
}
}
2021-11-24 05:40:28 +00:00
void Matrix::Activate(uint8_t pin) {
if (pin == active_pin) return;
2021-11-24 05:40:28 +00:00
if (active_pin != 255) {
_disable(active_pin);
}
_enable(pin);
active_pin = pin;
}
void Matrix::_enable(uint8_t pin) {
if (inverted) digitalWrite(pin, LOW);
else digitalWrite(pin, HIGH);
}
void Matrix::_disable(uint8_t pin) {
if (inverted) digitalWrite(pin, HIGH);
else digitalWrite(pin, LOW);
}