From 0200352f915a9a1497be710df8f0fc0ebee041c4 Mon Sep 17 00:00:00 2001 From: andreas Date: Tue, 19 Nov 2024 18:58:29 +0100 Subject: [PATCH] intermediate, untested: move creator to SensorBase --- lib/iictask/GwBME280.cpp | 8 ++++---- lib/iictask/GwBME280.h | 2 +- lib/iictask/GwBMP280.cpp | 13 ++++++------- lib/iictask/GwBMP280.h | 2 +- lib/iictask/GwIicSensors.h | 3 ++- lib/iictask/GwIicTask.cpp | 6 +++--- lib/iictask/GwQMP6988.cpp | 8 ++++---- lib/iictask/GwQMP6988.h | 2 +- lib/iictask/GwSHT3X.cpp | 8 ++++---- lib/iictask/GwSHT3X.h | 2 +- lib/sensors/GwSensor.h | 28 +++++++++++++++++++++------- lib/spitask/GWDMS22B.cpp | 2 +- lib/spitask/GwSpiSensor.h | 4 ++-- 13 files changed, 51 insertions(+), 37 deletions(-) diff --git a/lib/iictask/GwBME280.cpp b/lib/iictask/GwBME280.cpp index 656718c..e5aa36d 100644 --- a/lib/iictask/GwBME280.cpp +++ b/lib/iictask/GwBME280.cpp @@ -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); }); -IICSensorBase::Creator registerBME280(GwApi *api){ +SensorBase::Creator registerBME280(GwApi *api){ #if defined(GWBME280) || defined(GWBME28011) { api->addSensor(creator(api,PRFX1)); @@ -181,8 +181,8 @@ IICSensorBase::Creator registerBME280(GwApi *api){ return creator; } #else -IICSensorBase::Creator registerBME280(GwApi *api){ - return IICSensorBase::Creator(); +SensorBase::Creator registerBME280(GwApi *api){ + return SensorBase::Creator(); } #endif diff --git a/lib/iictask/GwBME280.h b/lib/iictask/GwBME280.h index a9ee4ec..c74adf2 100644 --- a/lib/iictask/GwBME280.h +++ b/lib/iictask/GwBME280.h @@ -1,5 +1,5 @@ #ifndef _GWBME280_H #define _GWBME280_H #include "GwIicSensors.h" -IICSensorBase::Creator registerBME280(GwApi *api); +SensorBase::Creator registerBME280(GwApi *api); #endif \ No newline at end of file diff --git a/lib/iictask/GwBMP280.cpp b/lib/iictask/GwBMP280.cpp index a6108bc..1608466 100644 --- a/lib/iictask/GwBMP280.cpp +++ b/lib/iictask/GwBMP280.cpp @@ -25,7 +25,7 @@ class BMP280Config; GwSensorConfigInitializerList configs; -class BMP280Config : public IICSensorBase{ +class BMP280Config : public IICSensorBase{ public: bool prAct=true; bool tmAct=true; @@ -38,8 +38,7 @@ class BMP280Config : public IICSensorBase{ float prOff=0; Adafruit_BMP280 *device=nullptr; uint32_t sensorId=-1; - BMP280Config(GwApi * api, const String &prfx):IICSensorBase("BMP280",api,prfx){ - } + using IICSensorBase::IICSensorBase; virtual bool isActive(){return prAct||tmAct;} virtual bool initDevice(GwApi *api,TwoWire *wire){ 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)){ return nullptr; } return new BMP280Config(api,prfx); }); -IICSensorBase::Creator registerBMP280(GwApi *api){ +SensorBase::Creator registerBMP280(GwApi *api){ #if defined(GWBMP280) || defined(GWBMP28011) { api->addSensor(creator(api,"BMP28011")); @@ -171,8 +170,8 @@ SCBMP280(configs, BMP28021, 2, 0x76); SCBMP280(configs, BMP28022, 2, 0x77); #else -IICSensorBase::Creator registerBMP280(GwApi *api){ - return IICSensorBase::Creator(); +SensorBase::Creator registerBMP280(GwApi *api){ + return SensorBase::Creator(); } #endif diff --git a/lib/iictask/GwBMP280.h b/lib/iictask/GwBMP280.h index b2fd0bc..f5a3e2c 100644 --- a/lib/iictask/GwBMP280.h +++ b/lib/iictask/GwBMP280.h @@ -1,6 +1,6 @@ #ifndef _GWBMP280_H #define _GWBMP280_H #include "GwIicSensors.h" -IICSensorBase::Creator registerBMP280(GwApi *api); +SensorBase::Creator registerBMP280(GwApi *api); #endif diff --git a/lib/iictask/GwIicSensors.h b/lib/iictask/GwIicSensors.h index 4937daa..66e64f4 100644 --- a/lib/iictask/GwIicSensors.h +++ b/lib/iictask/GwIicSensors.h @@ -13,7 +13,8 @@ using BUSTYPE=TwoWire; using IICSensorList=SensorList; -using IICSensorBase=SensorTemplate; +template +using IICSensorBase=SensorTemplate; template diff --git a/lib/iictask/GwIicTask.cpp b/lib/iictask/GwIicTask.cpp index e815054..fd5391e 100644 --- a/lib/iictask/GwIicTask.cpp +++ b/lib/iictask/GwIicTask.cpp @@ -43,7 +43,7 @@ static std::vector iicGroveList; void runIicTask(GwApi *api); -static void addGroveItems(std::vector &creators,GwApi *api, const String &bus,const String &grove, int, int) +static void addGroveItems(std::vector &creators,GwApi *api, const String &bus,const String &grove, int, int) { GwLog *logger=api->getLogger(); for (auto &&init : iicGroveList) @@ -71,7 +71,7 @@ static void addGroveItems(std::vector &creators,GwApi *a } 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; } } @@ -89,7 +89,7 @@ void initIicTask(GwApi *api){ #else bool addTask=false; GwConfigHandler *config=api->getConfig(); - std::vector creators; + std::vector creators; creators.push_back(registerSHT3X(api)); creators.push_back(registerQMP6988(api)); creators.push_back(registerBME280(api)); diff --git a/lib/iictask/GwQMP6988.cpp b/lib/iictask/GwQMP6988.cpp index dbcde7d..b599296 100644 --- a/lib/iictask/GwQMP6988.cpp +++ b/lib/iictask/GwQMP6988.cpp @@ -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); }; -IICSensorBase::Creator registerQMP6988(GwApi *api,IICSensorList &sensors){ +SensorBase::Creator registerQMP6988(GwApi *api,IICSensorList &sensors){ GwLog *logger=api->getLogger(); #if defined(GWQMP6988) || defined(GWQMP698811) { @@ -116,7 +116,7 @@ IICSensorBase::Creator registerQMP6988(GwApi *api,IICSensorList &sensors){ } #else - IICSensorBase::Creator registerQMP6988(GwApi *api){ - return IICSensorBase::Creator(); + SensorBase::Creator registerQMP6988(GwApi *api){ + return SensorBase::Creator(); } #endif \ No newline at end of file diff --git a/lib/iictask/GwQMP6988.h b/lib/iictask/GwQMP6988.h index af00616..135cd81 100644 --- a/lib/iictask/GwQMP6988.h +++ b/lib/iictask/GwQMP6988.h @@ -16,5 +16,5 @@ #ifdef _GWQMP6988 #include "QMP6988.h" #endif -IICSensorBase::Creator registerQMP6988(GwApi *api); +SensorBase::Creator registerQMP6988(GwApi *api); #endif \ No newline at end of file diff --git a/lib/iictask/GwSHT3X.cpp b/lib/iictask/GwSHT3X.cpp index 2de7fd8..7c78f90 100644 --- a/lib/iictask/GwSHT3X.cpp +++ b/lib/iictask/GwSHT3X.cpp @@ -106,10 +106,10 @@ class SHT3XConfig : public IICSensorBase{ intv*=1000; } }; -IICSensorBase::Creator creator=[](GwApi *api,const String &prfx){ +SensorBase::Creator creator=[](GwApi *api,const String &prfx){ return new SHT3XConfig(api,prfx); }; -IICSensorBase::Creator registerSHT3X(GwApi *api){ +SensorBase::Creator registerSHT3X(GwApi *api){ GwLog *logger=api->getLogger(); #if defined(GWSHT3X) || defined (GWSHT3X11) { @@ -143,8 +143,8 @@ IICSensorBase::Creator registerSHT3X(GwApi *api){ }; #else -IICSensorBase::Creator registerSHT3X(GwApi *api){ - return IICSensorBase::Creator(); +SensorBase::Creator registerSHT3X(GwApi *api){ + return SensorBase::Creator(); } #endif diff --git a/lib/iictask/GwSHT3X.h b/lib/iictask/GwSHT3X.h index b9beb79..6a5dfcf 100644 --- a/lib/iictask/GwSHT3X.h +++ b/lib/iictask/GwSHT3X.h @@ -16,5 +16,5 @@ #ifdef _GWSHT3X #include "SHT3X.h" #endif -IICSensorBase::Creator registerSHT3X(GwApi *api); +SensorBase::Creator registerSHT3X(GwApi *api); #endif \ No newline at end of file diff --git a/lib/sensors/GwSensor.h b/lib/sensors/GwSensor.h index e1617ea..0e469e5 100644 --- a/lib/sensors/GwSensor.h +++ b/lib/sensors/GwSensor.h @@ -33,12 +33,10 @@ class SensorBase{ int iid=99; //N2K instanceId int addr=-1; const String prefix; - const String type; long intv=0; bool ok=false; - virtual void readConfig(GwConfigHandler *cfg)=0; - SensorBase(BusType bt,const String &tn,GwApi *api,const String &prfx) - :busType(bt),type(tn),prefix(prfx){ + SensorBase(BusType bt,GwApi *api,const String &prfx) + :busType(bt),prefix(prfx){ } using Creator=std::function; virtual bool isActive(){return false;}; @@ -46,12 +44,18 @@ class SensorBase{ virtual bool preinit(GwApi * api){return false;} virtual void measure(GwApi * api,void *bus, int counterId){}; virtual ~SensorBase(){} + virtual void readConfig(GwConfigHandler *cfg)=0; + }; -template +template class SensorTemplate : public SensorBase{ public: - SensorTemplate(const String &tn,GwApi *api,const String &prfx) - :SensorBase(bt,tn,api,prfx){} + using ConfigReader=std::function; + 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,void *bus){ if (busType != bt) return false; @@ -61,6 +65,16 @@ class SensorTemplate : public SensorBase{ if (busType != bt) return; 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; + } }; diff --git a/lib/spitask/GWDMS22B.cpp b/lib/spitask/GWDMS22B.cpp index 94734e7..715ce83 100644 --- a/lib/spitask/GWDMS22B.cpp +++ b/lib/spitask/GWDMS22B.cpp @@ -83,7 +83,7 @@ class GWDMS22B : public SSISensor{ bool invt=false; String zeroConfigName; 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){ GwLog *logger=api->getLogger(); LOG_DEBUG(GwLog::LOG,"DMS22B configured, prefix=%s, intv=%f, active=%d",prefix.c_str(),fintv,(int)act); diff --git a/lib/spitask/GwSpiSensor.h b/lib/spitask/GwSpiSensor.h index 1a8ab92..9a27522 100644 --- a/lib/spitask/GwSpiSensor.h +++ b/lib/spitask/GwSpiSensor.h @@ -90,7 +90,7 @@ class SSIDevice{ }; -class SSISensor : public SensorTemplate{ +class SSISensor : public SensorTemplate{ std::unique_ptr device; protected: int bits=12; @@ -125,7 +125,7 @@ class SSISensor : public SensorTemplate{ } public: - SSISensor(const String &type,GwApi *api,const String &prfx, int host):SensorTemplate(type,api,prfx) + SSISensor(GwApi *api,const String &prfx, int host):SensorTemplate(api,prfx) { busId=host; }