Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/enhance: allow telemetry sensors connected to second I2C port #2891

Merged
merged 2 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/Power.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,10 @@ class AnalogBatteryLevel : public HasBatteryLevel
#if defined(HAS_TELEMETRY) && !defined(ARCH_PORTDUINO)
uint16_t getINAVoltage()
{
if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_INA219] == config.power.device_battery_ina_address) {
if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_INA219].first == config.power.device_battery_ina_address) {
return ina219Sensor.getBusVoltageMv();
} else if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_INA260] == config.power.device_battery_ina_address) {
} else if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_INA260].first ==
config.power.device_battery_ina_address) {
return ina260Sensor.getBusVoltageMv();
}
return 0;
Expand All @@ -294,11 +295,12 @@ class AnalogBatteryLevel : public HasBatteryLevel
if (!config.power.device_battery_ina_address) {
return false;
}
if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_INA219] == config.power.device_battery_ina_address) {
if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_INA219].first == config.power.device_battery_ina_address) {
if (!ina219Sensor.isInitialized())
return ina219Sensor.runOnce() > 0;
return ina219Sensor.isRunning();
} else if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_INA260] == config.power.device_battery_ina_address) {
} else if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_INA260].first ==
config.power.device_battery_ina_address) {
if (!ina260Sensor.isInitialized())
return ina260Sensor.runOnce() > 0;
return ina260Sensor.isRunning();
Expand Down
4 changes: 2 additions & 2 deletions src/detect/ScanI2CTwoWire.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class ScanI2CTwoWire : public ScanI2C

ScanI2C::FoundDevice find(ScanI2C::DeviceType) const override;

TwoWire *fetchI2CBus(ScanI2C::DeviceAddress) const;

bool exists(ScanI2C::DeviceType) const override;

size_t countDevices() const override;
Expand Down Expand Up @@ -51,6 +53,4 @@ class ScanI2CTwoWire : public ScanI2C
uint16_t getRegisterValue(const RegisterLocation &, ResponseWidth) const;

DeviceType probeOLED(ScanI2C::DeviceAddress) const;

TwoWire *fetchI2CBus(ScanI2C::DeviceAddress) const;
};
9 changes: 5 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "target_specific.h"
#include <Wire.h>
#include <memory>
#include <utility>
// #include <driver/rtc_io.h>

#include "mesh/eth/ethClient.h"
Expand Down Expand Up @@ -122,9 +123,8 @@ uint32_t serialSinceMsec;

bool pmu_found;

// Array map of sensor types (as array index) and i2c address as value we'll find in the i2c scan
uint8_t nodeTelemetrySensorsMap[_meshtastic_TelemetrySensorType_MAX + 1] = {
0}; // one is enough, missing elements will be initialized to 0 anyway.
// Array map of sensor types with i2c address and wire as we'll find in the i2c scan
std::pair<uint8_t, TwoWire *> nodeTelemetrySensorsMap[_meshtastic_TelemetrySensorType_MAX + 1] = {};
mverch67 marked this conversation as resolved.
Show resolved Hide resolved

Router *router = NULL; // Users of router don't care what sort of subclass implements that API

