Refurbish ReClockI2C API

* Make ReClockI2C API class
* Use new API in AirQualityTelemetry module
* Minor changes on some logs
This commit is contained in:
oscgonfer
2026-06-14 14:48:40 +02:00
parent 0200d45241
commit b7ec5cd35a
15 changed files with 338 additions and 320 deletions
+23 -38
View File
@@ -2,7 +2,6 @@
#if !MESHTASTIC_EXCLUDE_AIR_QUALITY_SENSOR && __has_include(<SensirionI2cSfa3x.h>)
#include "../detect/reClockI2C.h"
#include "../mesh/generated/meshtastic/telemetry.pb.h"
#include "SFA30Sensor.h"
@@ -14,11 +13,13 @@ bool SFA30Sensor::initDevice(TwoWire *bus, ScanI2C::FoundDevice *dev)
_bus = bus;
_address = dev->address.address;
_port = dev->address.port;
#ifdef SFA30_I2C_CLOCK_SPEED
_port = dev->address.port;
reClockI2C.setup(_bus, _port);
LOG_INFO("%s attempting to reclock speed to %uHz", sensorName, SFA30_I2C_CLOCK_SPEED);
uint32_t currentClock = reClockI2C(SFA30_I2C_CLOCK_SPEED, _bus, _port);
reClockI2C.setClock(SFA30_I2C_CLOCK_SPEED);
#endif /* SFA30_I2C_CLOCK_SPEED */
sfa30.begin(*_bus, _address);
@@ -26,10 +27,8 @@ bool SFA30Sensor::initDevice(TwoWire *bus, ScanI2C::FoundDevice *dev)
if (this->isError(sfa30.deviceReset())) {
#ifdef SFA30_I2C_CLOCK_SPEED
if (currentClock) {
LOG_INFO("%s restoring clock speed to %uHz", sensorName, currentClock);
reClockI2C(currentClock, _bus, _port);
}
LOG_INFO("%s restoring clock speed", sensorName);
reClockI2C.restoreClock();
#endif /* SFA30_I2C_CLOCK_SPEED */
return false;
}
@@ -37,10 +36,8 @@ bool SFA30Sensor::initDevice(TwoWire *bus, ScanI2C::FoundDevice *dev)
state = State::IDLE;
if (this->isError(sfa30.startContinuousMeasurement())) {
#ifdef SFA30_I2C_CLOCK_SPEED
if (currentClock) {
LOG_INFO("%s restoring clock speed to %uHz", sensorName, currentClock);
reClockI2C(currentClock, _bus, _port);
}
LOG_INFO("%s restoring clock speed", sensorName);
reClockI2C.restoreClock();
#endif /* SFA30_I2C_CLOCK_SPEED */
return false;
}
@@ -48,10 +45,8 @@ bool SFA30Sensor::initDevice(TwoWire *bus, ScanI2C::FoundDevice *dev)
LOG_INFO("%s starting measurement", sensorName);
#ifdef SFA30_I2C_CLOCK_SPEED
if (currentClock) {
LOG_INFO("%s restoring clock speed to %uHz", sensorName, currentClock);
reClockI2C(currentClock, _bus, _port);
}
LOG_INFO("%s restoring clock speed", sensorName);
reClockI2C.restoreClock();
#endif /* SFA30_I2C_CLOCK_SPEED */
status = 1;
@@ -77,7 +72,7 @@ void SFA30Sensor::sleep()
{
#ifdef SFA30_I2C_CLOCK_SPEED
LOG_INFO("%s attempting to reclock speed to %uHz", sensorName, SFA30_I2C_CLOCK_SPEED);
uint32_t currentClock = reClockI2C(SFA30_I2C_CLOCK_SPEED, _bus, _port);
reClockI2C.setClock(SFA30_I2C_CLOCK_SPEED);
#endif /* SFA30_I2C_CLOCK_SPEED */
// Note - not recommended for this sensor on a periodic basis
@@ -86,10 +81,8 @@ void SFA30Sensor::sleep()
};
#ifdef SFA30_I2C_CLOCK_SPEED
if (currentClock) {
LOG_INFO("%s restoring clock speed to %uHz", sensorName, currentClock);
reClockI2C(currentClock, _bus, _port);
}
LOG_INFO("%s restoring clock speed", sensorName);
reClockI2C.restoreClock();
#endif /* SFA30_I2C_CLOCK_SPEED */
LOG_INFO("%s: stop measurement", sensorName);
@@ -101,25 +94,21 @@ uint32_t SFA30Sensor::wakeUp()
{
#ifdef SFA30_I2C_CLOCK_SPEED
LOG_INFO("%s attempting to reclock speed to %uHz", sensorName, SFA30_I2C_CLOCK_SPEED);
uint32_t currentClock = reClockI2C(SFA30_I2C_CLOCK_SPEED, _bus, _port);
reClockI2C.setClock(SFA30_I2C_CLOCK_SPEED);
#endif /* SFA30_I2C_CLOCK_SPEED */
LOG_INFO("Waking up %s", sensorName);
if (this->isError(sfa30.startContinuousMeasurement())) {
#ifdef SFA30_I2C_CLOCK_SPEED
if (currentClock) {
LOG_INFO("%s restoring clock speed to %uHz", sensorName, currentClock);
reClockI2C(currentClock, _bus, _port);
}
LOG_INFO("%s restoring clock speed", sensorName);
reClockI2C.restoreClock();
#endif /* SFA30_I2C_CLOCK_SPEED */
return 0;
}
#ifdef SFA30_I2C_CLOCK_SPEED
if (currentClock) {
LOG_INFO("%s restoring clock speed to %uHz", sensorName, currentClock);
reClockI2C(currentClock, _bus, _port);
}
LOG_INFO("%s restoring clock speed", sensorName);
reClockI2C.restoreClock();
#endif /* SFA30_I2C_CLOCK_SPEED */
state = State::ACTIVE;
@@ -166,25 +155,21 @@ bool SFA30Sensor::getMetrics(meshtastic_Telemetry *measurement)
#ifdef SFA30_I2C_CLOCK_SPEED
LOG_INFO("%s attempting to reclock speed to %uHz", sensorName, SFA30_I2C_CLOCK_SPEED);
uint32_t currentClock = reClockI2C(SFA30_I2C_CLOCK_SPEED, _bus, _port);
reClockI2C.setClock(SFA30_I2C_CLOCK_SPEED);
#endif /* SFA30_I2C_CLOCK_SPEED */
if (this->isError(sfa30.readMeasuredValues(hcho, humidity, temperature))) {
LOG_WARN("%s: No values", sensorName);
#ifdef SFA30_I2C_CLOCK_SPEED
if (currentClock) {
LOG_INFO("%s restoring clock speed to %uHz", sensorName, currentClock);
reClockI2C(currentClock, _bus, _port);
}
LOG_INFO("%s restoring clock speed", sensorName);
reClockI2C.restoreClock();
#endif /* SFA30_I2C_CLOCK_SPEED */
return false;
}
#ifdef SFA30_I2C_CLOCK_SPEED
if (currentClock) {
LOG_INFO("%s restoring clock speed to %uHz", sensorName, currentClock);
reClockI2C(currentClock, _bus, _port);
}
LOG_INFO("%s restoring clock speed", sensorName);
reClockI2C.restoreClock();
#endif /* SFA30_I2C_CLOCK_SPEED */
measurement->variant.air_quality_metrics.has_form_temperature = true;