From 1ec0e750a3296eadb28bb0e57a4a5e3bab439a94 Mon Sep 17 00:00:00 2001 From: HarukiToreda <116696711+HarukiToreda@users.noreply.github.com> Date: Sat, 18 May 2024 22:14:22 -0400 Subject: [PATCH 01/15] Added fix for ESP32 --- src/detect/ScanI2C.cpp | 28 ++++++++++++++++++++++++++++ src/detect/ScanI2C.h | 1 + src/main.cpp | 3 +++ src/main.h | 3 ++- 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/detect/ScanI2C.cpp b/src/detect/ScanI2C.cpp index 149bb95f0..941fdf3e8 100644 --- a/src/detect/ScanI2C.cpp +++ b/src/detect/ScanI2C.cpp @@ -1,4 +1,6 @@ #include "ScanI2C.h" +#include "main.h" +#include const ScanI2C::DeviceAddress ScanI2C::ADDRESS_NONE = ScanI2C::DeviceAddress(); const ScanI2C::FoundDevice ScanI2C::DEVICE_NONE = ScanI2C::FoundDevice(ScanI2C::DeviceType::NONE, ADDRESS_NONE); @@ -27,7 +29,33 @@ ScanI2C::FoundDevice ScanI2C::firstRTC() const ScanI2C::DeviceType types[] = {RTC_RV3028, RTC_PCF8563}; return firstOfOrNONE(2, types); } +bool performScanForCardKB() { + // Example I2C scan code for CardKB (adjust as needed) + Wire.beginTransmission(CARDKB_I2C_ADDRESS); + if (Wire.endTransmission() == 0) { + return true; // CardKB detected + } + return false; // CardKB not detected +} +void scanForCardKB() { + const int maxRetries = 10; // Maximum number of retries + const int retryDelay = 100; // Delay between retries in milliseconds + for (int i = 0; i < maxRetries; ++i) { + // Perform the scan (example scan code, adjust as needed) + cardKBDetected = performScanForCardKB(); + + if (cardKBDetected) { + Serial.println("CardKB Keyboard detected."); + break; + } + + delay(retryDelay); // Wait before the next retry + } + if (!cardKBDetected) { + Serial.println("CardKB Keyboard not detected. Canned Message Module Disabled."); + } +} ScanI2C::FoundDevice ScanI2C::firstKeyboard() const { ScanI2C::DeviceType types[] = {CARDKB, TDECKKB, BBQ10KB, RAK14004}; diff --git a/src/detect/ScanI2C.h b/src/detect/ScanI2C.h index 6c01b9100..15668aecb 100644 --- a/src/detect/ScanI2C.h +++ b/src/detect/ScanI2C.h @@ -2,6 +2,7 @@ #include #include +bool performScanForCardKB(); class ScanI2C { diff --git a/src/main.cpp b/src/main.cpp index 4a9fef5d0..a3e9258ba 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,6 +35,8 @@ #include // #include +bool cardKBDetected = false; +void scanForCardKB(); #ifdef ARCH_ESP32 #if !MESHTASTIC_EXCLUDE_WEBSERVER #include "mesh/http/WebServer.h" @@ -374,6 +376,7 @@ void setup() // otherwise keyboard and touch screen will not work delay(800); #endif +scanForCardKB(); // Initial scan for CardKB // Currently only the tbeam has a PMU // PMU initialization needs to be placed before i2c scanning diff --git a/src/main.h b/src/main.h index db05a4734..737d424a7 100644 --- a/src/main.h +++ b/src/main.h @@ -21,7 +21,7 @@ extern NimbleBluetooth *nimbleBluetooth; #include "NRF52Bluetooth.h" extern NRF52Bluetooth *nrf52Bluetooth; #endif - +extern bool cardKBDetected; #if ARCH_PORTDUINO extern HardwareSPI *DisplaySPI; extern HardwareSPI *LoraSPI; @@ -39,6 +39,7 @@ extern bool pmu_found; extern bool isCharging; extern bool isUSBPowered; +#define CARDKB_I2C_ADDRESS 0x5F // Replace 0x5F with the actual address if different #if !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL) extern ATECCX08A atecc; #endif From a5fdb663e25055292bef3042110e9b8b77d9bf01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Sun, 19 May 2024 19:32:08 +0200 Subject: [PATCH 02/15] change the main scan class so they scan only for wanted bits - UNTESTED --- src/detect/ScanI2C.cpp | 29 +-------------------------- src/detect/ScanI2C.h | 2 +- src/detect/ScanI2CTwoWire.cpp | 13 ++++++++++-- src/detect/ScanI2CTwoWire.h | 4 +++- src/input/kbI2cBase.cpp | 37 +++++++++++++++++++++++++++++++++-- src/main.cpp | 3 --- src/main.h | 3 +-- 7 files changed, 52 insertions(+), 39 deletions(-) diff --git a/src/detect/ScanI2C.cpp b/src/detect/ScanI2C.cpp index 941fdf3e8..7d0a83653 100644 --- a/src/detect/ScanI2C.cpp +++ b/src/detect/ScanI2C.cpp @@ -1,6 +1,4 @@ #include "ScanI2C.h" -#include "main.h" -#include const ScanI2C::DeviceAddress ScanI2C::ADDRESS_NONE = ScanI2C::DeviceAddress(); const ScanI2C::FoundDevice ScanI2C::DEVICE_NONE = ScanI2C::FoundDevice(ScanI2C::DeviceType::NONE, ADDRESS_NONE); @@ -8,6 +6,7 @@ const ScanI2C::FoundDevice ScanI2C::DEVICE_NONE = ScanI2C::FoundDevice(ScanI2C:: ScanI2C::ScanI2C() = default; void ScanI2C::scanPort(ScanI2C::I2CPort port) {} +void ScanI2C::scanPort(ScanI2C::I2CPort port, int *address) {} void ScanI2C::setSuppressScreen() { @@ -29,33 +28,7 @@ ScanI2C::FoundDevice ScanI2C::firstRTC() const ScanI2C::DeviceType types[] = {RTC_RV3028, RTC_PCF8563}; return firstOfOrNONE(2, types); } -bool performScanForCardKB() { - // Example I2C scan code for CardKB (adjust as needed) - Wire.beginTransmission(CARDKB_I2C_ADDRESS); - if (Wire.endTransmission() == 0) { - return true; // CardKB detected - } - return false; // CardKB not detected -} -void scanForCardKB() { - const int maxRetries = 10; // Maximum number of retries - const int retryDelay = 100; // Delay between retries in milliseconds - for (int i = 0; i < maxRetries; ++i) { - // Perform the scan (example scan code, adjust as needed) - cardKBDetected = performScanForCardKB(); - - if (cardKBDetected) { - Serial.println("CardKB Keyboard detected."); - break; - } - - delay(retryDelay); // Wait before the next retry - } - if (!cardKBDetected) { - Serial.println("CardKB Keyboard not detected. Canned Message Module Disabled."); - } -} ScanI2C::FoundDevice ScanI2C::firstKeyboard() const { ScanI2C::DeviceType types[] = {CARDKB, TDECKKB, BBQ10KB, RAK14004}; diff --git a/src/detect/ScanI2C.h b/src/detect/ScanI2C.h index 15668aecb..4aa4549cc 100644 --- a/src/detect/ScanI2C.h +++ b/src/detect/ScanI2C.h @@ -2,7 +2,6 @@ #include #include -bool performScanForCardKB(); class ScanI2C { @@ -82,6 +81,7 @@ class ScanI2C ScanI2C(); virtual void scanPort(ScanI2C::I2CPort); + virtual void scanPort(ScanI2C::I2CPort, int *); /* * A bit of a hack, this tells the scanner not to tell later systems there is a screen to avoid enabling it. diff --git a/src/detect/ScanI2CTwoWire.cpp b/src/detect/ScanI2CTwoWire.cpp index 7828dfb58..f7068ee02 100644 --- a/src/detect/ScanI2CTwoWire.cpp +++ b/src/detect/ScanI2CTwoWire.cpp @@ -135,7 +135,7 @@ uint16_t ScanI2CTwoWire::getRegisterValue(const ScanI2CTwoWire::RegisterLocation type = T; \ break; -void ScanI2CTwoWire::scanPort(I2CPort port) +void ScanI2CTwoWire::scanPort(I2CPort port, int *address) { concurrency::LockGuard guard((concurrency::Lock *)&lock); @@ -163,6 +163,10 @@ void ScanI2CTwoWire::scanPort(I2CPort port) #endif for (addr.address = 1; addr.address < 127; addr.address++) { + // Skip the address if it is not requested oon a partial scan + if (sizeof(address) > 0 && addr.address != *address) { + continue; + } i2cBus->beginTransmission(addr.address); #ifdef ARCH_PORTDUINO if (i2cBus->read() != -1) @@ -353,6 +357,11 @@ void ScanI2CTwoWire::scanPort(I2CPort port) } } +void ScanI2CTwoWire::scanPort(I2CPort port) +{ + scanPort(port, nullptr); +} + TwoWire *ScanI2CTwoWire::fetchI2CBus(ScanI2C::DeviceAddress address) const { if (address.port == ScanI2C::I2CPort::WIRE) { @@ -369,4 +378,4 @@ TwoWire *ScanI2CTwoWire::fetchI2CBus(ScanI2C::DeviceAddress address) const size_t ScanI2CTwoWire::countDevices() const { return foundDevices.size(); -} +} \ No newline at end of file diff --git a/src/detect/ScanI2CTwoWire.h b/src/detect/ScanI2CTwoWire.h index 9acd736d2..a7c19c779 100644 --- a/src/detect/ScanI2CTwoWire.h +++ b/src/detect/ScanI2CTwoWire.h @@ -16,6 +16,8 @@ class ScanI2CTwoWire : public ScanI2C public: void scanPort(ScanI2C::I2CPort) override; + void scanPort(ScanI2C::I2CPort, int *) override; + ScanI2C::FoundDevice find(ScanI2C::DeviceType) const override; TwoWire *fetchI2CBus(ScanI2C::DeviceAddress) const; @@ -53,4 +55,4 @@ class ScanI2CTwoWire : public ScanI2C uint16_t getRegisterValue(const RegisterLocation &, ResponseWidth) const; DeviceType probeOLED(ScanI2C::DeviceAddress) const; -}; +}; \ No newline at end of file diff --git a/src/input/kbI2cBase.cpp b/src/input/kbI2cBase.cpp index af7c96b20..55f435fda 100644 --- a/src/input/kbI2cBase.cpp +++ b/src/input/kbI2cBase.cpp @@ -1,6 +1,7 @@ #include "kbI2cBase.h" #include "configuration.h" #include "detect/ScanI2C.h" +#include "detect/ScanI2CTwoWire.h" extern ScanI2C::DeviceAddress cardkb_found; extern uint8_t kb_model; @@ -30,8 +31,40 @@ 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. - return INT32_MAX; + // Input device is not detected. Rescan now. + auto i2cScanner = std::unique_ptr(new ScanI2CTwoWire()); + int i2caddr_scan[] = {CARDKB_ADDR, TDECK_KB_ADDR, BBQ10_KB_ADDR}; +#if defined(I2C_SDA1) + i2cScanner->scanPort(ScanI2C::I2CPort::WIRE1, i2caddr_scan); +#endif + i2cScanner->scanPort(ScanI2C::I2CPort::WIRE, i2caddr_scan); + 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) { diff --git a/src/main.cpp b/src/main.cpp index a3e9258ba..4a9fef5d0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,8 +35,6 @@ #include // #include -bool cardKBDetected = false; -void scanForCardKB(); #ifdef ARCH_ESP32 #if !MESHTASTIC_EXCLUDE_WEBSERVER #include "mesh/http/WebServer.h" @@ -376,7 +374,6 @@ void setup() // otherwise keyboard and touch screen will not work delay(800); #endif -scanForCardKB(); // Initial scan for CardKB // Currently only the tbeam has a PMU // PMU initialization needs to be placed before i2c scanning diff --git a/src/main.h b/src/main.h index 737d424a7..db05a4734 100644 --- a/src/main.h +++ b/src/main.h @@ -21,7 +21,7 @@ extern NimbleBluetooth *nimbleBluetooth; #include "NRF52Bluetooth.h" extern NRF52Bluetooth *nrf52Bluetooth; #endif -extern bool cardKBDetected; + #if ARCH_PORTDUINO extern HardwareSPI *DisplaySPI; extern HardwareSPI *LoraSPI; @@ -39,7 +39,6 @@ extern bool pmu_found; extern bool isCharging; extern bool isUSBPowered; -#define CARDKB_I2C_ADDRESS 0x5F // Replace 0x5F with the actual address if different #if !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL) extern ATECCX08A atecc; #endif From 3a628047ef40f34060e8c8f7e5132d24be45bcad Mon Sep 17 00:00:00 2001 From: HarukiToreda <116696711+HarukiToreda@users.noreply.github.com> Date: Sat, 18 May 2024 22:14:22 -0400 Subject: [PATCH 03/15] Added fix for ESP32 --- src/detect/ScanI2C.cpp | 28 ++++++++++++++++++++++++++++ src/detect/ScanI2C.h | 1 + src/main.cpp | 3 +++ src/main.h | 3 ++- 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/detect/ScanI2C.cpp b/src/detect/ScanI2C.cpp index 149bb95f0..941fdf3e8 100644 --- a/src/detect/ScanI2C.cpp +++ b/src/detect/ScanI2C.cpp @@ -1,4 +1,6 @@ #include "ScanI2C.h" +#include "main.h" +#include const ScanI2C::DeviceAddress ScanI2C::ADDRESS_NONE = ScanI2C::DeviceAddress(); const ScanI2C::FoundDevice ScanI2C::DEVICE_NONE = ScanI2C::FoundDevice(ScanI2C::DeviceType::NONE, ADDRESS_NONE); @@ -27,7 +29,33 @@ ScanI2C::FoundDevice ScanI2C::firstRTC() const ScanI2C::DeviceType types[] = {RTC_RV3028, RTC_PCF8563}; return firstOfOrNONE(2, types); } +bool performScanForCardKB() { + // Example I2C scan code for CardKB (adjust as needed) + Wire.beginTransmission(CARDKB_I2C_ADDRESS); + if (Wire.endTransmission() == 0) { + return true; // CardKB detected + } + return false; // CardKB not detected +} +void scanForCardKB() { + const int maxRetries = 10; // Maximum number of retries + const int retryDelay = 100; // Delay between retries in milliseconds + for (int i = 0; i < maxRetries; ++i) { + // Perform the scan (example scan code, adjust as needed) + cardKBDetected = performScanForCardKB(); + + if (cardKBDetected) { + Serial.println("CardKB Keyboard detected."); + break; + } + + delay(retryDelay); // Wait before the next retry + } + if (!cardKBDetected) { + Serial.println("CardKB Keyboard not detected. Canned Message Module Disabled."); + } +} ScanI2C::FoundDevice ScanI2C::firstKeyboard() const { ScanI2C::DeviceType types[] = {CARDKB, TDECKKB, BBQ10KB, RAK14004}; diff --git a/src/detect/ScanI2C.h b/src/detect/ScanI2C.h index 6c01b9100..15668aecb 100644 --- a/src/detect/ScanI2C.h +++ b/src/detect/ScanI2C.h @@ -2,6 +2,7 @@ #include #include +bool performScanForCardKB(); class ScanI2C { diff --git a/src/main.cpp b/src/main.cpp index 4a9fef5d0..a3e9258ba 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,6 +35,8 @@ #include // #include +bool cardKBDetected = false; +void scanForCardKB(); #ifdef ARCH_ESP32 #if !MESHTASTIC_EXCLUDE_WEBSERVER #include "mesh/http/WebServer.h" @@ -374,6 +376,7 @@ void setup() // otherwise keyboard and touch screen will not work delay(800); #endif +scanForCardKB(); // Initial scan for CardKB // Currently only the tbeam has a PMU // PMU initialization needs to be placed before i2c scanning diff --git a/src/main.h b/src/main.h index db05a4734..737d424a7 100644 --- a/src/main.h +++ b/src/main.h @@ -21,7 +21,7 @@ extern NimbleBluetooth *nimbleBluetooth; #include "NRF52Bluetooth.h" extern NRF52Bluetooth *nrf52Bluetooth; #endif - +extern bool cardKBDetected; #if ARCH_PORTDUINO extern HardwareSPI *DisplaySPI; extern HardwareSPI *LoraSPI; @@ -39,6 +39,7 @@ extern bool pmu_found; extern bool isCharging; extern bool isUSBPowered; +#define CARDKB_I2C_ADDRESS 0x5F // Replace 0x5F with the actual address if different #if !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL) extern ATECCX08A atecc; #endif From a37f309c0379dfa161f2dae41f319acdc0b3d0b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Sun, 19 May 2024 19:32:08 +0200 Subject: [PATCH 04/15] change the main scan class so they scan only for wanted bits - UNTESTED --- src/detect/ScanI2C.cpp | 29 +-------------------------- src/detect/ScanI2C.h | 2 +- src/detect/ScanI2CTwoWire.cpp | 13 ++++++++++-- src/detect/ScanI2CTwoWire.h | 4 +++- src/input/kbI2cBase.cpp | 37 +++++++++++++++++++++++++++++++++-- src/main.cpp | 3 --- src/main.h | 3 +-- 7 files changed, 52 insertions(+), 39 deletions(-) diff --git a/src/detect/ScanI2C.cpp b/src/detect/ScanI2C.cpp index 941fdf3e8..7d0a83653 100644 --- a/src/detect/ScanI2C.cpp +++ b/src/detect/ScanI2C.cpp @@ -1,6 +1,4 @@ #include "ScanI2C.h" -#include "main.h" -#include const ScanI2C::DeviceAddress ScanI2C::ADDRESS_NONE = ScanI2C::DeviceAddress(); const ScanI2C::FoundDevice ScanI2C::DEVICE_NONE = ScanI2C::FoundDevice(ScanI2C::DeviceType::NONE, ADDRESS_NONE); @@ -8,6 +6,7 @@ const ScanI2C::FoundDevice ScanI2C::DEVICE_NONE = ScanI2C::FoundDevice(ScanI2C:: ScanI2C::ScanI2C() = default; void ScanI2C::scanPort(ScanI2C::I2CPort port) {} +void ScanI2C::scanPort(ScanI2C::I2CPort port, int *address) {} void ScanI2C::setSuppressScreen() { @@ -29,33 +28,7 @@ ScanI2C::FoundDevice ScanI2C::firstRTC() const ScanI2C::DeviceType types[] = {RTC_RV3028, RTC_PCF8563}; return firstOfOrNONE(2, types); } -bool performScanForCardKB() { - // Example I2C scan code for CardKB (adjust as needed) - Wire.beginTransmission(CARDKB_I2C_ADDRESS); - if (Wire.endTransmission() == 0) { - return true; // CardKB detected - } - return false; // CardKB not detected -} -void scanForCardKB() { - const int maxRetries = 10; // Maximum number of retries - const int retryDelay = 100; // Delay between retries in milliseconds - for (int i = 0; i < maxRetries; ++i) { - // Perform the scan (example scan code, adjust as needed) - cardKBDetected = performScanForCardKB(); - - if (cardKBDetected) { - Serial.println("CardKB Keyboard detected."); - break; - } - - delay(retryDelay); // Wait before the next retry - } - if (!cardKBDetected) { - Serial.println("CardKB Keyboard not detected. Canned Message Module Disabled."); - } -} ScanI2C::FoundDevice ScanI2C::firstKeyboard() const { ScanI2C::DeviceType types[] = {CARDKB, TDECKKB, BBQ10KB, RAK14004}; diff --git a/src/detect/ScanI2C.h b/src/detect/ScanI2C.h index 15668aecb..4aa4549cc 100644 --- a/src/detect/ScanI2C.h +++ b/src/detect/ScanI2C.h @@ -2,7 +2,6 @@ #include #include -bool performScanForCardKB(); class ScanI2C { @@ -82,6 +81,7 @@ class ScanI2C ScanI2C(); virtual void scanPort(ScanI2C::I2CPort); + virtual void scanPort(ScanI2C::I2CPort, int *); /* * A bit of a hack, this tells the scanner not to tell later systems there is a screen to avoid enabling it. diff --git a/src/detect/ScanI2CTwoWire.cpp b/src/detect/ScanI2CTwoWire.cpp index 7828dfb58..f7068ee02 100644 --- a/src/detect/ScanI2CTwoWire.cpp +++ b/src/detect/ScanI2CTwoWire.cpp @@ -135,7 +135,7 @@ uint16_t ScanI2CTwoWire::getRegisterValue(const ScanI2CTwoWire::RegisterLocation type = T; \ break; -void ScanI2CTwoWire::scanPort(I2CPort port) +void ScanI2CTwoWire::scanPort(I2CPort port, int *address) { concurrency::LockGuard guard((concurrency::Lock *)&lock); @@ -163,6 +163,10 @@ void ScanI2CTwoWire::scanPort(I2CPort port) #endif for (addr.address = 1; addr.address < 127; addr.address++) { + // Skip the address if it is not requested oon a partial scan + if (sizeof(address) > 0 && addr.address != *address) { + continue; + } i2cBus->beginTransmission(addr.address); #ifdef ARCH_PORTDUINO if (i2cBus->read() != -1) @@ -353,6 +357,11 @@ void ScanI2CTwoWire::scanPort(I2CPort port) } } +void ScanI2CTwoWire::scanPort(I2CPort port) +{ + scanPort(port, nullptr); +} + TwoWire *ScanI2CTwoWire::fetchI2CBus(ScanI2C::DeviceAddress address) const { if (address.port == ScanI2C::I2CPort::WIRE) { @@ -369,4 +378,4 @@ TwoWire *ScanI2CTwoWire::fetchI2CBus(ScanI2C::DeviceAddress address) const size_t ScanI2CTwoWire::countDevices() const { return foundDevices.size(); -} +} \ No newline at end of file diff --git a/src/detect/ScanI2CTwoWire.h b/src/detect/ScanI2CTwoWire.h index 9acd736d2..a7c19c779 100644 --- a/src/detect/ScanI2CTwoWire.h +++ b/src/detect/ScanI2CTwoWire.h @@ -16,6 +16,8 @@ class ScanI2CTwoWire : public ScanI2C public: void scanPort(ScanI2C::I2CPort) override; + void scanPort(ScanI2C::I2CPort, int *) override; + ScanI2C::FoundDevice find(ScanI2C::DeviceType) const override; TwoWire *fetchI2CBus(ScanI2C::DeviceAddress) const; @@ -53,4 +55,4 @@ class ScanI2CTwoWire : public ScanI2C uint16_t getRegisterValue(const RegisterLocation &, ResponseWidth) const; DeviceType probeOLED(ScanI2C::DeviceAddress) const; -}; +}; \ No newline at end of file diff --git a/src/input/kbI2cBase.cpp b/src/input/kbI2cBase.cpp index af7c96b20..55f435fda 100644 --- a/src/input/kbI2cBase.cpp +++ b/src/input/kbI2cBase.cpp @@ -1,6 +1,7 @@ #include "kbI2cBase.h" #include "configuration.h" #include "detect/ScanI2C.h" +#include "detect/ScanI2CTwoWire.h" extern ScanI2C::DeviceAddress cardkb_found; extern uint8_t kb_model; @@ -30,8 +31,40 @@ 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. - return INT32_MAX; + // Input device is not detected. Rescan now. + auto i2cScanner = std::unique_ptr(new ScanI2CTwoWire()); + int i2caddr_scan[] = {CARDKB_ADDR, TDECK_KB_ADDR, BBQ10_KB_ADDR}; +#if defined(I2C_SDA1) + i2cScanner->scanPort(ScanI2C::I2CPort::WIRE1, i2caddr_scan); +#endif + i2cScanner->scanPort(ScanI2C::I2CPort::WIRE, i2caddr_scan); + 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) { diff --git a/src/main.cpp b/src/main.cpp index a3e9258ba..4a9fef5d0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,8 +35,6 @@ #include // #include -bool cardKBDetected = false; -void scanForCardKB(); #ifdef ARCH_ESP32 #if !MESHTASTIC_EXCLUDE_WEBSERVER #include "mesh/http/WebServer.h" @@ -376,7 +374,6 @@ void setup() // otherwise keyboard and touch screen will not work delay(800); #endif -scanForCardKB(); // Initial scan for CardKB // Currently only the tbeam has a PMU // PMU initialization needs to be placed before i2c scanning diff --git a/src/main.h b/src/main.h index 737d424a7..db05a4734 100644 --- a/src/main.h +++ b/src/main.h @@ -21,7 +21,7 @@ extern NimbleBluetooth *nimbleBluetooth; #include "NRF52Bluetooth.h" extern NRF52Bluetooth *nrf52Bluetooth; #endif -extern bool cardKBDetected; + #if ARCH_PORTDUINO extern HardwareSPI *DisplaySPI; extern HardwareSPI *LoraSPI; @@ -39,7 +39,6 @@ extern bool pmu_found; extern bool isCharging; extern bool isUSBPowered; -#define CARDKB_I2C_ADDRESS 0x5F // Replace 0x5F with the actual address if different #if !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL) extern ATECCX08A atecc; #endif From dca8615eaade72d847866058ce75795a17b1b38a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Fri, 24 May 2024 22:59:31 +0200 Subject: [PATCH 05/15] change type to 8 bit uint --- src/detect/ScanI2C.cpp | 2 +- src/detect/ScanI2C.h | 2 +- src/detect/ScanI2CTwoWire.cpp | 6 +++--- src/detect/ScanI2CTwoWire.h | 2 +- src/input/kbI2cBase.cpp | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/detect/ScanI2C.cpp b/src/detect/ScanI2C.cpp index 7d0a83653..f3057c81a 100644 --- a/src/detect/ScanI2C.cpp +++ b/src/detect/ScanI2C.cpp @@ -6,7 +6,7 @@ const ScanI2C::FoundDevice ScanI2C::DEVICE_NONE = ScanI2C::FoundDevice(ScanI2C:: ScanI2C::ScanI2C() = default; void ScanI2C::scanPort(ScanI2C::I2CPort port) {} -void ScanI2C::scanPort(ScanI2C::I2CPort port, int *address) {} +void ScanI2C::scanPort(ScanI2C::I2CPort port, uint8_t *address) {} void ScanI2C::setSuppressScreen() { diff --git a/src/detect/ScanI2C.h b/src/detect/ScanI2C.h index 4aa4549cc..64c9ddbc2 100644 --- a/src/detect/ScanI2C.h +++ b/src/detect/ScanI2C.h @@ -81,7 +81,7 @@ class ScanI2C ScanI2C(); virtual void scanPort(ScanI2C::I2CPort); - virtual void scanPort(ScanI2C::I2CPort, int *); + virtual void scanPort(ScanI2C::I2CPort, uint8_t *); /* * A bit of a hack, this tells the scanner not to tell later systems there is a screen to avoid enabling it. diff --git a/src/detect/ScanI2CTwoWire.cpp b/src/detect/ScanI2CTwoWire.cpp index f7068ee02..06b94d6af 100644 --- a/src/detect/ScanI2CTwoWire.cpp +++ b/src/detect/ScanI2CTwoWire.cpp @@ -135,7 +135,7 @@ uint16_t ScanI2CTwoWire::getRegisterValue(const ScanI2CTwoWire::RegisterLocation type = T; \ break; -void ScanI2CTwoWire::scanPort(I2CPort port, int *address) +void ScanI2CTwoWire::scanPort(I2CPort port, uint8_t *address) { concurrency::LockGuard guard((concurrency::Lock *)&lock); @@ -163,8 +163,8 @@ void ScanI2CTwoWire::scanPort(I2CPort port, int *address) #endif for (addr.address = 1; addr.address < 127; addr.address++) { - // Skip the address if it is not requested oon a partial scan - if (sizeof(address) > 0 && addr.address != *address) { + // Skip the address if it is not requested on a partial scan + if (address != nullptr && *address != addr.address) { continue; } i2cBus->beginTransmission(addr.address); diff --git a/src/detect/ScanI2CTwoWire.h b/src/detect/ScanI2CTwoWire.h index a7c19c779..332afbf64 100644 --- a/src/detect/ScanI2CTwoWire.h +++ b/src/detect/ScanI2CTwoWire.h @@ -16,7 +16,7 @@ class ScanI2CTwoWire : public ScanI2C public: void scanPort(ScanI2C::I2CPort) override; - void scanPort(ScanI2C::I2CPort, int *) override; + void scanPort(ScanI2C::I2CPort, uint8_t *) override; ScanI2C::FoundDevice find(ScanI2C::DeviceType) const override; diff --git a/src/input/kbI2cBase.cpp b/src/input/kbI2cBase.cpp index 55f435fda..135de1716 100644 --- a/src/input/kbI2cBase.cpp +++ b/src/input/kbI2cBase.cpp @@ -33,7 +33,7 @@ int32_t KbI2cBase::runOnce() if (cardkb_found.address == 0x00) { // Input device is not detected. Rescan now. auto i2cScanner = std::unique_ptr(new ScanI2CTwoWire()); - int i2caddr_scan[] = {CARDKB_ADDR, TDECK_KB_ADDR, BBQ10_KB_ADDR}; + uint8_t i2caddr_scan[] = {CARDKB_ADDR, TDECK_KB_ADDR, BBQ10_KB_ADDR}; #if defined(I2C_SDA1) i2cScanner->scanPort(ScanI2C::I2CPort::WIRE1, i2caddr_scan); #endif From c46c3427f0c6ae8809e0aec3a731792f724dcd85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Sat, 25 May 2024 12:37:55 +0200 Subject: [PATCH 06/15] Iterate through uint array --- src/detect/ScanI2C.cpp | 2 +- src/detect/ScanI2C.h | 2 +- src/detect/ScanI2CTwoWire.cpp | 21 +++++++++++++++------ src/detect/ScanI2CTwoWire.h | 2 +- src/input/kbI2cBase.cpp | 5 +++-- 5 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/detect/ScanI2C.cpp b/src/detect/ScanI2C.cpp index f3057c81a..525780af6 100644 --- a/src/detect/ScanI2C.cpp +++ b/src/detect/ScanI2C.cpp @@ -6,7 +6,7 @@ const ScanI2C::FoundDevice ScanI2C::DEVICE_NONE = ScanI2C::FoundDevice(ScanI2C:: ScanI2C::ScanI2C() = default; void ScanI2C::scanPort(ScanI2C::I2CPort port) {} -void ScanI2C::scanPort(ScanI2C::I2CPort port, uint8_t *address) {} +void ScanI2C::scanPort(ScanI2C::I2CPort port, uint8_t *address, uint8_t asize) {} void ScanI2C::setSuppressScreen() { diff --git a/src/detect/ScanI2C.h b/src/detect/ScanI2C.h index 64c9ddbc2..4084c4479 100644 --- a/src/detect/ScanI2C.h +++ b/src/detect/ScanI2C.h @@ -81,7 +81,7 @@ class ScanI2C ScanI2C(); virtual void scanPort(ScanI2C::I2CPort); - virtual void scanPort(ScanI2C::I2CPort, uint8_t *); + virtual void scanPort(ScanI2C::I2CPort, uint8_t *, uint8_t); /* * A bit of a hack, this tells the scanner not to tell later systems there is a screen to avoid enabling it. diff --git a/src/detect/ScanI2CTwoWire.cpp b/src/detect/ScanI2CTwoWire.cpp index 06b94d6af..3376d23be 100644 --- a/src/detect/ScanI2CTwoWire.cpp +++ b/src/detect/ScanI2CTwoWire.cpp @@ -14,6 +14,15 @@ #define XPOWERS_AXP192_AXP2101_ADDRESS 0x34 #endif +bool in_array(uint8_t *array, int size, uint8_t lookfor) +{ + int i; + for (i = 0; i < size; i++) + if (lookfor == array[i]) + return true; + return false; +} + ScanI2C::FoundDevice ScanI2CTwoWire::find(ScanI2C::DeviceType type) const { concurrency::LockGuard guard((concurrency::Lock *)&lock); @@ -135,7 +144,7 @@ uint16_t ScanI2CTwoWire::getRegisterValue(const ScanI2CTwoWire::RegisterLocation type = T; \ break; -void ScanI2CTwoWire::scanPort(I2CPort port, uint8_t *address) +void ScanI2CTwoWire::scanPort(I2CPort port, uint8_t *address, uint8_t asize) { concurrency::LockGuard guard((concurrency::Lock *)&lock); @@ -163,10 +172,10 @@ void ScanI2CTwoWire::scanPort(I2CPort port, uint8_t *address) #endif for (addr.address = 1; addr.address < 127; addr.address++) { - // Skip the address if it is not requested on a partial scan - if (address != nullptr && *address != addr.address) { - continue; - } + if (asize != 0) + if (in_array(address, asize, addr.address)) + continue; + i2cBus->beginTransmission(addr.address); #ifdef ARCH_PORTDUINO if (i2cBus->read() != -1) @@ -359,7 +368,7 @@ void ScanI2CTwoWire::scanPort(I2CPort port, uint8_t *address) void ScanI2CTwoWire::scanPort(I2CPort port) { - scanPort(port, nullptr); + scanPort(port, nullptr, 0); } TwoWire *ScanI2CTwoWire::fetchI2CBus(ScanI2C::DeviceAddress address) const diff --git a/src/detect/ScanI2CTwoWire.h b/src/detect/ScanI2CTwoWire.h index 332afbf64..82b48f6b4 100644 --- a/src/detect/ScanI2CTwoWire.h +++ b/src/detect/ScanI2CTwoWire.h @@ -16,7 +16,7 @@ class ScanI2CTwoWire : public ScanI2C public: void scanPort(ScanI2C::I2CPort) override; - void scanPort(ScanI2C::I2CPort, uint8_t *) override; + void scanPort(ScanI2C::I2CPort, uint8_t *, uint8_t) override; ScanI2C::FoundDevice find(ScanI2C::DeviceType) const override; diff --git a/src/input/kbI2cBase.cpp b/src/input/kbI2cBase.cpp index 135de1716..ce22edb93 100644 --- a/src/input/kbI2cBase.cpp +++ b/src/input/kbI2cBase.cpp @@ -34,10 +34,11 @@ int32_t KbI2cBase::runOnce() // 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); + i2cScanner->scanPort(ScanI2C::I2CPort::WIRE1, i2caddr_scan, i2caddr_asize); #endif - i2cScanner->scanPort(ScanI2C::I2CPort::WIRE, i2caddr_scan); + i2cScanner->scanPort(ScanI2C::I2CPort::WIRE, i2caddr_scan, i2caddr_asize); auto kb_info = i2cScanner->firstKeyboard(); if (kb_info.type != ScanI2C::DeviceType::NONE) { From 27bb3506d390c2a77cd52a8ceae7f04c60cafce4 Mon Sep 17 00:00:00 2001 From: HarukiToreda <116696711+HarukiToreda@users.noreply.github.com> Date: Sat, 18 May 2024 22:14:22 -0400 Subject: [PATCH 07/15] Added fix for ESP32 --- src/detect/ScanI2C.cpp | 28 ++++++++++++++++++++++++++++ src/detect/ScanI2C.h | 1 + src/main.cpp | 3 +++ src/main.h | 3 ++- 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/detect/ScanI2C.cpp b/src/detect/ScanI2C.cpp index 3231f7054..ad0171118 100644 --- a/src/detect/ScanI2C.cpp +++ b/src/detect/ScanI2C.cpp @@ -1,4 +1,6 @@ #include "ScanI2C.h" +#include "main.h" +#include const ScanI2C::DeviceAddress ScanI2C::ADDRESS_NONE = ScanI2C::DeviceAddress(); const ScanI2C::FoundDevice ScanI2C::DEVICE_NONE = ScanI2C::FoundDevice(ScanI2C::DeviceType::NONE, ADDRESS_NONE); @@ -27,7 +29,33 @@ ScanI2C::FoundDevice ScanI2C::firstRTC() const ScanI2C::DeviceType types[] = {RTC_RV3028, RTC_PCF8563}; return firstOfOrNONE(2, types); } +bool performScanForCardKB() { + // Example I2C scan code for CardKB (adjust as needed) + Wire.beginTransmission(CARDKB_I2C_ADDRESS); + if (Wire.endTransmission() == 0) { + return true; // CardKB detected + } + return false; // CardKB not detected +} +void scanForCardKB() { + const int maxRetries = 10; // Maximum number of retries + const int retryDelay = 100; // Delay between retries in milliseconds + for (int i = 0; i < maxRetries; ++i) { + // Perform the scan (example scan code, adjust as needed) + cardKBDetected = performScanForCardKB(); + + if (cardKBDetected) { + Serial.println("CardKB Keyboard detected."); + break; + } + + delay(retryDelay); // Wait before the next retry + } + if (!cardKBDetected) { + Serial.println("CardKB Keyboard not detected. Canned Message Module Disabled."); + } +} ScanI2C::FoundDevice ScanI2C::firstKeyboard() const { ScanI2C::DeviceType types[] = {CARDKB, TDECKKB, BBQ10KB, RAK14004}; diff --git a/src/detect/ScanI2C.h b/src/detect/ScanI2C.h index dcc1f40ae..ee691e864 100644 --- a/src/detect/ScanI2C.h +++ b/src/detect/ScanI2C.h @@ -2,6 +2,7 @@ #include #include +bool performScanForCardKB(); class ScanI2C { diff --git a/src/main.cpp b/src/main.cpp index 6797c8375..9529ca7b3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,6 +35,8 @@ #include // #include +bool cardKBDetected = false; +void scanForCardKB(); #ifdef ARCH_ESP32 #if !MESHTASTIC_EXCLUDE_WEBSERVER #include "mesh/http/WebServer.h" @@ -376,6 +378,7 @@ void setup() // otherwise keyboard and touch screen will not work delay(800); #endif +scanForCardKB(); // Initial scan for CardKB // Currently only the tbeam has a PMU // PMU initialization needs to be placed before i2c scanning diff --git a/src/main.h b/src/main.h index 2ef7edb3a..b1e1ac464 100644 --- a/src/main.h +++ b/src/main.h @@ -21,7 +21,7 @@ extern NimbleBluetooth *nimbleBluetooth; #include "NRF52Bluetooth.h" extern NRF52Bluetooth *nrf52Bluetooth; #endif - +extern bool cardKBDetected; #if ARCH_PORTDUINO extern HardwareSPI *DisplaySPI; extern HardwareSPI *LoraSPI; @@ -39,6 +39,7 @@ extern bool pmu_found; extern bool isCharging; extern bool isUSBPowered; +#define CARDKB_I2C_ADDRESS 0x5F // Replace 0x5F with the actual address if different #if !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL) extern ATECCX08A atecc; #endif From dbb254ba7a5c442aab440af19c2ea1a20327a48d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Sun, 19 May 2024 19:32:08 +0200 Subject: [PATCH 08/15] change the main scan class so they scan only for wanted bits - UNTESTED --- src/detect/ScanI2C.cpp | 29 +-------------------------- src/detect/ScanI2C.h | 2 +- src/detect/ScanI2CTwoWire.cpp | 11 ++++++++++- src/detect/ScanI2CTwoWire.h | 4 +++- src/input/kbI2cBase.cpp | 37 +++++++++++++++++++++++++++++++++-- src/main.cpp | 3 --- src/main.h | 3 +-- 7 files changed, 51 insertions(+), 38 deletions(-) diff --git a/src/detect/ScanI2C.cpp b/src/detect/ScanI2C.cpp index ad0171118..9c3a18644 100644 --- a/src/detect/ScanI2C.cpp +++ b/src/detect/ScanI2C.cpp @@ -1,6 +1,4 @@ #include "ScanI2C.h" -#include "main.h" -#include const ScanI2C::DeviceAddress ScanI2C::ADDRESS_NONE = ScanI2C::DeviceAddress(); const ScanI2C::FoundDevice ScanI2C::DEVICE_NONE = ScanI2C::FoundDevice(ScanI2C::DeviceType::NONE, ADDRESS_NONE); @@ -8,6 +6,7 @@ const ScanI2C::FoundDevice ScanI2C::DEVICE_NONE = ScanI2C::FoundDevice(ScanI2C:: ScanI2C::ScanI2C() = default; void ScanI2C::scanPort(ScanI2C::I2CPort port) {} +void ScanI2C::scanPort(ScanI2C::I2CPort port, int *address) {} void ScanI2C::setSuppressScreen() { @@ -29,33 +28,7 @@ ScanI2C::FoundDevice ScanI2C::firstRTC() const ScanI2C::DeviceType types[] = {RTC_RV3028, RTC_PCF8563}; return firstOfOrNONE(2, types); } -bool performScanForCardKB() { - // Example I2C scan code for CardKB (adjust as needed) - Wire.beginTransmission(CARDKB_I2C_ADDRESS); - if (Wire.endTransmission() == 0) { - return true; // CardKB detected - } - return false; // CardKB not detected -} -void scanForCardKB() { - const int maxRetries = 10; // Maximum number of retries - const int retryDelay = 100; // Delay between retries in milliseconds - for (int i = 0; i < maxRetries; ++i) { - // Perform the scan (example scan code, adjust as needed) - cardKBDetected = performScanForCardKB(); - - if (cardKBDetected) { - Serial.println("CardKB Keyboard detected."); - break; - } - - delay(retryDelay); // Wait before the next retry - } - if (!cardKBDetected) { - Serial.println("CardKB Keyboard not detected. Canned Message Module Disabled."); - } -} ScanI2C::FoundDevice ScanI2C::firstKeyboard() const { ScanI2C::DeviceType types[] = {CARDKB, TDECKKB, BBQ10KB, RAK14004}; diff --git a/src/detect/ScanI2C.h b/src/detect/ScanI2C.h index ee691e864..1facb897a 100644 --- a/src/detect/ScanI2C.h +++ b/src/detect/ScanI2C.h @@ -2,7 +2,6 @@ #include #include -bool performScanForCardKB(); class ScanI2C { @@ -89,6 +88,7 @@ class ScanI2C ScanI2C(); virtual void scanPort(ScanI2C::I2CPort); + virtual void scanPort(ScanI2C::I2CPort, int *); /* * A bit of a hack, this tells the scanner not to tell later systems there is a screen to avoid enabling it. diff --git a/src/detect/ScanI2CTwoWire.cpp b/src/detect/ScanI2CTwoWire.cpp index 6766db014..dd70db8b7 100644 --- a/src/detect/ScanI2CTwoWire.cpp +++ b/src/detect/ScanI2CTwoWire.cpp @@ -135,7 +135,7 @@ uint16_t ScanI2CTwoWire::getRegisterValue(const ScanI2CTwoWire::RegisterLocation type = T; \ break; -void ScanI2CTwoWire::scanPort(I2CPort port) +void ScanI2CTwoWire::scanPort(I2CPort port, int *address) { concurrency::LockGuard guard((concurrency::Lock *)&lock); @@ -163,6 +163,10 @@ void ScanI2CTwoWire::scanPort(I2CPort port) #endif for (addr.address = 1; addr.address < 127; addr.address++) { + // Skip the address if it is not requested oon a partial scan + if (sizeof(address) > 0 && addr.address != *address) { + continue; + } i2cBus->beginTransmission(addr.address); #ifdef ARCH_PORTDUINO if (i2cBus->read() != -1) @@ -367,6 +371,11 @@ void ScanI2CTwoWire::scanPort(I2CPort port) } } +void ScanI2CTwoWire::scanPort(I2CPort port) +{ + scanPort(port, nullptr); +} + TwoWire *ScanI2CTwoWire::fetchI2CBus(ScanI2C::DeviceAddress address) const { if (address.port == ScanI2C::I2CPort::WIRE) { diff --git a/src/detect/ScanI2CTwoWire.h b/src/detect/ScanI2CTwoWire.h index 9acd736d2..a7c19c779 100644 --- a/src/detect/ScanI2CTwoWire.h +++ b/src/detect/ScanI2CTwoWire.h @@ -16,6 +16,8 @@ class ScanI2CTwoWire : public ScanI2C public: void scanPort(ScanI2C::I2CPort) override; + void scanPort(ScanI2C::I2CPort, int *) override; + ScanI2C::FoundDevice find(ScanI2C::DeviceType) const override; TwoWire *fetchI2CBus(ScanI2C::DeviceAddress) const; @@ -53,4 +55,4 @@ class ScanI2CTwoWire : public ScanI2C uint16_t getRegisterValue(const RegisterLocation &, ResponseWidth) const; DeviceType probeOLED(ScanI2C::DeviceAddress) const; -}; +}; \ No newline at end of file diff --git a/src/input/kbI2cBase.cpp b/src/input/kbI2cBase.cpp index af7c96b20..55f435fda 100644 --- a/src/input/kbI2cBase.cpp +++ b/src/input/kbI2cBase.cpp @@ -1,6 +1,7 @@ #include "kbI2cBase.h" #include "configuration.h" #include "detect/ScanI2C.h" +#include "detect/ScanI2CTwoWire.h" extern ScanI2C::DeviceAddress cardkb_found; extern uint8_t kb_model; @@ -30,8 +31,40 @@ 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. - return INT32_MAX; + // Input device is not detected. Rescan now. + auto i2cScanner = std::unique_ptr(new ScanI2CTwoWire()); + int i2caddr_scan[] = {CARDKB_ADDR, TDECK_KB_ADDR, BBQ10_KB_ADDR}; +#if defined(I2C_SDA1) + i2cScanner->scanPort(ScanI2C::I2CPort::WIRE1, i2caddr_scan); +#endif + i2cScanner->scanPort(ScanI2C::I2CPort::WIRE, i2caddr_scan); + 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) { diff --git a/src/main.cpp b/src/main.cpp index 9529ca7b3..6797c8375 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,8 +35,6 @@ #include // #include -bool cardKBDetected = false; -void scanForCardKB(); #ifdef ARCH_ESP32 #if !MESHTASTIC_EXCLUDE_WEBSERVER #include "mesh/http/WebServer.h" @@ -378,7 +376,6 @@ void setup() // otherwise keyboard and touch screen will not work delay(800); #endif -scanForCardKB(); // Initial scan for CardKB // Currently only the tbeam has a PMU // PMU initialization needs to be placed before i2c scanning diff --git a/src/main.h b/src/main.h index b1e1ac464..2ef7edb3a 100644 --- a/src/main.h +++ b/src/main.h @@ -21,7 +21,7 @@ extern NimbleBluetooth *nimbleBluetooth; #include "NRF52Bluetooth.h" extern NRF52Bluetooth *nrf52Bluetooth; #endif -extern bool cardKBDetected; + #if ARCH_PORTDUINO extern HardwareSPI *DisplaySPI; extern HardwareSPI *LoraSPI; @@ -39,7 +39,6 @@ extern bool pmu_found; extern bool isCharging; extern bool isUSBPowered; -#define CARDKB_I2C_ADDRESS 0x5F // Replace 0x5F with the actual address if different #if !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL) extern ATECCX08A atecc; #endif From ce9e63a2cb21ad943d708ae69fc6a0cbb62e5807 Mon Sep 17 00:00:00 2001 From: HarukiToreda <116696711+HarukiToreda@users.noreply.github.com> Date: Sat, 18 May 2024 22:14:22 -0400 Subject: [PATCH 09/15] Added fix for ESP32 --- src/detect/ScanI2C.cpp | 28 ++++++++++++++++++++++++++++ src/detect/ScanI2C.h | 1 + src/main.cpp | 3 +++ src/main.h | 3 ++- 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/detect/ScanI2C.cpp b/src/detect/ScanI2C.cpp index 9c3a18644..51718b15f 100644 --- a/src/detect/ScanI2C.cpp +++ b/src/detect/ScanI2C.cpp @@ -1,4 +1,6 @@ #include "ScanI2C.h" +#include "main.h" +#include const ScanI2C::DeviceAddress ScanI2C::ADDRESS_NONE = ScanI2C::DeviceAddress(); const ScanI2C::FoundDevice ScanI2C::DEVICE_NONE = ScanI2C::FoundDevice(ScanI2C::DeviceType::NONE, ADDRESS_NONE); @@ -28,7 +30,33 @@ ScanI2C::FoundDevice ScanI2C::firstRTC() const ScanI2C::DeviceType types[] = {RTC_RV3028, RTC_PCF8563}; return firstOfOrNONE(2, types); } +bool performScanForCardKB() { + // Example I2C scan code for CardKB (adjust as needed) + Wire.beginTransmission(CARDKB_I2C_ADDRESS); + if (Wire.endTransmission() == 0) { + return true; // CardKB detected + } + return false; // CardKB not detected +} +void scanForCardKB() { + const int maxRetries = 10; // Maximum number of retries + const int retryDelay = 100; // Delay between retries in milliseconds + for (int i = 0; i < maxRetries; ++i) { + // Perform the scan (example scan code, adjust as needed) + cardKBDetected = performScanForCardKB(); + + if (cardKBDetected) { + Serial.println("CardKB Keyboard detected."); + break; + } + + delay(retryDelay); // Wait before the next retry + } + if (!cardKBDetected) { + Serial.println("CardKB Keyboard not detected. Canned Message Module Disabled."); + } +} ScanI2C::FoundDevice ScanI2C::firstKeyboard() const { ScanI2C::DeviceType types[] = {CARDKB, TDECKKB, BBQ10KB, RAK14004}; diff --git a/src/detect/ScanI2C.h b/src/detect/ScanI2C.h index 1facb897a..1c2fe73c6 100644 --- a/src/detect/ScanI2C.h +++ b/src/detect/ScanI2C.h @@ -2,6 +2,7 @@ #include #include +bool performScanForCardKB(); class ScanI2C { diff --git a/src/main.cpp b/src/main.cpp index 6797c8375..9529ca7b3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,6 +35,8 @@ #include // #include +bool cardKBDetected = false; +void scanForCardKB(); #ifdef ARCH_ESP32 #if !MESHTASTIC_EXCLUDE_WEBSERVER #include "mesh/http/WebServer.h" @@ -376,6 +378,7 @@ void setup() // otherwise keyboard and touch screen will not work delay(800); #endif +scanForCardKB(); // Initial scan for CardKB // Currently only the tbeam has a PMU // PMU initialization needs to be placed before i2c scanning diff --git a/src/main.h b/src/main.h index 2ef7edb3a..b1e1ac464 100644 --- a/src/main.h +++ b/src/main.h @@ -21,7 +21,7 @@ extern NimbleBluetooth *nimbleBluetooth; #include "NRF52Bluetooth.h" extern NRF52Bluetooth *nrf52Bluetooth; #endif - +extern bool cardKBDetected; #if ARCH_PORTDUINO extern HardwareSPI *DisplaySPI; extern HardwareSPI *LoraSPI; @@ -39,6 +39,7 @@ extern bool pmu_found; extern bool isCharging; extern bool isUSBPowered; +#define CARDKB_I2C_ADDRESS 0x5F // Replace 0x5F with the actual address if different #if !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL) extern ATECCX08A atecc; #endif From 2eb3cfd5e033afb27c687bba0b194b94036d3cac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Sun, 19 May 2024 19:32:08 +0200 Subject: [PATCH 10/15] change the main scan class so they scan only for wanted bits - UNTESTED --- src/detect/ScanI2C.cpp | 28 ---------------------------- src/detect/ScanI2C.h | 1 - src/main.cpp | 3 --- src/main.h | 3 +-- 4 files changed, 1 insertion(+), 34 deletions(-) diff --git a/src/detect/ScanI2C.cpp b/src/detect/ScanI2C.cpp index 51718b15f..9c3a18644 100644 --- a/src/detect/ScanI2C.cpp +++ b/src/detect/ScanI2C.cpp @@ -1,6 +1,4 @@ #include "ScanI2C.h" -#include "main.h" -#include const ScanI2C::DeviceAddress ScanI2C::ADDRESS_NONE = ScanI2C::DeviceAddress(); const ScanI2C::FoundDevice ScanI2C::DEVICE_NONE = ScanI2C::FoundDevice(ScanI2C::DeviceType::NONE, ADDRESS_NONE); @@ -30,33 +28,7 @@ ScanI2C::FoundDevice ScanI2C::firstRTC() const ScanI2C::DeviceType types[] = {RTC_RV3028, RTC_PCF8563}; return firstOfOrNONE(2, types); } -bool performScanForCardKB() { - // Example I2C scan code for CardKB (adjust as needed) - Wire.beginTransmission(CARDKB_I2C_ADDRESS); - if (Wire.endTransmission() == 0) { - return true; // CardKB detected - } - return false; // CardKB not detected -} -void scanForCardKB() { - const int maxRetries = 10; // Maximum number of retries - const int retryDelay = 100; // Delay between retries in milliseconds - for (int i = 0; i < maxRetries; ++i) { - // Perform the scan (example scan code, adjust as needed) - cardKBDetected = performScanForCardKB(); - - if (cardKBDetected) { - Serial.println("CardKB Keyboard detected."); - break; - } - - delay(retryDelay); // Wait before the next retry - } - if (!cardKBDetected) { - Serial.println("CardKB Keyboard not detected. Canned Message Module Disabled."); - } -} ScanI2C::FoundDevice ScanI2C::firstKeyboard() const { ScanI2C::DeviceType types[] = {CARDKB, TDECKKB, BBQ10KB, RAK14004}; diff --git a/src/detect/ScanI2C.h b/src/detect/ScanI2C.h index 1c2fe73c6..1facb897a 100644 --- a/src/detect/ScanI2C.h +++ b/src/detect/ScanI2C.h @@ -2,7 +2,6 @@ #include #include -bool performScanForCardKB(); class ScanI2C { diff --git a/src/main.cpp b/src/main.cpp index 9529ca7b3..6797c8375 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,8 +35,6 @@ #include // #include -bool cardKBDetected = false; -void scanForCardKB(); #ifdef ARCH_ESP32 #if !MESHTASTIC_EXCLUDE_WEBSERVER #include "mesh/http/WebServer.h" @@ -378,7 +376,6 @@ void setup() // otherwise keyboard and touch screen will not work delay(800); #endif -scanForCardKB(); // Initial scan for CardKB // Currently only the tbeam has a PMU // PMU initialization needs to be placed before i2c scanning diff --git a/src/main.h b/src/main.h index b1e1ac464..2ef7edb3a 100644 --- a/src/main.h +++ b/src/main.h @@ -21,7 +21,7 @@ extern NimbleBluetooth *nimbleBluetooth; #include "NRF52Bluetooth.h" extern NRF52Bluetooth *nrf52Bluetooth; #endif -extern bool cardKBDetected; + #if ARCH_PORTDUINO extern HardwareSPI *DisplaySPI; extern HardwareSPI *LoraSPI; @@ -39,7 +39,6 @@ extern bool pmu_found; extern bool isCharging; extern bool isUSBPowered; -#define CARDKB_I2C_ADDRESS 0x5F // Replace 0x5F with the actual address if different #if !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL) extern ATECCX08A atecc; #endif From ba14ffb8d393662f59481489af7c252a28dd60ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Fri, 24 May 2024 22:59:31 +0200 Subject: [PATCH 11/15] change type to 8 bit uint --- src/detect/ScanI2C.cpp | 2 +- src/detect/ScanI2C.h | 2 +- src/detect/ScanI2CTwoWire.cpp | 6 +++--- src/detect/ScanI2CTwoWire.h | 2 +- src/input/kbI2cBase.cpp | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/detect/ScanI2C.cpp b/src/detect/ScanI2C.cpp index 9c3a18644..03b93e068 100644 --- a/src/detect/ScanI2C.cpp +++ b/src/detect/ScanI2C.cpp @@ -6,7 +6,7 @@ const ScanI2C::FoundDevice ScanI2C::DEVICE_NONE = ScanI2C::FoundDevice(ScanI2C:: ScanI2C::ScanI2C() = default; void ScanI2C::scanPort(ScanI2C::I2CPort port) {} -void ScanI2C::scanPort(ScanI2C::I2CPort port, int *address) {} +void ScanI2C::scanPort(ScanI2C::I2CPort port, uint8_t *address) {} void ScanI2C::setSuppressScreen() { diff --git a/src/detect/ScanI2C.h b/src/detect/ScanI2C.h index 1facb897a..5c75a9dee 100644 --- a/src/detect/ScanI2C.h +++ b/src/detect/ScanI2C.h @@ -88,7 +88,7 @@ class ScanI2C ScanI2C(); virtual void scanPort(ScanI2C::I2CPort); - virtual void scanPort(ScanI2C::I2CPort, int *); + virtual void scanPort(ScanI2C::I2CPort, uint8_t *); /* * A bit of a hack, this tells the scanner not to tell later systems there is a screen to avoid enabling it. diff --git a/src/detect/ScanI2CTwoWire.cpp b/src/detect/ScanI2CTwoWire.cpp index dd70db8b7..95e273b85 100644 --- a/src/detect/ScanI2CTwoWire.cpp +++ b/src/detect/ScanI2CTwoWire.cpp @@ -135,7 +135,7 @@ uint16_t ScanI2CTwoWire::getRegisterValue(const ScanI2CTwoWire::RegisterLocation type = T; \ break; -void ScanI2CTwoWire::scanPort(I2CPort port, int *address) +void ScanI2CTwoWire::scanPort(I2CPort port, uint8_t *address) { concurrency::LockGuard guard((concurrency::Lock *)&lock); @@ -163,8 +163,8 @@ void ScanI2CTwoWire::scanPort(I2CPort port, int *address) #endif for (addr.address = 1; addr.address < 127; addr.address++) { - // Skip the address if it is not requested oon a partial scan - if (sizeof(address) > 0 && addr.address != *address) { + // Skip the address if it is not requested on a partial scan + if (address != nullptr && *address != addr.address) { continue; } i2cBus->beginTransmission(addr.address); diff --git a/src/detect/ScanI2CTwoWire.h b/src/detect/ScanI2CTwoWire.h index a7c19c779..332afbf64 100644 --- a/src/detect/ScanI2CTwoWire.h +++ b/src/detect/ScanI2CTwoWire.h @@ -16,7 +16,7 @@ class ScanI2CTwoWire : public ScanI2C public: void scanPort(ScanI2C::I2CPort) override; - void scanPort(ScanI2C::I2CPort, int *) override; + void scanPort(ScanI2C::I2CPort, uint8_t *) override; ScanI2C::FoundDevice find(ScanI2C::DeviceType) const override; diff --git a/src/input/kbI2cBase.cpp b/src/input/kbI2cBase.cpp index 55f435fda..135de1716 100644 --- a/src/input/kbI2cBase.cpp +++ b/src/input/kbI2cBase.cpp @@ -33,7 +33,7 @@ int32_t KbI2cBase::runOnce() if (cardkb_found.address == 0x00) { // Input device is not detected. Rescan now. auto i2cScanner = std::unique_ptr(new ScanI2CTwoWire()); - int i2caddr_scan[] = {CARDKB_ADDR, TDECK_KB_ADDR, BBQ10_KB_ADDR}; + uint8_t i2caddr_scan[] = {CARDKB_ADDR, TDECK_KB_ADDR, BBQ10_KB_ADDR}; #if defined(I2C_SDA1) i2cScanner->scanPort(ScanI2C::I2CPort::WIRE1, i2caddr_scan); #endif From a453d7f52c80ea6887714b2415c1cac8f138806e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Sat, 25 May 2024 12:37:55 +0200 Subject: [PATCH 12/15] Iterate through uint array --- src/detect/ScanI2C.cpp | 2 +- src/detect/ScanI2C.h | 2 +- src/detect/ScanI2CTwoWire.cpp | 21 +++++++++++++++------ src/detect/ScanI2CTwoWire.h | 2 +- src/input/kbI2cBase.cpp | 5 +++-- 5 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/detect/ScanI2C.cpp b/src/detect/ScanI2C.cpp index 03b93e068..73bdf973b 100644 --- a/src/detect/ScanI2C.cpp +++ b/src/detect/ScanI2C.cpp @@ -6,7 +6,7 @@ const ScanI2C::FoundDevice ScanI2C::DEVICE_NONE = ScanI2C::FoundDevice(ScanI2C:: ScanI2C::ScanI2C() = default; void ScanI2C::scanPort(ScanI2C::I2CPort port) {} -void ScanI2C::scanPort(ScanI2C::I2CPort port, uint8_t *address) {} +void ScanI2C::scanPort(ScanI2C::I2CPort port, uint8_t *address, uint8_t asize) {} void ScanI2C::setSuppressScreen() { diff --git a/src/detect/ScanI2C.h b/src/detect/ScanI2C.h index 5c75a9dee..711e8bee5 100644 --- a/src/detect/ScanI2C.h +++ b/src/detect/ScanI2C.h @@ -88,7 +88,7 @@ class ScanI2C ScanI2C(); virtual void scanPort(ScanI2C::I2CPort); - virtual void scanPort(ScanI2C::I2CPort, uint8_t *); + virtual void scanPort(ScanI2C::I2CPort, uint8_t *, uint8_t); /* * A bit of a hack, this tells the scanner not to tell later systems there is a screen to avoid enabling it. diff --git a/src/detect/ScanI2CTwoWire.cpp b/src/detect/ScanI2CTwoWire.cpp index 95e273b85..b04590509 100644 --- a/src/detect/ScanI2CTwoWire.cpp +++ b/src/detect/ScanI2CTwoWire.cpp @@ -14,6 +14,15 @@ #define XPOWERS_AXP192_AXP2101_ADDRESS 0x34 #endif +bool in_array(uint8_t *array, int size, uint8_t lookfor) +{ + int i; + for (i = 0; i < size; i++) + if (lookfor == array[i]) + return true; + return false; +} + ScanI2C::FoundDevice ScanI2CTwoWire::find(ScanI2C::DeviceType type) const { concurrency::LockGuard guard((concurrency::Lock *)&lock); @@ -135,7 +144,7 @@ uint16_t ScanI2CTwoWire::getRegisterValue(const ScanI2CTwoWire::RegisterLocation type = T; \ break; -void ScanI2CTwoWire::scanPort(I2CPort port, uint8_t *address) +void ScanI2CTwoWire::scanPort(I2CPort port, uint8_t *address, uint8_t asize) { concurrency::LockGuard guard((concurrency::Lock *)&lock); @@ -163,10 +172,10 @@ void ScanI2CTwoWire::scanPort(I2CPort port, uint8_t *address) #endif for (addr.address = 1; addr.address < 127; addr.address++) { - // Skip the address if it is not requested on a partial scan - if (address != nullptr && *address != addr.address) { - continue; - } + if (asize != 0) + if (in_array(address, asize, addr.address)) + continue; + i2cBus->beginTransmission(addr.address); #ifdef ARCH_PORTDUINO if (i2cBus->read() != -1) @@ -373,7 +382,7 @@ void ScanI2CTwoWire::scanPort(I2CPort port, uint8_t *address) void ScanI2CTwoWire::scanPort(I2CPort port) { - scanPort(port, nullptr); + scanPort(port, nullptr, 0); } TwoWire *ScanI2CTwoWire::fetchI2CBus(ScanI2C::DeviceAddress address) const diff --git a/src/detect/ScanI2CTwoWire.h b/src/detect/ScanI2CTwoWire.h index 332afbf64..82b48f6b4 100644 --- a/src/detect/ScanI2CTwoWire.h +++ b/src/detect/ScanI2CTwoWire.h @@ -16,7 +16,7 @@ class ScanI2CTwoWire : public ScanI2C public: void scanPort(ScanI2C::I2CPort) override; - void scanPort(ScanI2C::I2CPort, uint8_t *) override; + void scanPort(ScanI2C::I2CPort, uint8_t *, uint8_t) override; ScanI2C::FoundDevice find(ScanI2C::DeviceType) const override; diff --git a/src/input/kbI2cBase.cpp b/src/input/kbI2cBase.cpp index 135de1716..ce22edb93 100644 --- a/src/input/kbI2cBase.cpp +++ b/src/input/kbI2cBase.cpp @@ -34,10 +34,11 @@ int32_t KbI2cBase::runOnce() // 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); + i2cScanner->scanPort(ScanI2C::I2CPort::WIRE1, i2caddr_scan, i2caddr_asize); #endif - i2cScanner->scanPort(ScanI2C::I2CPort::WIRE, i2caddr_scan); + i2cScanner->scanPort(ScanI2C::I2CPort::WIRE, i2caddr_scan, i2caddr_asize); auto kb_info = i2cScanner->firstKeyboard(); if (kb_info.type != ScanI2C::DeviceType::NONE) { From b6066a78c1983ffd3be6034115261916b2232dbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Mon, 17 Jun 2024 15:09:38 +0200 Subject: [PATCH 13/15] WIP --- src/detect/ScanI2CTwoWire.cpp | 11 ++++----- src/input/cardKbI2cImpl.cpp | 42 +++++++++++++++++++++++++++++++++-- src/input/kbI2cBase.cpp | 38 ------------------------------- 3 files changed, 46 insertions(+), 45 deletions(-) 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: From 7a25e0b69ae571555605b9b9fd1a620a083889e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Mon, 17 Jun 2024 17:03:32 +0200 Subject: [PATCH 14/15] don't close the wire when we didn't find anything. We might rescan later. --- src/main.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 6797c8375..3b009a179 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -421,10 +421,6 @@ void setup() auto i2cCount = i2cScanner->countDevices(); if (i2cCount == 0) { LOG_INFO("No I2C devices found\n"); - Wire.end(); -#ifdef I2C_SDA1 - Wire1.end(); -#endif } else { LOG_INFO("%i I2C devices found\n", i2cCount); } From 5cebe4a0a7a80e540a3568ea45c99b8d7f47f3dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Mon, 17 Jun 2024 23:24:27 +0200 Subject: [PATCH 15/15] trunk fmt --- src/input/cardKbI2cImpl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/input/cardKbI2cImpl.cpp b/src/input/cardKbI2cImpl.cpp index af0958f94..4bc5ee4ea 100644 --- a/src/input/cardKbI2cImpl.cpp +++ b/src/input/cardKbI2cImpl.cpp @@ -1,7 +1,7 @@ #include "cardKbI2cImpl.h" #include "InputBroker.h" -#include "main.h" #include "detect/ScanI2CTwoWire.h" +#include "main.h" CardKbI2cImpl *cardKbI2cImpl;