hardware/Atreus: From select/unselectRow, go to toggleRow

Instead of having a separate function for selecting and unselecting rows, have
only one that toggles them - we only ever want to toggle them anyway. While
there, optimize it slightly, by not toggling `DDRD`: if we set those in `setup`,
they'll remain set, and there's no need to toggle them.

This makes the code cleaner and smaller, at the cost of four bytes of RAM.

Signed-off-by: Gergely Nagy <algernon@keyboard.io>
pull/454/head
Gergely Nagy 6 years ago
parent 6fb3f208f8
commit 495ef43b13
No known key found for this signature in database
GPG Key ID: AC1E90BAC433F68F

@ -46,12 +46,15 @@ uint16_t Atreus::masks_[ROWS];
uint8_t Atreus::debounce_matrix_[ROWS][COLS]; uint8_t Atreus::debounce_matrix_[ROWS][COLS];
uint8_t Atreus::debounce = 3; uint8_t Atreus::debounce = 3;
constexpr uint8_t Atreus::row_pins[4];
void Atreus::setup(void) { void Atreus::setup(void) {
wdt_disable(); wdt_disable();
delay(100); delay(100);
for (uint8_t i = 0; i < ROWS; i++) { for (uint8_t i = 0; i < ROWS; i++) {
unselectRow(i); DDRD |= _BV(row_pins[i]);
PORTD |= _BV(row_pins[i]);
keyState_[i] = previousKeyState_[i] = 0; keyState_[i] = previousKeyState_[i] = 0;
} }
@ -82,50 +85,8 @@ void Atreus::setup(void) {
TIMSK1 = _BV(TOIE1); TIMSK1 = _BV(TOIE1);
} }
void Atreus::selectRow(uint8_t row) { void Atreus::toggleRow(uint8_t row) {
switch (row) { PORTD ^= _BV(row_pins[row]);
case 0:
DDRD |= (_BV(0));
PORTD &= ~(_BV(0));
break;
case 1:
DDRD |= (_BV(1));
PORTD &= ~(_BV(1));
break;
case 2:
DDRD |= (_BV(3));
PORTD &= ~(_BV(3));
break;
case 3:
DDRD |= (_BV(2));
PORTD &= ~(_BV(2));
break;
default:
break;
}
}
void Atreus::unselectRow(uint8_t row) {
switch (row) {
case 0:
DDRD &= ~(_BV(0));
PORTD |= (_BV(0));
break;
case 1:
DDRD &= ~(_BV(1));
PORTD |= (_BV(1));
break;
case 2:
DDRD &= ~(_BV(3));
PORTD |= (_BV(3));
break;
case 3:
DDRD &= ~(_BV(2));
PORTD |= (_BV(2));
break;
default:
break;
}
} }
uint16_t Atreus::readCols() { uint16_t Atreus::readCols() {
@ -149,9 +110,9 @@ void Atreus::readMatrixRow(uint8_t current_row) {
mask = debounceMaskForRow(current_row); mask = debounceMaskForRow(current_row);
selectRow(current_row); toggleRow(current_row);
cols = (readCols() & mask) | (keyState_[current_row] & ~mask); cols = (readCols() & mask) | (keyState_[current_row] & ~mask);
unselectRow(current_row); toggleRow(current_row);
debounceRow(cols ^ keyState_[current_row], current_row); debounceRow(cols ^ keyState_[current_row], current_row);
keyState_[current_row] = cols; keyState_[current_row] = cols;
} }

@ -127,14 +127,15 @@ class Atreus {
static uint8_t debounce; static uint8_t debounce;
private: private:
static constexpr uint8_t row_pins[4] = {0, 1, 3, 2};
static uint16_t previousKeyState_[matrix_rows]; static uint16_t previousKeyState_[matrix_rows];
static uint16_t keyState_[matrix_rows]; static uint16_t keyState_[matrix_rows];
static uint16_t masks_[matrix_rows]; static uint16_t masks_[matrix_rows];
static void readMatrixRow(uint8_t row); static void readMatrixRow(uint8_t row);
static uint16_t readCols(); static uint16_t readCols();
static void selectRow(uint8_t row); static void toggleRow(uint8_t row);
static void unselectRow(uint8_t row);
static uint8_t debounce_matrix_[matrix_rows][matrix_columns]; static uint8_t debounce_matrix_[matrix_rows][matrix_columns];
static uint16_t debounceMaskForRow(uint8_t row); static uint16_t debounceMaskForRow(uint8_t row);

Loading…
Cancel
Save