From f8726905ea98ca80d972c5eb6a9162ff3bd2fe45 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Sun, 20 May 2018 09:59:53 +0200 Subject: [PATCH] Support detaching & re-attaching the expander Partly addresses #5. We still need to gracefully handle when the expander is not coming back. Signed-off-by: Gergely Nagy --- src/kaleidoscope/hardware/ErgoDox.cpp | 2 ++ src/kaleidoscope/hardware/ErgoDoxScanner.cpp | 19 ++++++++++++++----- src/kaleidoscope/hardware/ErgoDoxScanner.h | 3 ++- 3 files changed, 18 insertions(+), 6 deletions(-) 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();