diff --git a/src/kaleidoscope/hardware/ATMegaKeyboard.cpp b/src/kaleidoscope/hardware/ATMegaKeyboard.cpp index 2feaa996..230eb3de 100644 --- a/src/kaleidoscope/hardware/ATMegaKeyboard.cpp +++ b/src/kaleidoscope/hardware/ATMegaKeyboard.cpp @@ -40,11 +40,11 @@ void ATMegaKeyboard::setup(void) { OUTPUT_HIGH(KeyboardHardware.matrix_row_pins[i]); } - /* Set up Timer1 for 500usec */ + /* Set up Timer1 for 1700usec */ TCCR1B = _BV(WGM13); TCCR1A = 0; - const uint32_t cycles = (F_CPU / 2000000) * 500; + const uint32_t cycles = (F_CPU / 2000000) * 1700; ICR1 = cycles; TCCR1B = _BV(WGM13) | _BV(CS10); @@ -59,12 +59,10 @@ void ATMegaKeyboard::attachToHost() { UDCON &= ~_BV(DETACH); } -void ATMegaKeyboard::readMatrix(void) { +void __attribute__((optimize(3))) ATMegaKeyboard::readMatrix(void) { for (uint8_t current_row = 0; current_row < KeyboardHardware.matrix_rows; current_row++) { uint16_t mask, cols; - KeyboardHardware.previousKeyState_[current_row] = KeyboardHardware.keyState_[current_row]; - mask = KeyboardHardware.debounceMaskForRow(current_row); OUTPUT_TOGGLE(KeyboardHardware.matrix_row_pins[current_row]); @@ -95,7 +93,7 @@ bool ATMegaKeyboard::isKeyswitchPressed(uint8_t keyIndex) { keyIndex % KeyboardHardware.matrix_columns); } -void ATMegaKeyboard::actOnMatrixScan() { +void __attribute__((optimize(3))) ATMegaKeyboard::actOnMatrixScan() { for (byte row = 0; row < KeyboardHardware.matrix_rows; row++) { for (byte col = 0; col < KeyboardHardware.matrix_columns; col++) { uint8_t keyState = (bitRead(KeyboardHardware.previousKeyState_[row], col) << 0) | @@ -109,12 +107,12 @@ void ATMegaKeyboard::actOnMatrixScan() { } void ATMegaKeyboard::scanMatrix() { - if (!do_scan_) - return; - - do_scan_ = false; - - KeyboardHardware.readMatrix(); + if (do_scan_) { + do_scan_ = false; + // We only want to update our matrix if the timer has expired. + KeyboardHardware.readMatrix(); + } + // We ALWAYS want to tell Kaleidoscope about the state of the matrix KeyboardHardware.actOnMatrixScan(); } @@ -142,6 +140,7 @@ bool ATMegaKeyboard::isKeyMasked(byte row, byte col) { uint16_t ATMegaKeyboard::readCols() { uint16_t results = 0x00 ; for (uint8_t i = 0; i < KeyboardHardware.matrix_columns; i++) { + asm("NOP"); // We need to pause a beat before reading or we may read before the pin is hot results |= (!READ_PIN(KeyboardHardware.matrix_col_pins[i]) << i); } return results; diff --git a/src/kaleidoscope/hardware/ez/ErgoDox.cpp b/src/kaleidoscope/hardware/ez/ErgoDox.cpp index 5b183c3c..7150ef29 100644 --- a/src/kaleidoscope/hardware/ez/ErgoDox.cpp +++ b/src/kaleidoscope/hardware/ez/ErgoDox.cpp @@ -67,11 +67,11 @@ void ErgoDox::setup(void) { setStatusLEDBrightness(2, 15); setStatusLEDBrightness(3, 15); - /* Set up Timer1 for 500usec */ + /* Set up Timer1 for 1700usec */ TCCR1B = _BV(WGM13); TCCR1A = 0; - const uint32_t cycles = (F_CPU / 2000000) * 500; + const uint32_t cycles = (F_CPU / 2000000) * 1700; ICR1 = cycles; TCCR1B = _BV(WGM13) | _BV(CS10); @@ -82,17 +82,16 @@ ISR(TIMER1_OVF_vect) { do_scan_ = true; } -void ErgoDox::readMatrixRow(uint8_t row) { +void __attribute__((optimize(3))) ErgoDox::readMatrixRow(uint8_t row) { uint8_t mask, cols; - previousKeyState_[row] = keyState_[row]; mask = debounceMaskForRow(row); cols = (scanner_.readCols(row) & mask) | (keyState_[row] & ~mask); debounceRow(cols ^ keyState_[row], row); keyState_[row] = cols; } -void ErgoDox::readMatrix() { +void __attribute__((optimize(3))) ErgoDox::readMatrix() { do_scan_ = false; scanner_.reattachExpanderOnError(); @@ -108,7 +107,7 @@ void ErgoDox::readMatrix() { } } -void ErgoDox::actOnMatrixScan() { +void __attribute__((optimize(3))) ErgoDox::actOnMatrixScan() { for (byte row = 0; row < ROWS; row++) { for (byte col = 0; col < COLS; col++) { uint8_t keyState = (bitRead(previousKeyState_[row], col) << 0) | @@ -116,14 +115,18 @@ void ErgoDox::actOnMatrixScan() { if (keyState) handleKeyswitchEvent(Key_NoKey, row, col, keyState); } + previousKeyState_[row] = keyState_[row]; } } void ErgoDox::scanMatrix() { - if (!do_scan_) - return; + if (do_scan_) { + do_scan_ = false; + // We only want to update our matrix if the timer has expired. + readMatrix(); + } - readMatrix(); + // We ALWAYS want to tell Kaleidoscope about the state of the matrix actOnMatrixScan(); } diff --git a/src/kaleidoscope/hardware/ez/ErgoDox/ErgoDoxScanner.cpp b/src/kaleidoscope/hardware/ez/ErgoDox/ErgoDoxScanner.cpp index aed1c342..c2042c8f 100644 --- a/src/kaleidoscope/hardware/ez/ErgoDox/ErgoDoxScanner.cpp +++ b/src/kaleidoscope/hardware/ez/ErgoDox/ErgoDoxScanner.cpp @@ -99,7 +99,7 @@ ErgoDoxScanner::begin() { initCols(); } -void +void __attribute__((optimize(3))) ErgoDoxScanner::selectRow(int row) { if (row < 7) { if (!expander_error_) { @@ -149,7 +149,7 @@ out: } } -void +void __attribute__((optimize(3))) ErgoDoxScanner::unselectRows() { DDRB &= ~(1 << 0 | 1 << 1 | 1 << 2 | 1 << 3); PORTB &= ~(1 << 0 | 1 << 1 | 1 << 2 | 1 << 3); @@ -159,7 +159,7 @@ ErgoDoxScanner::unselectRows() { PORTC &= ~(1 << 6); } -uint8_t +uint8_t __attribute__((optimize(3))) ErgoDoxScanner::readCols(int row) { if (row < 7) { if (expander_error_) { diff --git a/src/kaleidoscope/hardware/olkb/Planck.cpp b/src/kaleidoscope/hardware/olkb/Planck.cpp index 2a70ba99..5597171f 100644 --- a/src/kaleidoscope/hardware/olkb/Planck.cpp +++ b/src/kaleidoscope/hardware/olkb/Planck.cpp @@ -18,6 +18,7 @@ #ifdef ARDUINO_AVR_PLANCK #include +#include namespace kaleidoscope { namespace hardware { diff --git a/src/kaleidoscope/hardware/technomancy/Atreus.cpp b/src/kaleidoscope/hardware/technomancy/Atreus.cpp index 1cc441dd..61a7c375 100644 --- a/src/kaleidoscope/hardware/technomancy/Atreus.cpp +++ b/src/kaleidoscope/hardware/technomancy/Atreus.cpp @@ -27,6 +27,7 @@ #ifdef ARDUINO_AVR_ATREUS #include +#include namespace kaleidoscope { namespace hardware { @@ -70,22 +71,6 @@ void Atreus::resetDevice() { asm volatile("jmp 0x7E00"); } -uint16_t Atreus::readCols() { - uint16_t results = 0; - results |= (!READ_PIN(KeyboardHardware.matrix_col_pins[0]) << 0); - results |= (!READ_PIN(KeyboardHardware.matrix_col_pins[1]) << 1); - results |= (!READ_PIN(KeyboardHardware.matrix_col_pins[2]) << 2); - results |= (!READ_PIN(KeyboardHardware.matrix_col_pins[3]) << 3); - results |= (!READ_PIN(KeyboardHardware.matrix_col_pins[4]) << 4); - results |= (!READ_PIN(KeyboardHardware.matrix_col_pins[5]) << 5); - results |= (!READ_PIN(KeyboardHardware.matrix_col_pins[6]) << 6); - results |= (!READ_PIN(KeyboardHardware.matrix_col_pins[7]) << 7); - results |= (!READ_PIN(KeyboardHardware.matrix_col_pins[8]) << 8); - results |= (!READ_PIN(KeyboardHardware.matrix_col_pins[9]) << 9); - results |= (!READ_PIN(KeyboardHardware.matrix_col_pins[10]) << 10); - - return results; -} } } diff --git a/src/kaleidoscope/hardware/technomancy/Atreus.h b/src/kaleidoscope/hardware/technomancy/Atreus.h index 1ac4c393..d4f0821b 100644 --- a/src/kaleidoscope/hardware/technomancy/Atreus.h +++ b/src/kaleidoscope/hardware/technomancy/Atreus.h @@ -52,7 +52,6 @@ class Atreus: public kaleidoscope::hardware::ATMegaKeyboard { void resetDevice(); protected: - uint16_t readCols(); }; #define KEYMAP( \