correct handling for grove based iic sensors
This commit is contained in:
parent
99d7484afb
commit
b091d9aee4
|
@ -2,8 +2,6 @@
|
||||||
#ifdef _GWIIC
|
#ifdef _GWIIC
|
||||||
#if defined(GWBME280) || defined(GWBME28011) || defined(GWBME28012)|| defined(GWBME28021)|| defined(GWBME28022)
|
#if defined(GWBME280) || defined(GWBME28011) || defined(GWBME28012)|| defined(GWBME28021)|| defined(GWBME28022)
|
||||||
#define _GWBME280
|
#define _GWBME280
|
||||||
#else
|
|
||||||
#undef _GWBME280
|
|
||||||
#endif
|
#endif
|
||||||
#else
|
#else
|
||||||
#undef _GWBME280
|
#undef _GWBME280
|
||||||
|
@ -17,10 +15,11 @@
|
||||||
#include <Adafruit_BME280.h>
|
#include <Adafruit_BME280.h>
|
||||||
#endif
|
#endif
|
||||||
#ifdef _GWBME280
|
#ifdef _GWBME280
|
||||||
#define PRFX1 "BME28011"
|
#define TYPE "BME280"
|
||||||
#define PRFX2 "BME28012"
|
#define PRFX1 TYPE "11"
|
||||||
#define PRFX3 "BME28021"
|
#define PRFX2 TYPE "12"
|
||||||
#define PRFX4 "BME28022"
|
#define PRFX3 TYPE "21"
|
||||||
|
#define PRFX4 TYPE "22"
|
||||||
class BME280Config : public IICSensorBase{
|
class BME280Config : public IICSensorBase{
|
||||||
public:
|
public:
|
||||||
bool prAct=true;
|
bool prAct=true;
|
||||||
|
@ -36,7 +35,7 @@ class BME280Config : public IICSensorBase{
|
||||||
float prOff=0;
|
float prOff=0;
|
||||||
Adafruit_BME280 *device=nullptr;
|
Adafruit_BME280 *device=nullptr;
|
||||||
uint32_t sensorId=-1;
|
uint32_t sensorId=-1;
|
||||||
BME280Config(GwApi * api, const String &prfx):SensorBase(api,prfx){
|
BME280Config(GwApi * api, const String &prfx):SensorBase(TYPE,api,prfx){
|
||||||
}
|
}
|
||||||
virtual bool isActive(){return prAct||huAct||tmAct;}
|
virtual bool isActive(){return prAct||huAct||tmAct;}
|
||||||
virtual bool initDevice(GwApi *api,TwoWire *wire){
|
virtual bool initDevice(GwApi *api,TwoWire *wire){
|
||||||
|
@ -106,6 +105,7 @@ class BME280Config : public IICSensorBase{
|
||||||
|
|
||||||
virtual void readConfig(GwConfigHandler *cfg) override
|
virtual void readConfig(GwConfigHandler *cfg) override
|
||||||
{
|
{
|
||||||
|
if (ok) return;
|
||||||
if (prefix == PRFX1)
|
if (prefix == PRFX1)
|
||||||
{
|
{
|
||||||
busId = 1;
|
busId = 1;
|
||||||
|
@ -137,11 +137,13 @@ class BME280Config : public IICSensorBase{
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static IICSensorBase::Creator creator([](GwApi *api, const String &prfx){
|
||||||
void registerBME280(GwApi *api,IICSensorList &sensors){
|
return new BME280Config(api,prfx);
|
||||||
|
});
|
||||||
|
IICSensorBase::Creator registerBME280(GwApi *api,IICSensorList &sensors){
|
||||||
#if defined(GWBME280) || defined(GWBME28011)
|
#if defined(GWBME280) || defined(GWBME28011)
|
||||||
{
|
{
|
||||||
BME280Config *cfg=new BME280Config(api,PRFX1);
|
auto *cfg=creator(api,PRFX1);
|
||||||
sensors.add(api,cfg);
|
sensors.add(api,cfg);
|
||||||
CHECK_IIC1();
|
CHECK_IIC1();
|
||||||
#pragma message "GWBME28011 defined"
|
#pragma message "GWBME28011 defined"
|
||||||
|
@ -149,7 +151,7 @@ void registerBME280(GwApi *api,IICSensorList &sensors){
|
||||||
#endif
|
#endif
|
||||||
#if defined(GWBME28012)
|
#if defined(GWBME28012)
|
||||||
{
|
{
|
||||||
BME280Config *cfg=new BME280Config(api,PRFX2);
|
auto *cfg=creator(api,PRFX2);
|
||||||
sensors.add(api,cfg);
|
sensors.add(api,cfg);
|
||||||
CHECK_IIC1();
|
CHECK_IIC1();
|
||||||
#pragma message "GWBME28012 defined"
|
#pragma message "GWBME28012 defined"
|
||||||
|
@ -157,7 +159,7 @@ void registerBME280(GwApi *api,IICSensorList &sensors){
|
||||||
#endif
|
#endif
|
||||||
#if defined(GWBME28021)
|
#if defined(GWBME28021)
|
||||||
{
|
{
|
||||||
BME280Config *cfg=new BME280Config(api,PRFX3);
|
auto *cfg=creator(api,PRFX3);
|
||||||
sensors.add(api,cfg);
|
sensors.add(api,cfg);
|
||||||
CHECK_IIC2();
|
CHECK_IIC2();
|
||||||
#pragma message "GWBME28021 defined"
|
#pragma message "GWBME28021 defined"
|
||||||
|
@ -165,15 +167,17 @@ void registerBME280(GwApi *api,IICSensorList &sensors){
|
||||||
#endif
|
#endif
|
||||||
#if defined(GWBME28022)
|
#if defined(GWBME28022)
|
||||||
{
|
{
|
||||||
BME280Config *cfg=new BME280Config(api,PRFX4);
|
auto *cfg=creator(api,PRFX4);
|
||||||
sensors.add(api,cfg);
|
sensors.add(api,cfg);
|
||||||
CHECK_IIC1();
|
CHECK_IIC1();
|
||||||
#pragma message "GWBME28022 defined"
|
#pragma message "GWBME28022 defined"
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
return creator;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
void registerBME280(GwApi *api,IICSensorList &sensors){
|
IICSensorBase::Creator registerBME280(GwApi *api,IICSensorList &sensors){
|
||||||
|
return IICSensorBase::Creator();
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#ifndef _GWBME280_H
|
#ifndef _GWBME280_H
|
||||||
#define _GWBME280_H
|
#define _GWBME280_H
|
||||||
#include "GwIicSensors.h"
|
#include "GwIicSensors.h"
|
||||||
void registerBME280(GwApi *api,IICSensorList &sensors);
|
IICSensorBase::Creator registerBME280(GwApi *api,IICSensorList &sensors);
|
||||||
#endif
|
#endif
|
|
@ -103,8 +103,15 @@ void sendN2kTemperature(GwApi *api,CFG &cfg,double value, int counterId){
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef _GWI_IIC1
|
||||||
#define CHECK_IIC1() checkDef(GWIIC_SCL,GWIIC_SDA)
|
#define CHECK_IIC1() checkDef(GWIIC_SCL,GWIIC_SDA)
|
||||||
#define CHECK_IIC2() checkDef(GWIIC_SCL2,GWIIC_SDA2)
|
#else
|
||||||
|
#define CHECK_IIC1()
|
||||||
|
#endif
|
||||||
|
#ifndef _GWI_IIC2
|
||||||
|
#define CHECK_IIC2() checkDef(GWIIC_SCL2,GWIIC_SDA2)
|
||||||
|
#else
|
||||||
|
#define CHECK_IIC2()
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -43,31 +43,41 @@ static std::vector<IICGrove> iicGroveList;
|
||||||
void runIicTask(GwApi *api);
|
void runIicTask(GwApi *api);
|
||||||
|
|
||||||
static IICSensorList sensors;
|
static IICSensorList sensors;
|
||||||
static void addGroveItems(IICSensorBase::Creator creator,GwApi *api, IICSensorList &sensors, const String &bus,const String &grove, int, int)
|
static void addGroveItems(std::vector<IICSensorBase::Creator> &creators,GwApi *api, IICSensorList &sensors, const String &bus,const String &grove, int, int)
|
||||||
{
|
{
|
||||||
GwLog *logger=api->getLogger();
|
GwLog *logger=api->getLogger();
|
||||||
for (auto &&init : iicGroveList)
|
for (auto &&init : iicGroveList)
|
||||||
{
|
{
|
||||||
LOG_DEBUG(GwLog::LOG, "trying grove item %s:%s:%s for grove %s, bus %s",
|
LOG_DEBUG(GwLog::DEBUG, "trying grove item %s:%s:%s for grove %s, bus %s",
|
||||||
init.base.c_str(),init.grove.c_str(),
|
init.base.c_str(),init.grove.c_str(),
|
||||||
init.suffix.c_str(),grove.c_str(),bus.c_str()
|
init.suffix.c_str(),grove.c_str(),bus.c_str()
|
||||||
);
|
);
|
||||||
String prfx = init.item(grove, bus);
|
String prfx = init.item(grove, bus);
|
||||||
if (!prfx.isEmpty())
|
if (!prfx.isEmpty())
|
||||||
{
|
{
|
||||||
|
bool found=false;
|
||||||
|
for (auto &&creator : creators)
|
||||||
|
{
|
||||||
|
if (! creator) continue;
|
||||||
auto *scfg = creator(api, prfx);
|
auto *scfg = creator(api, prfx);
|
||||||
scfg->readConfig(api->getConfig());
|
scfg->readConfig(api->getConfig());
|
||||||
if (scfg->ok)
|
if (scfg->ok)
|
||||||
{
|
{
|
||||||
LOG_DEBUG(GwLog::LOG, "adding %s from grove config", prfx.c_str());
|
LOG_DEBUG(GwLog::LOG, "adding %s from grove config", prfx.c_str());
|
||||||
sensors.add(api, scfg);
|
sensors.add(api, scfg);
|
||||||
|
found=true;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
LOG_DEBUG(GwLog::ERROR, "invalid grove sensor config %s", prfx.c_str());
|
LOG_DEBUG(GwLog::DEBUG, "unmatched grove sensor config %s for %s", prfx.c_str(), scfg->type.c_str());
|
||||||
delete scfg;
|
delete scfg;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (! found){
|
||||||
|
LOG_DEBUG(GwLog::ERROR,"no iic sensor found for %s",prfx.c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,21 +88,16 @@ void initIicTask(GwApi *api){
|
||||||
#else
|
#else
|
||||||
bool addTask=false;
|
bool addTask=false;
|
||||||
GwConfigHandler *config=api->getConfig();
|
GwConfigHandler *config=api->getConfig();
|
||||||
IICSensorBase::Creator sht3xCreator=registerSHT3X(api,sensors);
|
std::vector<IICSensorBase::Creator> creators;
|
||||||
|
creators.push_back(registerSHT3X(api,sensors));
|
||||||
|
creators.push_back(registerQMP6988(api,sensors));
|
||||||
|
creators.push_back(registerBME280(api,sensors));
|
||||||
#ifdef _GWI_IIC1
|
#ifdef _GWI_IIC1
|
||||||
addGroveItems(sht3xCreator,api,sensors,"1",_GWI_IIC1);
|
addGroveItems(creators,api,sensors,"1",_GWI_IIC1);
|
||||||
#endif
|
#endif
|
||||||
#ifdef _GWI_IIC2
|
#ifdef _GWI_IIC2
|
||||||
addGroveItems(sht3xCreator,api,sensors,"2",_GWI_IIC2);
|
addGroveItems(creators,api,sensors,"2",_GWI_IIC2);
|
||||||
#endif
|
#endif
|
||||||
IICSensorBase::Creator qmp6988Creator=registerQMP6988(api,sensors);
|
|
||||||
#ifdef _GWI_IIC1
|
|
||||||
addGroveItems(qmp6988Creator,api,sensors,"1",_GWI_IIC1);
|
|
||||||
#endif
|
|
||||||
#ifdef _GWI_IIC2
|
|
||||||
addGroveItems(qmp6988Creator,api,sensors,"2",_GWI_IIC2);
|
|
||||||
#endif
|
|
||||||
registerBME280(api,sensors);
|
|
||||||
for (auto it=sensors.begin();it != sensors.end();it++){
|
for (auto it=sensors.begin();it != sensors.end();it++){
|
||||||
if ((*it)->preinit(api)) addTask=true;
|
if ((*it)->preinit(api)) addTask=true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
#define _IIC_GROOVE_LIST
|
#define _IIC_GROOVE_LIST
|
||||||
#include "GwQMP6988.h"
|
#include "GwQMP6988.h"
|
||||||
#ifdef _GWQMP6988
|
#ifdef _GWQMP6988
|
||||||
#define PRFX1 "QMP698811"
|
#define TYPE "QMP6988"
|
||||||
#define PRFX2 "QMP698812"
|
#define PRFX1 TYPE "11"
|
||||||
#define PRFX3 "QMP698821"
|
#define PRFX2 TYPE "12"
|
||||||
#define PRFX4 "QMP698822"
|
#define PRFX3 TYPE "21"
|
||||||
|
#define PRFX4 TYPE "22"
|
||||||
class QMP6988Config : public IICSensorBase{
|
class QMP6988Config : public IICSensorBase{
|
||||||
public:
|
public:
|
||||||
String prNam="Pressure";
|
String prNam="Pressure";
|
||||||
|
@ -12,7 +13,7 @@ class QMP6988Config : public IICSensorBase{
|
||||||
tN2kPressureSource prSrc=tN2kPressureSource::N2kps_Atmospheric;
|
tN2kPressureSource prSrc=tN2kPressureSource::N2kps_Atmospheric;
|
||||||
float prOff=0;
|
float prOff=0;
|
||||||
QMP6988 *device=nullptr;
|
QMP6988 *device=nullptr;
|
||||||
QMP6988Config(GwApi* api,const String &prefix):SensorBase(api,prefix){}
|
QMP6988Config(GwApi* api,const String &prefix):SensorBase(TYPE,api,prefix){}
|
||||||
virtual bool isActive(){return prAct;};
|
virtual bool isActive(){return prAct;};
|
||||||
virtual bool initDevice(GwApi *api,TwoWire *wire){
|
virtual bool initDevice(GwApi *api,TwoWire *wire){
|
||||||
if (!isActive()) return false;
|
if (!isActive()) return false;
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
#include "GwSHT3X.h"
|
#include "GwSHT3X.h"
|
||||||
#ifdef _GWSHT3X
|
#ifdef _GWSHT3X
|
||||||
#define PRFX1 "SHT3X11"
|
#define TYPE "SHT3X"
|
||||||
#define PRFX2 "SHT3X12"
|
#define PRFX1 TYPE "11"
|
||||||
#define PRFX3 "SHT3X21"
|
#define PRFX2 TYPE "12"
|
||||||
#define PRFX4 "SHT3X22"
|
#define PRFX3 TYPE "21"
|
||||||
|
#define PRFX4 TYPE "22"
|
||||||
|
|
||||||
class SHT3XConfig : public IICSensorBase{
|
class SHT3XConfig : public IICSensorBase{
|
||||||
public:
|
public:
|
||||||
|
@ -15,7 +16,7 @@ class SHT3XConfig : public IICSensorBase{
|
||||||
tN2kTempSource tmSrc;
|
tN2kTempSource tmSrc;
|
||||||
SHT3X *device=nullptr;
|
SHT3X *device=nullptr;
|
||||||
SHT3XConfig(GwApi *api,const String &prefix):
|
SHT3XConfig(GwApi *api,const String &prefix):
|
||||||
SensorBase(api,prefix){}
|
SensorBase(TYPE,api,prefix){}
|
||||||
virtual bool isActive(){
|
virtual bool isActive(){
|
||||||
return tmAct || huAct;
|
return tmAct || huAct;
|
||||||
}
|
}
|
||||||
|
@ -147,7 +148,7 @@ IICSensorBase::Creator registerSHT3X(GwApi *api,IICSensorList &sensors){
|
||||||
|
|
||||||
#else
|
#else
|
||||||
IICSensorBase::Creator registerSHT3X(GwApi *api,IICSensorList &sensors){
|
IICSensorBase::Creator registerSHT3X(GwApi *api,IICSensorList &sensors){
|
||||||
|
return IICSensorBase::Creator();
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -22,11 +22,12 @@ class SensorBase{
|
||||||
int busId=0;
|
int busId=0;
|
||||||
int iid=99; //N2K instanceId
|
int iid=99; //N2K instanceId
|
||||||
int addr=-1;
|
int addr=-1;
|
||||||
String prefix;
|
const String prefix;
|
||||||
|
const String type;
|
||||||
long intv=0;
|
long intv=0;
|
||||||
bool ok=false;
|
bool ok=false;
|
||||||
virtual void readConfig(GwConfigHandler *cfg)=0;
|
virtual void readConfig(GwConfigHandler *cfg)=0;
|
||||||
SensorBase(GwApi *api,const String &prfx):prefix(prfx){
|
SensorBase(const String &tn,GwApi *api,const String &prfx):type(tn),prefix(prfx){
|
||||||
}
|
}
|
||||||
using Creator=std::function<SensorBase<BUS> *(GwApi *api,const String &prfx)>;
|
using Creator=std::function<SensorBase<BUS> *(GwApi *api,const String &prfx)>;
|
||||||
virtual bool isActive(){return false;};
|
virtual bool isActive(){return false;};
|
||||||
|
|
|
@ -125,7 +125,7 @@ class SSISensor : public SensorBase<BusType>{
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
SSISensor(GwApi *api,const String &prfx, int host):SensorBase(api,prfx)
|
SSISensor(const String &type,GwApi *api,const String &prfx, int host):SensorBase(type,api,prfx)
|
||||||
{
|
{
|
||||||
busId=host;
|
busId=host;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue