diff --git a/src/detect/ScanI2CTwoWire.cpp b/src/detect/ScanI2CTwoWire.cpp index ff4f9b890..151e559f1 100644 --- a/src/detect/ScanI2CTwoWire.cpp +++ b/src/detect/ScanI2CTwoWire.cpp @@ -156,44 +156,11 @@ bool ScanI2CTwoWire::i2cCommandResponseLength(ScanI2C::DeviceAddress addr, uint1 } #if HAS_TELEMETRY && !MESHTASTIC_EXCLUDE_AIR_QUALITY_SENSOR -// FIXME Move to a separate file for detection of sensors that require more complex interactions? -// For SEN5X detection -// Note, this code needs to be called before setting the I2C bus speed -// for the screen at high speed. The speed needs to be at 100kHz, otherwise -// detection will not work -String readSEN5xProductName(TwoWire *i2cBus, uint8_t address) +#include "../modules/Telemetry/Sensor/SEN5XSensor.h" +bool probeSEN5X(TwoWire *i2cBus, uint8_t address, ScanI2C::I2CPort port) { - uint8_t cmd[] = {0xD0, 0x14}; - uint8_t response[48] = {0}; - - i2cBus->beginTransmission(address); - i2cBus->write(cmd, 2); - if (i2cBus->endTransmission() != 0) - return ""; - - delay(20); - if (i2cBus->requestFrom(address, (uint8_t)48) != 48) - return ""; - - for (int i = 0; i < 48 && i2cBus->available(); ++i) { - response[i] = i2cBus->read(); - } - - char productName[33] = {0}; - int j = 0; - for (int i = 0; i < 48 && j < 32; i += 3) { - if (response[i] >= 32 && response[i] <= 126) - productName[j++] = response[i]; - else - break; - - if (response[i + 1] >= 32 && response[i + 1] <= 126) - productName[j++] = response[i + 1]; - else - break; - } - - return String(productName); + SEN5XSensor sen5xsensor; + return sen5xsensor.probe(i2cBus, address, port); } #endif @@ -861,33 +828,18 @@ void ScanI2CTwoWire::scanPort(I2CPort port, uint8_t *address, uint8_t asize) type = ICM42607P; logFoundDevice("ICM-42607-P", (uint8_t)addr.address); break; - } -#if HAS_TELEMETRY && !MESHTASTIC_EXCLUDE_AIR_QUALITY_SENSOR - String prod = ""; - prod = readSEN5xProductName(i2cBus, addr.address); - if (prod.startsWith("SEN55")) { - type = SEN5X; - logFoundDevice("Sensirion SEN55", addr.address); - break; - } else if (prod.startsWith("SEN54")) { - type = SEN5X; - logFoundDevice("Sensirion SEN54", addr.address); - break; - } else if (prod.startsWith("SEN50")) { - type = SEN5X; - logFoundDevice("Sensirion SEN50", addr.address); - break; - } -#endif - if (addr.address == BMX160_ADDR) { - type = BMX160; - logFoundDevice("BMX160", (uint8_t)addr.address); - break; - } else { + } else if (registerValue == 0x68) { // WHO_AM_I from datasheet type = MPU6050; logFoundDevice("MPU6050", (uint8_t)addr.address); break; } +#if HAS_TELEMETRY && !MESHTASTIC_EXCLUDE_AIR_QUALITY_SENSOR + if (probeSEN5X(i2cBus, addr.address, port)) { + type = SEN5X; + logFoundDevice("SEN5X", addr.address); + break; + } +#endif } break; diff --git a/src/modules/Telemetry/Sensor/PMSA003ISensor.h b/src/modules/Telemetry/Sensor/PMSA003ISensor.h index 51af73087..9b9cad37a 100644 --- a/src/modules/Telemetry/Sensor/PMSA003ISensor.h +++ b/src/modules/Telemetry/Sensor/PMSA003ISensor.h @@ -39,4 +39,4 @@ class PMSA003ISensor : public TelemetrySensor #endif }; -#endif \ No newline at end of file +#endif diff --git a/src/modules/Telemetry/Sensor/SCD30Sensor.h b/src/modules/Telemetry/Sensor/SCD30Sensor.h index 373d8e41c..82c9a5532 100644 --- a/src/modules/Telemetry/Sensor/SCD30Sensor.h +++ b/src/modules/Telemetry/Sensor/SCD30Sensor.h @@ -52,4 +52,4 @@ class SCD30Sensor : public TelemetrySensor meshtastic_AdminMessage *response) override; }; -#endif \ No newline at end of file +#endif diff --git a/src/modules/Telemetry/Sensor/SCD4XSensor.h b/src/modules/Telemetry/Sensor/SCD4XSensor.h index 50df60bd3..0e1a46f44 100644 --- a/src/modules/Telemetry/Sensor/SCD4XSensor.h +++ b/src/modules/Telemetry/Sensor/SCD4XSensor.h @@ -62,4 +62,4 @@ class SCD4XSensor : public TelemetrySensor meshtastic_AdminMessage *response) override; }; -#endif \ No newline at end of file +#endif diff --git a/src/modules/Telemetry/Sensor/SEN5XSensor.cpp b/src/modules/Telemetry/Sensor/SEN5XSensor.cpp index 4f5688b46..e88e81b9e 100644 --- a/src/modules/Telemetry/Sensor/SEN5XSensor.cpp +++ b/src/modules/Telemetry/Sensor/SEN5XSensor.cpp @@ -75,6 +75,24 @@ bool SEN5XSensor::findModel() return true; } +bool SEN5XSensor::probe(TwoWire *bus, uint8_t address, ScanI2C::I2CPort port) +{ + LOG_INFO("SEN5X: probing sensor"); + + _bus = bus; + _address = address; +#ifdef SEN5X_I2C_CLOCK_SPEED + _port = port; + reClockI2C.setup(_bus, _port); +#endif /* SEN5X_I2C_CLOCK_SPEED */ + + if (!findModel()) { + LOG_DEBUG("SEN5X: can't find SEN5X model"); + return false; + } + return true; +} + bool SEN5XSensor::sendCommand(uint16_t command) { uint8_t nothing; diff --git a/src/modules/Telemetry/Sensor/SEN5XSensor.h b/src/modules/Telemetry/Sensor/SEN5XSensor.h index 7a1984a62..935c8cb21 100644 --- a/src/modules/Telemetry/Sensor/SEN5XSensor.h +++ b/src/modules/Telemetry/Sensor/SEN5XSensor.h @@ -155,6 +155,7 @@ See: https://sensirion.com/resource/application_note/low_power_mode/sen5x public: SEN5XSensor(); + bool probe(TwoWire *bus, uint8_t address, ScanI2C::I2CPort port); virtual bool initDevice(TwoWire *bus, ScanI2C::FoundDevice *dev) override; virtual bool getMetrics(meshtastic_Telemetry *measurement) override; @@ -169,4 +170,4 @@ See: https://sensirion.com/resource/application_note/low_power_mode/sen5x meshtastic_AdminMessage *response) override; }; -#endif \ No newline at end of file +#endif