283 remove settingsManager and avoid the String class
This commit is contained in:
@@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
#define TELEM_CHANNEL_SELF 1 // LPP data channel for 'self' device
|
#define TELEM_CHANNEL_SELF 1 // LPP data channel for 'self' device
|
||||||
|
|
||||||
#define TELEM_INA3221_ADDRESS 0x40 // INA3221 3 channel current, voltage, power sensor I2C address
|
#define TELEM_INA3221_ADDRESS 0x42 // INA3221 3 channel current, voltage, power sensor I2C address
|
||||||
#define TELEM_INA3221_SHUNT_VALUE 0.100 // most variants will have a 0.1 ohm shunts
|
#define TELEM_INA3221_SHUNT_VALUE 0.100 // most variants will have a 0.1 ohm shunts
|
||||||
#define TELEM_INA3221_SETTING_CH1 "INA3221 Channel 1"
|
#define TELEM_INA3221_SETTING_CH1 "INA3221 Channel 1"
|
||||||
#define TELEM_INA3221_SETTING_CH2 "INA3221 Channel 2"
|
#define TELEM_INA3221_SETTING_CH2 "INA3221 Channel 2"
|
||||||
|
|||||||
@@ -1,72 +0,0 @@
|
|||||||
#include <vector>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
class SensorSettingsManager {
|
|
||||||
private:
|
|
||||||
std::vector<std::pair<std::string, bool>> settings;
|
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
int getSettingCount() const {
|
|
||||||
return static_cast<int>(settings.size());
|
|
||||||
};
|
|
||||||
|
|
||||||
bool addSetting(const std::string& name, bool defaultValue = false){
|
|
||||||
for (const auto& setting : settings) {
|
|
||||||
if (setting.first == name) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
settings.emplace_back(name, defaultValue);
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
|
|
||||||
bool removeSetting(const std::string& name) {
|
|
||||||
for (auto it = settings.begin(); it != settings.end(); ++it) {
|
|
||||||
if (it->first == name) {
|
|
||||||
settings.erase(it);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
|
|
||||||
const char* getSettingValue(const std::string& name) const{
|
|
||||||
for (const auto& setting : settings) {
|
|
||||||
if (setting.first == name) {
|
|
||||||
return setting.second ? "true" : "false";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
};
|
|
||||||
|
|
||||||
const char* getSettingValue(int index) const {
|
|
||||||
if (index >= 0 && index < getSettingCount()) {
|
|
||||||
return settings[index].second ? "true" : "false";
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
};
|
|
||||||
|
|
||||||
bool setSettingValue(const std::string& name, const std::string& value) {
|
|
||||||
for (auto& setting : settings) {
|
|
||||||
if (setting.first == name) {
|
|
||||||
// Convert value to boolean
|
|
||||||
if (value == "1" || value == "true") {
|
|
||||||
setting.second = true;
|
|
||||||
} else {
|
|
||||||
setting.second = false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char* getSettingName(int index) const {
|
|
||||||
if (index >= 0 && index < getSettingCount()){
|
|
||||||
return settings[index].first.c_str();
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
};
|
|
||||||
|
|
||||||
};
|
|
||||||
@@ -75,34 +75,21 @@ mesh::LocalIdentity radio_new_identity() {
|
|||||||
return mesh::LocalIdentity(&rng); // create new random identity
|
return mesh::LocalIdentity(&rng); // create new random identity
|
||||||
}
|
}
|
||||||
|
|
||||||
PromicroSensorManager::PromicroSensorManager(){
|
INA3221 INA_3221(TELEM_INA3221_ADDRESS, &Wire);
|
||||||
INA_3221 = new INA3221(TELEM_INA3221_ADDRESS, &Wire);
|
|
||||||
}
|
|
||||||
|
|
||||||
PromicroSensorManager::~PromicroSensorManager(){
|
|
||||||
if (INA_3221) {
|
|
||||||
delete INA_3221;
|
|
||||||
INA_3221 = nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool PromicroSensorManager::begin() {
|
bool PromicroSensorManager::begin() {
|
||||||
if (INA_3221->begin() ) {
|
if (INA_3221.begin() ) {
|
||||||
Serial.print("Found INA3221 at address ");
|
Serial.print("Found INA3221 at address ");
|
||||||
Serial.print(INA_3221->getAddress());
|
Serial.print(INA_3221.getAddress());
|
||||||
Serial.println();
|
Serial.println();
|
||||||
Serial.print(INA_3221->getDieID(), HEX);
|
Serial.print(INA_3221.getDieID(), HEX);
|
||||||
Serial.print(INA_3221->getManufacturerID(), HEX);
|
Serial.print(INA_3221.getManufacturerID(), HEX);
|
||||||
Serial.print(INA_3221->getConfiguration(), HEX);
|
Serial.print(INA_3221.getConfiguration(), HEX);
|
||||||
Serial.println();
|
Serial.println();
|
||||||
|
|
||||||
for(int i = 0; i < 3; i++) {
|
for(int i = 0; i < 3; i++) {
|
||||||
INA_3221->setShuntR(i, TELEM_INA3221_SHUNT_VALUE);
|
INA_3221.setShuntR(i, TELEM_INA3221_SHUNT_VALUE);
|
||||||
}
|
}
|
||||||
// add INA3221 settings to settings manager
|
|
||||||
settingsManager.addSetting(TELEM_INA3221_SETTING_CH1, true);
|
|
||||||
settingsManager.addSetting(TELEM_INA3221_SETTING_CH2, true);
|
|
||||||
settingsManager.addSetting(TELEM_INA3221_SETTING_CH3, true);
|
|
||||||
INA3221initialized = true;
|
INA3221initialized = true;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -120,22 +107,21 @@ bool PromicroSensorManager::querySensors(uint8_t requester_permissions, CayenneL
|
|||||||
if (INA3221initialized) {
|
if (INA3221initialized) {
|
||||||
for(int i = 0; i < 3; i++) {
|
for(int i = 0; i < 3; i++) {
|
||||||
// add only enabled INA3221 channels to telemetry
|
// add only enabled INA3221 channels to telemetry
|
||||||
if (settingsManager.getSettingValue(INA3221_CHANNEL_NAMES[i]) == "true") {
|
if (INA3221_CHANNEL_ENABLED[i]) {
|
||||||
|
|
||||||
// TODO: remove when telemetry support gets properly added
|
// TODO: remove when telemetry support gets properly added
|
||||||
// Serial.print("CH");
|
// Serial.print("CH");
|
||||||
// Serial.print(i);
|
// Serial.print(i);
|
||||||
// Serial.print(" Voltage: ");
|
// Serial.print(" Voltage: ");
|
||||||
// Serial.print(INA_3221->getBusVoltage(i));
|
// Serial.print(INA_3221.getBusVoltage(i));
|
||||||
// Serial.print("V Current: ");
|
// Serial.print("V Current: ");
|
||||||
// Serial.print(INA_3221->getCurrent(i));
|
// Serial.print(INA_3221.getCurrent(i));
|
||||||
// Serial.print("A Power: ");
|
// Serial.print("A Power: ");
|
||||||
// Serial.print(INA_3221->getPower(i));
|
// Serial.print(INA_3221.getPower(i));
|
||||||
// Serial.println();
|
// Serial.println();
|
||||||
|
|
||||||
telemetry.addVoltage(INA3221_CHANNELS[i], INA_3221->getBusVoltage(i));
|
telemetry.addVoltage(INA3221_CHANNELS[i], INA_3221.getBusVoltage(i));
|
||||||
telemetry.addCurrent(INA3221_CHANNELS[i], INA_3221->getCurrent(i));
|
telemetry.addCurrent(INA3221_CHANNELS[i], INA_3221.getCurrent(i));
|
||||||
telemetry.addPower(INA3221_CHANNELS[i], INA_3221->getPower(i));
|
telemetry.addPower(INA3221_CHANNELS[i], INA_3221.getPower(i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -145,36 +131,40 @@ bool PromicroSensorManager::querySensors(uint8_t requester_permissions, CayenneL
|
|||||||
}
|
}
|
||||||
|
|
||||||
int PromicroSensorManager::getNumSettings() const {
|
int PromicroSensorManager::getNumSettings() const {
|
||||||
return settingsManager.getSettingCount();
|
return NUM_SENSOR_SETTINGS;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* PromicroSensorManager::getSettingName(int i) const {
|
const char* PromicroSensorManager::getSettingName(int i) const {
|
||||||
return settingsManager.getSettingName(i);
|
if (i >= 0 && i < NUM_SENSOR_SETTINGS) {
|
||||||
|
return INA3221_CHANNEL_NAMES[i];
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* PromicroSensorManager::getSettingValue(int i) const {
|
const char* PromicroSensorManager::getSettingValue(int i) const {
|
||||||
return settingsManager.getSettingValue(i);
|
if (i >= 0 && i < NUM_SENSOR_SETTINGS) {
|
||||||
|
return INA3221_CHANNEL_ENABLED[i] ? "1" : "0";
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PromicroSensorManager::setSettingValue(const char* name, const char* value) {
|
bool PromicroSensorManager::setSettingValue(const char* name, const char* value) {
|
||||||
if (settingsManager.setSettingValue(name, value)) {
|
for (int i = 0; i < NUM_SENSOR_SETTINGS; i++) {
|
||||||
onSettingsChanged();
|
if (strcmp(name, INA3221_CHANNEL_NAMES[i]) == 0) {
|
||||||
return true;
|
int channelEnabled = INA_3221.getEnableChannel(i);
|
||||||
}
|
if (strcmp(value, "1") == 0) {
|
||||||
return false;
|
INA3221_CHANNEL_ENABLED[i] = true;
|
||||||
}
|
if (!channelEnabled) {
|
||||||
|
INA_3221.enableChannel(i);
|
||||||
void PromicroSensorManager::onSettingsChanged() {
|
}
|
||||||
if (INA3221initialized) {
|
} else {
|
||||||
for(int i = 0; i < 3; i++) {
|
INA3221_CHANNEL_ENABLED[i] = false;
|
||||||
int channelEnabled = INA_3221->getEnableChannel(i);
|
if (channelEnabled) {
|
||||||
bool settingEnabled = settingsManager.getSettingValue(INA3221_CHANNEL_NAMES[i]) == "true";
|
INA_3221.disableChannel(i);
|
||||||
if (!settingEnabled && channelEnabled) {
|
}
|
||||||
INA_3221->disableChannel(i);
|
|
||||||
}
|
|
||||||
if (settingEnabled && !channelEnabled) {
|
|
||||||
INA_3221->enableChannel(i);
|
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
@@ -8,9 +8,10 @@
|
|||||||
#include <helpers/CustomLLCC68Wrapper.h>
|
#include <helpers/CustomLLCC68Wrapper.h>
|
||||||
#include <helpers/AutoDiscoverRTCClock.h>
|
#include <helpers/AutoDiscoverRTCClock.h>
|
||||||
#include <helpers/SensorManager.h>
|
#include <helpers/SensorManager.h>
|
||||||
#include <helpers/sensors/SensorSettingsManager.h>
|
|
||||||
#include <INA3221.h>
|
#include <INA3221.h>
|
||||||
|
|
||||||
|
#define NUM_SENSOR_SETTINGS 3
|
||||||
|
|
||||||
extern PromicroBoard board;
|
extern PromicroBoard board;
|
||||||
extern WRAPPER_CLASS radio_driver;
|
extern WRAPPER_CLASS radio_driver;
|
||||||
extern AutoDiscoverRTCClock rtc_clock;
|
extern AutoDiscoverRTCClock rtc_clock;
|
||||||
@@ -22,19 +23,17 @@ void radio_set_params(float freq, float bw, uint8_t sf, uint8_t cr);
|
|||||||
void radio_set_tx_power(uint8_t dbm);
|
void radio_set_tx_power(uint8_t dbm);
|
||||||
mesh::LocalIdentity radio_new_identity();
|
mesh::LocalIdentity radio_new_identity();
|
||||||
|
|
||||||
|
|
||||||
class PromicroSensorManager: public SensorManager {
|
class PromicroSensorManager: public SensorManager {
|
||||||
INA3221 * INA_3221;
|
|
||||||
bool INA3221initialized = false;
|
bool INA3221initialized = false;
|
||||||
SensorSettingsManager settingsManager;
|
|
||||||
|
|
||||||
// INA3221 channels in telemetry
|
// INA3221 channels in telemetry
|
||||||
int INA3221_CHANNELS[3] = {TELEM_CHANNEL_SELF + 1, TELEM_CHANNEL_SELF + 2, TELEM_CHANNEL_SELF+ 3};
|
int INA3221_CHANNELS[NUM_SENSOR_SETTINGS] = {TELEM_CHANNEL_SELF + 1, TELEM_CHANNEL_SELF + 2, TELEM_CHANNEL_SELF+ 3};
|
||||||
char * INA3221_CHANNEL_NAMES[3] = { TELEM_INA3221_SETTING_CH1, TELEM_INA3221_SETTING_CH2, TELEM_INA3221_SETTING_CH3};
|
char * INA3221_CHANNEL_NAMES[NUM_SENSOR_SETTINGS] = { TELEM_INA3221_SETTING_CH1, TELEM_INA3221_SETTING_CH2, TELEM_INA3221_SETTING_CH3};
|
||||||
void onSettingsChanged();
|
bool INA3221_CHANNEL_ENABLED[NUM_SENSOR_SETTINGS] = {true, true, true};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
PromicroSensorManager();
|
PromicroSensorManager(){};
|
||||||
~PromicroSensorManager();
|
|
||||||
bool begin() override;
|
bool begin() override;
|
||||||
bool querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) override;
|
bool querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) override;
|
||||||
int getNumSettings() const override;
|
int getNumSettings() const override;
|
||||||
|
|||||||
Reference in New Issue
Block a user