Expand Down Expand Up @@ -491,7 +491,8 @@ void setup()
{ \
auto found = i2cScanner->find(SCANNER_T); \
if (found.type != ScanI2C::DeviceType::NONE) { \
nodeTelemetrySensorsMap[PB_T] = found.address.address; \
nodeTelemetrySensorsMap[PB_T].first = found.address.address; \
nodeTelemetrySensorsMap[PB_T].second = i2cScanner->fetchI2CBus(found.address); \
LOG_DEBUG("found i2c sensor %s\n", STRING(PB_T)); \
} \
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/Modules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void setupModules()
#endif
#if HAS_SENSOR
new EnvironmentTelemetryModule();
if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_PMSA003I] > 0) {
if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_PMSA003I].first > 0) {
new AirQualityTelemetryModule();
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/modules/Telemetry/Sensor/BME280Sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ int32_t BME280Sensor::runOnce()
if (!hasSensor()) {
return DEFAULT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS;
}
status = bme280.begin(nodeTelemetrySensorsMap[sensorType]);
status = bme280.begin(nodeTelemetrySensorsMap[sensorType].first, nodeTelemetrySensorsMap[sensorType].second);

bme280.setSampling(Adafruit_BME280::MODE_FORCED,
Adafruit_BME280::SAMPLING_X1, // Temp. oversampling
Expand Down
2 changes: 1 addition & 1 deletion src/modules/Telemetry/Sensor/BME280Sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "TelemetrySensor.h"
#include <Adafruit_BME280.h>

class BME280Sensor : virtual public TelemetrySensor
class BME280Sensor : public TelemetrySensor
{
private:
Adafruit_BME280 bme280;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/Telemetry/Sensor/BME680Sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ int32_t BME680Sensor::runOnce()
if (!hasSensor()) {
return DEFAULT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS;
}
if (!bme680.begin(nodeTelemetrySensorsMap[sensorType], Wire))
if (!bme680.begin(nodeTelemetrySensorsMap[sensorType].first, *nodeTelemetrySensorsMap[sensorType].second))
checkStatus("begin");

if (bme680.status == BSEC_OK) {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/Telemetry/Sensor/BME680Sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include "bme680_iaq_33v_3s_4d/bsec_iaq.h"

class BME680Sensor : virtual public TelemetrySensor
class BME680Sensor : public TelemetrySensor
{
private:
Bsec2 bme680;
Expand Down
3 changes: 2 additions & 1 deletion src/modules/Telemetry/Sensor/BMP280Sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ int32_t BMP280Sensor::runOnce()
if (!hasSensor()) {
return DEFAULT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS;
}
status = bmp280.begin(nodeTelemetrySensorsMap[sensorType]);
bmp280 = Adafruit_BMP280(nodeTelemetrySensorsMap[sensorType].second);
status = bmp280.begin(nodeTelemetrySensorsMap[sensorType].first);

bmp280.setSampling(Adafruit_BMP280::MODE_FORCED,
Adafruit_BMP280::SAMPLING_X1, // Temp. oversampling
Expand Down
2 changes: 1 addition & 1 deletion src/modules/Telemetry/Sensor/BMP280Sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "TelemetrySensor.h"
#include <Adafruit_BMP280.h>

class BMP280Sensor : virtual public TelemetrySensor
class BMP280Sensor : public TelemetrySensor
{
private:
Adafruit_BMP280 bmp280;
Expand Down
4 changes: 2 additions & 2 deletions src/modules/Telemetry/Sensor/INA219Sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ int32_t INA219Sensor::runOnce()
return DEFAULT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS;
}
if (!ina219.success()) {
ina219 = Adafruit_INA219(nodeTelemetrySensorsMap[sensorType]);
status = ina219.begin();
ina219 = Adafruit_INA219(nodeTelemetrySensorsMap[sensorType].first);
status = ina219.begin(nodeTelemetrySensorsMap[sensorType].second);
} else {
status = ina219.success();
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/Telemetry/Sensor/INA219Sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "VoltageSensor.h"
#include <Adafruit_INA219.h>

class INA219Sensor : virtual public TelemetrySensor, VoltageSensor
class INA219Sensor : public TelemetrySensor, VoltageSensor
{
private:
Adafruit_INA219 ina219;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/Telemetry/Sensor/INA260Sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ int32_t INA260Sensor::runOnce()
}

if (!status) {
status = ina260.begin(nodeTelemetrySensorsMap[sensorType]);
status = ina260.begin(nodeTelemetrySensorsMap[sensorType].first, nodeTelemetrySensorsMap[sensorType].second);
}
return initI2CSensor();
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/Telemetry/Sensor/INA260Sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "VoltageSensor.h"
#include <Adafruit_INA260.h>

class INA260Sensor : virtual public TelemetrySensor, VoltageSensor
class INA260Sensor : public TelemetrySensor, VoltageSensor
{
private:
Adafruit_INA260 ina260 = Adafruit_INA260();
Expand Down
2 changes: 1 addition & 1 deletion src/modules/Telemetry/Sensor/LPS22HBSensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ int32_t LPS22HBSensor::runOnce()
if (!hasSensor()) {
return DEFAULT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS;
}
status = lps22hb.begin_I2C(nodeTelemetrySensorsMap[sensorType]);
status = lps22hb.begin_I2C(nodeTelemetrySensorsMap[sensorType].first, nodeTelemetrySensorsMap[sensorType].second);
return initI2CSensor();
}

Expand Down
2 changes: 1 addition & 1 deletion src/modules/Telemetry/Sensor/LPS22HBSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <Adafruit_LPS2X.h>
#include <Adafruit_Sensor.h>

class LPS22HBSensor : virtual public TelemetrySensor
class LPS22HBSensor : public TelemetrySensor
{
private:
Adafruit_LPS22 lps22hb;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/Telemetry/Sensor/MCP9808Sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ int32_t MCP9808Sensor::runOnce()
if (!hasSensor()) {
return DEFAULT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS;
}
status = mcp9808.begin(nodeTelemetrySensorsMap[sensorType]);
status = mcp9808.begin(nodeTelemetrySensorsMap[sensorType].first, nodeTelemetrySensorsMap[sensorType].second);
return initI2CSensor();
}

Expand Down
2 changes: 1 addition & 1 deletion src/modules/Telemetry/Sensor/MCP9808Sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "TelemetrySensor.h"
#include <Adafruit_MCP9808.h>

class MCP9808Sensor : virtual public TelemetrySensor
class MCP9808Sensor : public TelemetrySensor
{
private:
Adafruit_MCP9808 mcp9808;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/Telemetry/Sensor/SHT31Sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "TelemetrySensor.h"
#include <Adafruit_SHT31.h>

class SHT31Sensor : virtual public TelemetrySensor
class SHT31Sensor : public TelemetrySensor
{
private:
Adafruit_SHT31 sht31 = Adafruit_SHT31();
Expand Down
2 changes: 1 addition & 1 deletion src/modules/Telemetry/Sensor/SHTC3Sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "TelemetrySensor.h"
#include <Adafruit_SHTC3.h>

class SHTC3Sensor : virtual public TelemetrySensor
class SHTC3Sensor : public TelemetrySensor
{
private:
Adafruit_SHTC3 shtc3 = Adafruit_SHTC3();
Expand Down
13 changes: 8 additions & 5 deletions src/modules/Telemetry/Sensor/TelemetrySensor.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#pragma once
#include "../mesh/generated/meshtastic/telemetry.pb.h"
#include "NodeDB.h"
#include <utility>

class TwoWire;

#define DEFAULT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS 1000
extern uint8_t nodeTelemetrySensorsMap[_meshtastic_TelemetrySensorType_MAX + 1];
extern std::pair<uint8_t, TwoWire *> nodeTelemetrySensorsMap[_meshtastic_TelemetrySensorType_MAX + 1];

class TelemetrySensor
{
Expand All @@ -16,17 +19,17 @@ class TelemetrySensor
}

const char *sensorName;
meshtastic_TelemetrySensorType sensorType;
meshtastic_TelemetrySensorType sensorType = meshtastic_TelemetrySensorType_SENSOR_UNSET;
unsigned status;
bool initialized = false;

int32_t initI2CSensor()
{
if (!status) {
LOG_WARN("Could not connect to detected %s sensor.\n Removing from nodeTelemetrySensorsMap.\n", sensorName);
nodeTelemetrySensorsMap[sensorType] = 0;
nodeTelemetrySensorsMap[sensorType].first = 0;
} else {
LOG_INFO("Opened %s sensor on default i2c bus\n", sensorName);
LOG_INFO("Opened %s sensor on i2c bus\n", sensorName);
setup();
}
initialized = true;
Expand All @@ -35,7 +38,7 @@ class TelemetrySensor
virtual void setup();

public:
bool hasSensor() { return sensorType < sizeof(nodeTelemetrySensorsMap) && nodeTelemetrySensorsMap[sensorType] > 0; }
bool hasSensor() { return nodeTelemetrySensorsMap[sensorType].first > 0; }

virtual int32_t runOnce() = 0;
virtual bool isInitialized() { return initialized; }
Expand Down
Loading