diff --git a/src/detect/ScanI2CTwoWire.cpp b/src/detect/ScanI2CTwoWire.cpp index b04590509..86408b8d2 100644 --- a/src/detect/ScanI2CTwoWire.cpp +++ b/src/detect/ScanI2CTwoWire.cpp @@ -148,7 +148,7 @@ void ScanI2CTwoWire::scanPort(I2CPort port, uint8_t *address, uint8_t asize) { concurrency::LockGuard guard((concurrency::Lock *)&lock); - LOG_DEBUG("Scanning for i2c devices on port %d\n", port); + LOG_DEBUG("Scanning for I2C devices on port %d\n", port); uint8_t err; @@ -172,10 +172,11 @@ void ScanI2CTwoWire::scanPort(I2CPort port, uint8_t *address, uint8_t asize) #endif for (addr.address = 1; addr.address < 127; addr.address++) { - if (asize != 0) - if (in_array(address, asize, addr.address)) + if (asize != 0) { + if (!in_array(address, asize, addr.address)) continue; - + LOG_DEBUG("Scanning address 0x%x\n", addr.address); + } i2cBus->beginTransmission(addr.address); #ifdef ARCH_PORTDUINO if (i2cBus->read() != -1) @@ -369,7 +370,7 @@ void ScanI2CTwoWire::scanPort(I2CPort port, uint8_t *address, uint8_t asize) LOG_INFO("Device found at address 0x%x was not able to be enumerated\n", addr.address); } } else if (err == 4) { - LOG_ERROR("Unknown error at address 0x%x\n", addr); + LOG_ERROR("Unknown error at address 0x%x\n", addr.address); } // Check if a type was found for the enumerated device - save, if so diff --git a/src/input/cardKbI2cImpl.cpp b/src/input/cardKbI2cImpl.cpp index e000f36eb..d10f1118f 100644 --- a/src/input/cardKbI2cImpl.cpp +++ b/src/input/cardKbI2cImpl.cpp @@ -1,5 +1,7 @@ #include "cardKbI2cImpl.h" #include "InputBroker.h" +#include "detect/ScanI2C.h" +#include "detect/ScanI2CTwoWire.h" CardKbI2cImpl *cardKbI2cImpl; @@ -8,8 +10,44 @@ CardKbI2cImpl::CardKbI2cImpl() : KbI2cBase("cardKB") {} void CardKbI2cImpl::init() { if (cardkb_found.address == 0x00) { - disable(); - return; + LOG_DEBUG("Rescanning for I2C keyboard\n"); + uint8_t i2caddr_scan[] = {CARDKB_ADDR, TDECK_KB_ADDR, BBQ10_KB_ADDR}; + uint8_t i2caddr_asize = 3; + auto i2cScanner = std::unique_ptr(new ScanI2CTwoWire()); + +#if defined(I2C_SDA1) + i2cScanner->scanPort(ScanI2C::I2CPort::WIRE1, i2caddr_scan, i2caddr_asize); +#endif + i2cScanner->scanPort(ScanI2C::I2CPort::WIRE, i2caddr_scan, i2caddr_asize); + auto kb_info = i2cScanner->firstKeyboard(); + + if (kb_info.type != ScanI2C::DeviceType::NONE) { + cardkb_found = kb_info.address; + switch (kb_info.type) { + case ScanI2C::DeviceType::RAK14004: + kb_model = 0x02; + break; + case ScanI2C::DeviceType::CARDKB: + kb_model = 0x00; + break; + case ScanI2C::DeviceType::TDECKKB: + // assign an arbitrary value to distinguish from other models + kb_model = 0x10; + break; + case ScanI2C::DeviceType::BBQ10KB: + // assign an arbitrary value to distinguish from other models + kb_model = 0x11; + break; + default: + // use this as default since it's also just zero + LOG_WARN("kb_info.type is unknown(0x%02x), setting kb_model=0x00\n", kb_info.type); + kb_model = 0x00; + } + } + if (cardkb_found.address == 0x00) { + disable(); + return; + } } inputBroker->registerSource(this); diff --git a/src/input/kbI2cBase.cpp b/src/input/kbI2cBase.cpp index ce22edb93..024b16b9e 100644 --- a/src/input/kbI2cBase.cpp +++ b/src/input/kbI2cBase.cpp @@ -30,44 +30,6 @@ uint8_t read_from_14004(TwoWire *i2cBus, uint8_t reg, uint8_t *data, uint8_t len int32_t KbI2cBase::runOnce() { - if (cardkb_found.address == 0x00) { - // Input device is not detected. Rescan now. - auto i2cScanner = std::unique_ptr(new ScanI2CTwoWire()); - uint8_t i2caddr_scan[] = {CARDKB_ADDR, TDECK_KB_ADDR, BBQ10_KB_ADDR}; - uint8_t i2caddr_asize = 3; -#if defined(I2C_SDA1) - i2cScanner->scanPort(ScanI2C::I2CPort::WIRE1, i2caddr_scan, i2caddr_asize); -#endif - i2cScanner->scanPort(ScanI2C::I2CPort::WIRE, i2caddr_scan, i2caddr_asize); - auto kb_info = i2cScanner->firstKeyboard(); - - if (kb_info.type != ScanI2C::DeviceType::NONE) { - cardkb_found = kb_info.address; - switch (kb_info.type) { - case ScanI2C::DeviceType::RAK14004: - kb_model = 0x02; - break; - case ScanI2C::DeviceType::CARDKB: - kb_model = 0x00; - break; - case ScanI2C::DeviceType::TDECKKB: - // assign an arbitrary value to distinguish from other models - kb_model = 0x10; - break; - case ScanI2C::DeviceType::BBQ10KB: - // assign an arbitrary value to distinguish from other models - kb_model = 0x11; - break; - default: - // use this as default since it's also just zero - LOG_WARN("kb_info.type is unknown(0x%02x), setting kb_model=0x00\n", kb_info.type); - kb_model = 0x00; - } - } - if (cardkb_found.address == 0x00) - return INT32_MAX; - } - if (!i2cBus) { switch (cardkb_found.port) { case ScanI2C::WIRE1: