arduino-joystick/Matrix.cpp

40 lines
947 B
C++

#include "Matrix.h"
Matrix::Matrix(uint8_t* columns, bool inverted) {
this->columns = columns;
this->num_columns = sizeof(columns);
this->active_pin = num_columns;
this->inverted = inverted;
for (uint8_t i = 0; i < num_columns; i++) {
pinMode(columns[i], OUTPUT);
if (inverted) digitalWrite(columns[i], HIGH);
else digitalWrite(columns[i], LOW);
}
}
void Matrix::Activate(uint8_t column) {
if (column > num_columns) return; // TODO: throw an error here?
uint8_t pin = columns[column];
if (pin == active_pin) return;
if (active_pin != num_columns) {
_disable(active_pin);
}
_enable(pin);
active_pin = pin;
delayMicroseconds(10); // allow ample time for signal to propagate
}
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);
}