diff --git a/src/kaleidoscope/hardware/ErgoDox.cpp b/src/kaleidoscope/hardware/ErgoDox.cpp index 990772e8..43965cde 100644 --- a/src/kaleidoscope/hardware/ErgoDox.cpp +++ b/src/kaleidoscope/hardware/ErgoDox.cpp @@ -92,6 +92,8 @@ void ErgoDox::readMatrixRow(uint8_t row) { void ErgoDox::readMatrix() { do_scan_ = false; + scanner_.reattachExpanderOnError(); + for (uint8_t row = 0; row < ROWS / 2; row++) { scanner_.selectRow(row); scanner_.selectRow(row + ROWS / 2); diff --git a/src/kaleidoscope/hardware/ErgoDoxScanner.cpp b/src/kaleidoscope/hardware/ErgoDoxScanner.cpp index 2db4c139..630dc3d3 100644 --- a/src/kaleidoscope/hardware/ErgoDoxScanner.cpp +++ b/src/kaleidoscope/hardware/ErgoDoxScanner.cpp @@ -45,11 +45,7 @@ namespace hardware { uint8_t ErgoDoxScanner::initExpander() { uint8_t status = 0x20; - if (i2c_initialized_ == false) { - i2c_init(); - i2c_initialized_ = true; - _delay_ms(1000); - } + i2c_init(); status = i2c_start(I2C_ADDR_WRITE); if (status) @@ -187,5 +183,18 @@ out: } } +void +ErgoDoxScanner::reattachExpanderOnError() { + static uint8_t attach_counter; + + if (!expander_error_) + return; + + if (attach_counter++ != 0) + return; + + expander_error_ = initExpander(); +} + } } diff --git a/src/kaleidoscope/hardware/ErgoDoxScanner.h b/src/kaleidoscope/hardware/ErgoDoxScanner.h index 42302955..1439725d 100644 --- a/src/kaleidoscope/hardware/ErgoDoxScanner.h +++ b/src/kaleidoscope/hardware/ErgoDoxScanner.h @@ -40,8 +40,9 @@ class ErgoDoxScanner { void unselectRows(); uint8_t readCols(int row); + void reattachExpanderOnError(); + private: - bool i2c_initialized_ = false; uint8_t expander_error_ = 0x20; uint8_t initExpander();