diff --git a/src/kaleidoscope/hardware/ez/ErgoDox.cpp b/src/kaleidoscope/hardware/ez/ErgoDox.cpp index 7150ef29..46f1fdd6 100644 --- a/src/kaleidoscope/hardware/ez/ErgoDox.cpp +++ b/src/kaleidoscope/hardware/ez/ErgoDox.cpp @@ -97,13 +97,13 @@ void __attribute__((optimize(3))) ErgoDox::readMatrix() { scanner_.reattachExpanderOnError(); for (uint8_t row = 0; row < ROWS / 2; row++) { - scanner_.selectRow(row); - scanner_.selectRow(row + ROWS / 2); + scanner_.selectExtenderRow(row); + scanner_.toggleATMegaRow(row); readMatrixRow(row); readMatrixRow(row + ROWS / 2); - scanner_.unselectRows(); + scanner_.toggleATMegaRow(row); } } diff --git a/src/kaleidoscope/hardware/ez/ErgoDox/ErgoDoxScanner.cpp b/src/kaleidoscope/hardware/ez/ErgoDox/ErgoDoxScanner.cpp index c2042c8f..d9fb49c0 100644 --- a/src/kaleidoscope/hardware/ez/ErgoDox/ErgoDoxScanner.cpp +++ b/src/kaleidoscope/hardware/ez/ErgoDox/ErgoDoxScanner.cpp @@ -85,80 +85,65 @@ out: 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 ErgoDoxScanner::begin() { expander_error_ = initExpander(); - unselectRows(); - initCols(); + // Init columns + 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))) -ErgoDoxScanner::selectRow(int row) { - if (row < 7) { - if (!expander_error_) { - expander_error_ = i2c_start(I2C_ADDR_WRITE); - if (expander_error_) - goto out; - expander_error_ = i2c_write(GPIOA); - if (expander_error_) - goto out; - expander_error_ = i2c_write(0xFF & ~(1 << row)); - if (expander_error_) - goto out; +void __attribute__((optimize(3))) ErgoDoxScanner::selectExtenderRow(int row) { + if (!expander_error_) { + expander_error_ = i2c_start(I2C_ADDR_WRITE); + if (expander_error_) + goto out; + expander_error_ = i2c_write(GPIOA); + if (expander_error_) + goto out; + expander_error_ = i2c_write(0xFF & ~(1 << row)); + if (expander_error_) + goto out; out: - i2c_stop(); - } - } else { - switch (row) { - case 7: - DDRB |= (1 << 0); - PORTB &= ~(1 << 0); + i2c_stop(); + } +} + +void __attribute__((optimize(3))) ErgoDoxScanner::toggleATMegaRow(int row) { + switch (row) { + case 0: + PORTB ^= (1 << 0); break; - case 8: - DDRB |= (1 << 1); - PORTB &= ~(1 << 1); + case 1: + PORTB ^= (1 << 1); break; - case 9: - DDRB |= (1 << 2); - PORTB &= ~(1 << 2); + case 2: + PORTB ^= (1 << 2); break; - case 10: - DDRB |= (1 << 3); - PORTB &= ~(1 << 3); + case 3: + PORTB ^= (1 << 3); break; - case 11: - DDRD |= (1 << 2); - PORTD &= ~(1 << 3); + case 4: + PORTD ^= (1 << 2); break; - case 12: - DDRD |= (1 << 3); - PORTD &= ~(1 << 3); + case 5: + PORTD ^= (1 << 3); break; - case 13: - DDRC |= (1 << 6); - PORTC &= ~(1 << 6); + case 6: + PORTC ^= (1 << 6); 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))) ErgoDoxScanner::readCols(int row) { if (row < 7) { diff --git a/src/kaleidoscope/hardware/ez/ErgoDox/ErgoDoxScanner.h b/src/kaleidoscope/hardware/ez/ErgoDox/ErgoDoxScanner.h index c7613e31..b5249655 100644 --- a/src/kaleidoscope/hardware/ez/ErgoDox/ErgoDoxScanner.h +++ b/src/kaleidoscope/hardware/ez/ErgoDox/ErgoDoxScanner.h @@ -38,9 +38,8 @@ class ErgoDoxScanner { ErgoDoxScanner() {} void begin(); - void initCols(); - void selectRow(int row); - void unselectRows(); + void toggleATMegaRow(int row); + void selectExtenderRow(int row); uint8_t readCols(int row); void reattachExpanderOnError();