intermediate, untested: move creator to SensorBase

This commit is contained in:
andreas 2024-11-19 18:58:29 +01:00
parent d904d15ece
commit 0200352f91
13 changed files with 51 additions and 37 deletions

View File

@ -146,10 +146,10 @@ class BME280Config : public IICSensorBase{
} }
}; };
static IICSensorBase::Creator creator([](GwApi *api, const String &prfx){ static SensorBase::Creator creator([](GwApi *api, const String &prfx){
return new BME280Config(api,prfx); return new BME280Config(api,prfx);
}); });
IICSensorBase::Creator registerBME280(GwApi *api){ SensorBase::Creator registerBME280(GwApi *api){
#if defined(GWBME280) || defined(GWBME28011) #if defined(GWBME280) || defined(GWBME28011)
{ {
api->addSensor(creator(api,PRFX1)); api->addSensor(creator(api,PRFX1));
@ -181,8 +181,8 @@ IICSensorBase::Creator registerBME280(GwApi *api){
return creator; return creator;
} }
#else #else
IICSensorBase::Creator registerBME280(GwApi *api){ SensorBase::Creator registerBME280(GwApi *api){
return IICSensorBase::Creator(); return SensorBase::Creator();
} }
#endif #endif

View File

@ -1,5 +1,5 @@
#ifndef _GWBME280_H #ifndef _GWBME280_H
#define _GWBME280_H #define _GWBME280_H
#include "GwIicSensors.h" #include "GwIicSensors.h"
IICSensorBase::Creator registerBME280(GwApi *api); SensorBase::Creator registerBME280(GwApi *api);
#endif #endif

View File

@ -25,7 +25,7 @@
class BMP280Config; class BMP280Config;
GwSensorConfigInitializerList<BMP280Config> configs; GwSensorConfigInitializerList<BMP280Config> configs;
class BMP280Config : public IICSensorBase{ class BMP280Config : public IICSensorBase<BMP280Config>{
public: public:
bool prAct=true; bool prAct=true;
bool tmAct=true; bool tmAct=true;
@ -38,8 +38,7 @@ class BMP280Config : public IICSensorBase{
float prOff=0; float prOff=0;
Adafruit_BMP280 *device=nullptr; Adafruit_BMP280 *device=nullptr;
uint32_t sensorId=-1; uint32_t sensorId=-1;
BMP280Config(GwApi * api, const String &prfx):IICSensorBase("BMP280",api,prfx){ using IICSensorBase<BMP280Config>::IICSensorBase;
}
virtual bool isActive(){return prAct||tmAct;} virtual bool isActive(){return prAct||tmAct;}
virtual bool initDevice(GwApi *api,TwoWire *wire){ virtual bool initDevice(GwApi *api,TwoWire *wire){
GwLog *logger=api->getLogger(); GwLog *logger=api->getLogger();
@ -98,13 +97,13 @@ class BMP280Config : public IICSensorBase{
}; };
static IICSensorBase::Creator creator([](GwApi *api, const String &prfx)->BMP280Config*{ static SensorBase::Creator creator([](GwApi *api, const String &prfx)->BMP280Config*{
if (! configs.knowsPrefix(prfx)){ if (! configs.knowsPrefix(prfx)){
return nullptr; return nullptr;
} }
return new BMP280Config(api,prfx); return new BMP280Config(api,prfx);
}); });
IICSensorBase::Creator registerBMP280(GwApi *api){ SensorBase::Creator registerBMP280(GwApi *api){
#if defined(GWBMP280) || defined(GWBMP28011) #if defined(GWBMP280) || defined(GWBMP28011)
{ {
api->addSensor(creator(api,"BMP28011")); api->addSensor(creator(api,"BMP28011"));
@ -171,8 +170,8 @@ SCBMP280(configs, BMP28021, 2, 0x76);
SCBMP280(configs, BMP28022, 2, 0x77); SCBMP280(configs, BMP28022, 2, 0x77);
#else #else
IICSensorBase::Creator registerBMP280(GwApi *api){ SensorBase::Creator registerBMP280(GwApi *api){
return IICSensorBase::Creator(); return SensorBase::Creator();
} }
#endif #endif

View File

@ -1,6 +1,6 @@
#ifndef _GWBMP280_H #ifndef _GWBMP280_H
#define _GWBMP280_H #define _GWBMP280_H
#include "GwIicSensors.h" #include "GwIicSensors.h"
IICSensorBase::Creator registerBMP280(GwApi *api); SensorBase::Creator registerBMP280(GwApi *api);
#endif #endif

View File

@ -13,7 +13,8 @@
using BUSTYPE=TwoWire; using BUSTYPE=TwoWire;
using IICSensorList=SensorList; using IICSensorList=SensorList;
using IICSensorBase=SensorTemplate<BUSTYPE,SensorBase::IIC>; template<typename Sensor>
using IICSensorBase=SensorTemplate<BUSTYPE,SensorBase::IIC,Sensor>;
template <class CFG> template <class CFG>

View File

@ -43,7 +43,7 @@ static std::vector<IICGrove> iicGroveList;
void runIicTask(GwApi *api); void runIicTask(GwApi *api);
static void addGroveItems(std::vector<IICSensorBase::Creator> &creators,GwApi *api, const String &bus,const String &grove, int, int) static void addGroveItems(std::vector<SensorBase::Creator> &creators,GwApi *api, const String &bus,const String &grove, int, int)
{ {
GwLog *logger=api->getLogger(); GwLog *logger=api->getLogger();
for (auto &&init : iicGroveList) for (auto &&init : iicGroveList)
@ -71,7 +71,7 @@ static void addGroveItems(std::vector<IICSensorBase::Creator> &creators,GwApi *a
} }
else else
{ {
LOG_DEBUG(GwLog::DEBUG, "unmatched grove sensor config %s for %s", prfx.c_str(), scfg->type.c_str()); LOG_DEBUG(GwLog::DEBUG, "unmatched grove sensor config %s", prfx.c_str());
delete scfg; delete scfg;
} }
} }
@ -89,7 +89,7 @@ void initIicTask(GwApi *api){
#else #else
bool addTask=false; bool addTask=false;
GwConfigHandler *config=api->getConfig(); GwConfigHandler *config=api->getConfig();
std::vector<IICSensorBase::Creator> creators; std::vector<SensorBase::Creator> creators;
creators.push_back(registerSHT3X(api)); creators.push_back(registerSHT3X(api));
creators.push_back(registerQMP6988(api)); creators.push_back(registerQMP6988(api));
creators.push_back(registerBME280(api)); creators.push_back(registerBME280(api));

View File

@ -79,10 +79,10 @@ class QMP6988Config : public IICSensorBase{
} }
}; };
static IICSensorBase::Creator creator=[](GwApi *api,const String &prfx){ static SensorBase::Creator creator=[](GwApi *api,const String &prfx){
return new QMP6988Config(api,prfx); return new QMP6988Config(api,prfx);
}; };
IICSensorBase::Creator registerQMP6988(GwApi *api,IICSensorList &sensors){ SensorBase::Creator registerQMP6988(GwApi *api,IICSensorList &sensors){
GwLog *logger=api->getLogger(); GwLog *logger=api->getLogger();
#if defined(GWQMP6988) || defined(GWQMP698811) #if defined(GWQMP6988) || defined(GWQMP698811)
{ {
@ -116,7 +116,7 @@ IICSensorBase::Creator registerQMP6988(GwApi *api,IICSensorList &sensors){
} }
#else #else
IICSensorBase::Creator registerQMP6988(GwApi *api){ SensorBase::Creator registerQMP6988(GwApi *api){
return IICSensorBase::Creator(); return SensorBase::Creator();
} }
#endif #endif

View File

@ -16,5 +16,5 @@
#ifdef _GWQMP6988 #ifdef _GWQMP6988
#include "QMP6988.h" #include "QMP6988.h"
#endif #endif
IICSensorBase::Creator registerQMP6988(GwApi *api); SensorBase::Creator registerQMP6988(GwApi *api);
#endif #endif

View File

@ -106,10 +106,10 @@ class SHT3XConfig : public IICSensorBase{
intv*=1000; intv*=1000;
} }
}; };
IICSensorBase::Creator creator=[](GwApi *api,const String &prfx){ SensorBase::Creator creator=[](GwApi *api,const String &prfx){
return new SHT3XConfig(api,prfx); return new SHT3XConfig(api,prfx);
}; };
IICSensorBase::Creator registerSHT3X(GwApi *api){ SensorBase::Creator registerSHT3X(GwApi *api){
GwLog *logger=api->getLogger(); GwLog *logger=api->getLogger();
#if defined(GWSHT3X) || defined (GWSHT3X11) #if defined(GWSHT3X) || defined (GWSHT3X11)
{ {
@ -143,8 +143,8 @@ IICSensorBase::Creator registerSHT3X(GwApi *api){
}; };
#else #else
IICSensorBase::Creator registerSHT3X(GwApi *api){ SensorBase::Creator registerSHT3X(GwApi *api){
return IICSensorBase::Creator(); return SensorBase::Creator();
} }
#endif #endif

View File

@ -16,5 +16,5 @@
#ifdef _GWSHT3X #ifdef _GWSHT3X
#include "SHT3X.h" #include "SHT3X.h"
#endif #endif
IICSensorBase::Creator registerSHT3X(GwApi *api); SensorBase::Creator registerSHT3X(GwApi *api);
#endif #endif

View File

@ -33,12 +33,10 @@ class SensorBase{
int iid=99; //N2K instanceId int iid=99; //N2K instanceId
int addr=-1; int addr=-1;
const 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; SensorBase(BusType bt,GwApi *api,const String &prfx)
SensorBase(BusType bt,const String &tn,GwApi *api,const String &prfx) :busType(bt),prefix(prfx){
:busType(bt),type(tn),prefix(prfx){
} }
using Creator=std::function<SensorBase *(GwApi *api,const String &prfx)>; using Creator=std::function<SensorBase *(GwApi *api,const String &prfx)>;
virtual bool isActive(){return false;}; virtual bool isActive(){return false;};
@ -46,12 +44,18 @@ class SensorBase{
virtual bool preinit(GwApi * api){return false;} virtual bool preinit(GwApi * api){return false;}
virtual void measure(GwApi * api,void *bus, int counterId){}; virtual void measure(GwApi * api,void *bus, int counterId){};
virtual ~SensorBase(){} virtual ~SensorBase(){}
virtual void readConfig(GwConfigHandler *cfg)=0;
}; };
template<typename BUS,SensorBase::BusType bt> template<typename BUS,SensorBase::BusType bt,typename Sensor>
class SensorTemplate : public SensorBase{ class SensorTemplate : public SensorBase{
public: public:
SensorTemplate(const String &tn,GwApi *api,const String &prfx) using ConfigReader=std::function<bool(Sensor *,GwConfigHandler *)>;
:SensorBase(bt,tn,api,prfx){} protected:
ConfigReader configReader;
public:
SensorTemplate(GwApi *api,const String &prfx)
:SensorBase(bt,api,prfx){}
virtual bool initDevice(GwApi *api,BUS *bus){return false;}; virtual bool initDevice(GwApi *api,BUS *bus){return false;};
virtual bool initDevice(GwApi *api,void *bus){ virtual bool initDevice(GwApi *api,void *bus){
if (busType != bt) return false; if (busType != bt) return false;
@ -61,6 +65,16 @@ class SensorTemplate : public SensorBase{
if (busType != bt) return; if (busType != bt) return;
measure(api,(BUS*)bus,counterId); measure(api,(BUS*)bus,counterId);
}; };
virtual void setConfigReader(ConfigReader r){
configReader=r;
}
protected:
bool configFromReader(Sensor *s, GwConfigHandler *cfg){
if (configReader){
return configReader(s,cfg);
}
return false;
}
}; };

View File

@ -83,7 +83,7 @@ class GWDMS22B : public SSISensor{
bool invt=false; bool invt=false;
String zeroConfigName; String zeroConfigName;
public: public:
GWDMS22B(GwApi *api,const String &prfx, int host):SSISensor("DMS22B",api,prfx,host){} GWDMS22B(GwApi *api,const String &prfx, int host):SSISensor(api,prfx,host){}
virtual bool preinit(GwApi * api){ virtual bool preinit(GwApi * api){
GwLog *logger=api->getLogger(); GwLog *logger=api->getLogger();
LOG_DEBUG(GwLog::LOG,"DMS22B configured, prefix=%s, intv=%f, active=%d",prefix.c_str(),fintv,(int)act); LOG_DEBUG(GwLog::LOG,"DMS22B configured, prefix=%s, intv=%f, active=%d",prefix.c_str(),fintv,(int)act);

View File

@ -90,7 +90,7 @@ class SSIDevice{
}; };
class SSISensor : public SensorTemplate<BUSTYPE,SensorBase::SPI>{ class SSISensor : public SensorTemplate<BUSTYPE,SensorBase::SPI, SSISensor>{
std::unique_ptr<SSIDevice> device; std::unique_ptr<SSIDevice> device;
protected: protected:
int bits=12; int bits=12;
@ -125,7 +125,7 @@ class SSISensor : public SensorTemplate<BUSTYPE,SensorBase::SPI>{
} }
public: public:
SSISensor(const String &type,GwApi *api,const String &prfx, int host):SensorTemplate<BUSTYPE,SensorBase::SPI>(type,api,prfx) SSISensor(GwApi *api,const String &prfx, int host):SensorTemplate<BUSTYPE,SensorBase::SPI,SSISensor>(api,prfx)
{ {
busId=host; busId=host;
} }