hardware/ErgoDox: Improved row selection

Improved the ATMega parts of row toggling, similar to how `ATMegaKeyboard` does
it, and separated it from selecting extender rows. This results in a tiny
increase in speed, and better clarity.

Signed-off-by: Gergely Nagy <algernon@keyboard.io>
pull/474/head
Gergely Nagy 6 years ago
parent 873968404c
commit 392eac9903
No known key found for this signature in database
GPG Key ID: AC1E90BAC433F68F

@ -97,13 +97,13 @@ void __attribute__((optimize(3))) ErgoDox::readMatrix() {
scanner_.reattachExpanderOnError(); scanner_.reattachExpanderOnError();
for (uint8_t row = 0; row < ROWS / 2; row++) { for (uint8_t row = 0; row < ROWS / 2; row++) {
scanner_.selectRow(row); scanner_.selectExtenderRow(row);
scanner_.selectRow(row + ROWS / 2); scanner_.toggleATMegaRow(row);
readMatrixRow(row); readMatrixRow(row);
readMatrixRow(row + ROWS / 2); readMatrixRow(row + ROWS / 2);
scanner_.unselectRows(); scanner_.toggleATMegaRow(row);
} }
} }

@ -85,80 +85,65 @@ out:
return status; return status;
} }
void
ErgoDoxScanner::initCols() {
DDRF &= ~(1 << 7 | 1 << 6 | 1 << 5 | 1 << 4 | 1 << 1 | 1 << 0);
PORTF |= (1 << 7 | 1 << 6 | 1 << 5 | 1 << 4 | 1 << 1 | 1 << 0);
}
void void
ErgoDoxScanner::begin() { ErgoDoxScanner::begin() {
expander_error_ = initExpander(); expander_error_ = initExpander();
unselectRows(); // Init columns
initCols(); DDRF &= ~(1 << 7 | 1 << 6 | 1 << 5 | 1 << 4 | 1 << 1 | 1 << 0);
PORTF |= (1 << 7 | 1 << 6 | 1 << 5 | 1 << 4 | 1 << 1 | 1 << 0);
// Init rows
DDRB |= (1 << 0 | 1 << 1 | 1 << 2 | 1 << 3);
PORTB |= (1 << 0 | 1 << 1 | 1 << 2 | 1 << 3);
DDRD |= (1 << 2 | 1 << 3);
PORTD |= (1 << 2 | 1 << 3);
DDRC |= (1 << 6);
PORTC |= (1 << 6);
} }
void __attribute__((optimize(3))) void __attribute__((optimize(3))) ErgoDoxScanner::selectExtenderRow(int row) {
ErgoDoxScanner::selectRow(int row) { if (!expander_error_) {
if (row < 7) { expander_error_ = i2c_start(I2C_ADDR_WRITE);
if (!expander_error_) { if (expander_error_)
expander_error_ = i2c_start(I2C_ADDR_WRITE); goto out;
if (expander_error_) expander_error_ = i2c_write(GPIOA);
goto out; if (expander_error_)
expander_error_ = i2c_write(GPIOA); goto out;
if (expander_error_) expander_error_ = i2c_write(0xFF & ~(1 << row));
goto out; if (expander_error_)
expander_error_ = i2c_write(0xFF & ~(1 << row)); goto out;
if (expander_error_)
goto out;
out: out:
i2c_stop(); i2c_stop();
} }
} else { }
switch (row) {
case 7: void __attribute__((optimize(3))) ErgoDoxScanner::toggleATMegaRow(int row) {
DDRB |= (1 << 0); switch (row) {
PORTB &= ~(1 << 0); case 0:
PORTB ^= (1 << 0);
break; break;
case 8: case 1:
DDRB |= (1 << 1); PORTB ^= (1 << 1);
PORTB &= ~(1 << 1);
break; break;
case 9: case 2:
DDRB |= (1 << 2); PORTB ^= (1 << 2);
PORTB &= ~(1 << 2);
break; break;
case 10: case 3:
DDRB |= (1 << 3); PORTB ^= (1 << 3);
PORTB &= ~(1 << 3);
break; break;
case 11: case 4:
DDRD |= (1 << 2); PORTD ^= (1 << 2);
PORTD &= ~(1 << 3);
break; break;
case 12: case 5:
DDRD |= (1 << 3); PORTD ^= (1 << 3);
PORTD &= ~(1 << 3);
break; break;
case 13: case 6:
DDRC |= (1 << 6); PORTC ^= (1 << 6);
PORTC &= ~(1 << 6);
break; break;
}
} }
} }
void __attribute__((optimize(3)))
ErgoDoxScanner::unselectRows() {
DDRB &= ~(1 << 0 | 1 << 1 | 1 << 2 | 1 << 3);
PORTB &= ~(1 << 0 | 1 << 1 | 1 << 2 | 1 << 3);
DDRD &= ~(1 << 2 | 1 << 3);
PORTD &= ~(1 << 2 | 1 << 3);
DDRC &= ~(1 << 6);
PORTC &= ~(1 << 6);
}
uint8_t __attribute__((optimize(3))) uint8_t __attribute__((optimize(3)))
ErgoDoxScanner::readCols(int row) { ErgoDoxScanner::readCols(int row) {
if (row < 7) { if (row < 7) {

@ -38,9 +38,8 @@ class ErgoDoxScanner {
ErgoDoxScanner() {} ErgoDoxScanner() {}
void begin(); void begin();
void initCols(); void toggleATMegaRow(int row);
void selectRow(int row); void selectExtenderRow(int row);
void unselectRows();
uint8_t readCols(int row); uint8_t readCols(int row);
void reattachExpanderOnError(); void reattachExpanderOnError();

Loading…
Cancel
Save