change the main scan class so they scan only for wanted bits - UNTESTED

This commit is contained in:
Thomas Göttgens 2024-05-19 19:32:08 +02:00
parent 27bb3506d3
commit dbb254ba7a
7 changed files with 51 additions and 38 deletions

View File

@ -1,6 +1,4 @@
#include "ScanI2C.h" #include "ScanI2C.h"
#include "main.h"
#include <Wire.h>
const ScanI2C::DeviceAddress ScanI2C::ADDRESS_NONE = ScanI2C::DeviceAddress(); const ScanI2C::DeviceAddress ScanI2C::ADDRESS_NONE = ScanI2C::DeviceAddress();
const ScanI2C::FoundDevice ScanI2C::DEVICE_NONE = ScanI2C::FoundDevice(ScanI2C::DeviceType::NONE, ADDRESS_NONE); 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; ScanI2C::ScanI2C() = default;
void ScanI2C::scanPort(ScanI2C::I2CPort port) {} void ScanI2C::scanPort(ScanI2C::I2CPort port) {}
void ScanI2C::scanPort(ScanI2C::I2CPort port, int *address) {}
void ScanI2C::setSuppressScreen() void ScanI2C::setSuppressScreen()
{ {
@ -29,33 +28,7 @@ ScanI2C::FoundDevice ScanI2C::firstRTC() const
ScanI2C::DeviceType types[] = {RTC_RV3028, RTC_PCF8563}; ScanI2C::DeviceType types[] = {RTC_RV3028, RTC_PCF8563};
return firstOfOrNONE(2, types); 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::FoundDevice ScanI2C::firstKeyboard() const
{ {
ScanI2C::DeviceType types[] = {CARDKB, TDECKKB, BBQ10KB, RAK14004}; ScanI2C::DeviceType types[] = {CARDKB, TDECKKB, BBQ10KB, RAK14004};

View File

@ -2,7 +2,6 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
bool performScanForCardKB();
class ScanI2C class ScanI2C
{ {
@ -89,6 +88,7 @@ class ScanI2C
ScanI2C(); ScanI2C();
virtual void scanPort(ScanI2C::I2CPort); 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. * A bit of a hack, this tells the scanner not to tell later systems there is a screen to avoid enabling it.

View File

@ -135,7 +135,7 @@ uint16_t ScanI2CTwoWire::getRegisterValue(const ScanI2CTwoWire::RegisterLocation
type = T; \ type = T; \
break; break;
void ScanI2CTwoWire::scanPort(I2CPort port) void ScanI2CTwoWire::scanPort(I2CPort port, int *address)
{ {
concurrency::LockGuard guard((concurrency::Lock *)&lock); concurrency::LockGuard guard((concurrency::Lock *)&lock);
@ -163,6 +163,10 @@ void ScanI2CTwoWire::scanPort(I2CPort port)
#endif #endif
for (addr.address = 1; addr.address < 127; addr.address++) { 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); i2cBus->beginTransmission(addr.address);
#ifdef ARCH_PORTDUINO #ifdef ARCH_PORTDUINO
if (i2cBus->read() != -1) 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 TwoWire *ScanI2CTwoWire::fetchI2CBus(ScanI2C::DeviceAddress address) const
{ {
if (address.port == ScanI2C::I2CPort::WIRE) { if (address.port == ScanI2C::I2CPort::WIRE) {

View File

@ -16,6 +16,8 @@ class ScanI2CTwoWire : public ScanI2C
public: public:
void scanPort(ScanI2C::I2CPort) override; void scanPort(ScanI2C::I2CPort) override;
void scanPort(ScanI2C::I2CPort, int *) override;
ScanI2C::FoundDevice find(ScanI2C::DeviceType) const override; ScanI2C::FoundDevice find(ScanI2C::DeviceType) const override;
TwoWire *fetchI2CBus(ScanI2C::DeviceAddress) const; TwoWire *fetchI2CBus(ScanI2C::DeviceAddress) const;

View File

@ -1,6 +1,7 @@
#include "kbI2cBase.h" #include "kbI2cBase.h"
#include "configuration.h" #include "configuration.h"
#include "detect/ScanI2C.h" #include "detect/ScanI2C.h"
#include "detect/ScanI2CTwoWire.h"
extern ScanI2C::DeviceAddress cardkb_found; extern ScanI2C::DeviceAddress cardkb_found;
extern uint8_t kb_model; extern uint8_t kb_model;
@ -30,7 +31,39 @@ uint8_t read_from_14004(TwoWire *i2cBus, uint8_t reg, uint8_t *data, uint8_t len
int32_t KbI2cBase::runOnce() int32_t KbI2cBase::runOnce()
{ {
if (cardkb_found.address == 0x00) { if (cardkb_found.address == 0x00) {
// Input device is not detected. // Input device is not detected. Rescan now.
auto i2cScanner = std::unique_ptr<ScanI2CTwoWire>(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; return INT32_MAX;
} }

View File

@ -35,8 +35,6 @@
#include <utility> #include <utility>
// #include <driver/rtc_io.h> // #include <driver/rtc_io.h>
bool cardKBDetected = false;
void scanForCardKB();
#ifdef ARCH_ESP32 #ifdef ARCH_ESP32
#if !MESHTASTIC_EXCLUDE_WEBSERVER #if !MESHTASTIC_EXCLUDE_WEBSERVER
#include "mesh/http/WebServer.h" #include "mesh/http/WebServer.h"
@ -378,7 +376,6 @@ void setup()
// otherwise keyboard and touch screen will not work // otherwise keyboard and touch screen will not work
delay(800); delay(800);
#endif #endif
scanForCardKB(); // Initial scan for CardKB
// Currently only the tbeam has a PMU // Currently only the tbeam has a PMU
// PMU initialization needs to be placed before i2c scanning // PMU initialization needs to be placed before i2c scanning

View File

@ -21,7 +21,7 @@ extern NimbleBluetooth *nimbleBluetooth;
#include "NRF52Bluetooth.h" #include "NRF52Bluetooth.h"
extern NRF52Bluetooth *nrf52Bluetooth; extern NRF52Bluetooth *nrf52Bluetooth;
#endif #endif
extern bool cardKBDetected;
#if ARCH_PORTDUINO #if ARCH_PORTDUINO
extern HardwareSPI *DisplaySPI; extern HardwareSPI *DisplaySPI;
extern HardwareSPI *LoraSPI; extern HardwareSPI *LoraSPI;
@ -39,7 +39,6 @@ extern bool pmu_found;
extern bool isCharging; extern bool isCharging;
extern bool isUSBPowered; extern bool isUSBPowered;
#define CARDKB_I2C_ADDRESS 0x5F // Replace 0x5F with the actual address if different
#if !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL) #if !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL)
extern ATECCX08A atecc; extern ATECCX08A atecc;
#endif #endif