diff --git a/lib/iictask/GwBME280.cpp b/lib/iictask/GwBME280.cpp index 9804e7e..498bf1e 100644 --- a/lib/iictask/GwBME280.cpp +++ b/lib/iictask/GwBME280.cpp @@ -21,7 +21,7 @@ #define PRFX2 "BME28012" #define PRFX3 "BME28021" #define PRFX4 "BME28022" -class BME280Config : public SensorBase{ +class BME280Config : public IICSensorBase{ public: bool prAct=true; bool tmAct=true; @@ -138,7 +138,7 @@ class BME280Config : public SensorBase{ }; -void registerBME280(GwApi *api,SensorList &sensors){ +void registerBME280(GwApi *api,IICSensorList &sensors){ #if defined(GWBME280) || defined(GWBME28011) { BME280Config *cfg=new BME280Config(api,PRFX1); @@ -173,7 +173,7 @@ void registerBME280(GwApi *api,SensorList &sensors){ #endif } #else -void registerBME280(GwApi *api,SensorList &sensors){ +void registerBME280(GwApi *api,IICSensorList &sensors){ } #endif diff --git a/lib/iictask/GwBME280.h b/lib/iictask/GwBME280.h index 03bfe57..5f2abb5 100644 --- a/lib/iictask/GwBME280.h +++ b/lib/iictask/GwBME280.h @@ -1,5 +1,5 @@ #ifndef _GWBME280_H #define _GWBME280_H #include "GwIicSensors.h" -void registerBME280(GwApi *api,SensorList &sensors); +void registerBME280(GwApi *api,IICSensorList &sensors); #endif \ No newline at end of file diff --git a/lib/iictask/GwIicSensors.h b/lib/iictask/GwIicSensors.h index 313e82b..51f787c 100644 --- a/lib/iictask/GwIicSensors.h +++ b/lib/iictask/GwIicSensors.h @@ -4,12 +4,16 @@ #include "N2kMessages.h" #include "GwXdrTypeMappings.h" #include "GwHardware.h" +#include "GwSensor.h" #ifdef _GWIIC #include #else class TwoWire; #endif +using BusType=TwoWire; +using IICSensorList=SensorList; +using IICSensorBase=SensorBase; #define CFG_GET(name,prefix) \ cfg->getValue(name, GwConfigDefinitions::prefix ## name) @@ -100,33 +104,6 @@ void sendN2kTemperature(GwApi *api,CFG &cfg,double value, int counterId){ } -class SensorBase{ - public: - int busId=0; - int iid=99; //N2K instanceId - int addr=-1; - String prefix; - long intv=0; - bool ok=false; - virtual void readConfig(GwConfigHandler *cfg)=0; - SensorBase(GwApi *api,const String &prfx):prefix(prfx){ - } - virtual bool isActive(){return false;}; - virtual bool initDevice(GwApi *api,TwoWire *wire){return false;}; - virtual bool preinit(GwApi * api){return false;} - virtual void measure(GwApi * api,TwoWire *wire, int counterId){}; - virtual ~SensorBase(){} -}; - -class SensorList : public std::vector{ - public: - void add(GwApi *api, SensorBase *sensor){ - sensor->readConfig(api->getConfig()); - api->getLogger()->logDebug(GwLog::LOG,"configured sensor %s, status %d",sensor->prefix.c_str(),(int)sensor->ok); - push_back(sensor); - } - using std::vector::vector; -}; #define CHECK_IIC1() checkDef(GWIIC_SCL,GWIIC_SDA) #define CHECK_IIC2() checkDef(GWIIC_SCL2,GWIIC_SDA2) diff --git a/lib/iictask/GwIicTask.cpp b/lib/iictask/GwIicTask.cpp index 51f3633..9698592 100644 --- a/lib/iictask/GwIicTask.cpp +++ b/lib/iictask/GwIicTask.cpp @@ -26,7 +26,7 @@ void runIicTask(GwApi *api); -static SensorList sensors; +static IICSensorList sensors; void initIicTask(GwApi *api){ GwLog *logger=api->getLogger(); @@ -124,7 +124,7 @@ void runIicTask(GwApi *api){ GwIntervalRunner timers; int counterId=api->addCounter("iicsensors"); for (auto it=sensors.begin();it != sensors.end();it++){ - SensorBase *cfg=*it; + IICSensorBase *cfg=*it; auto bus=buses.find(cfg->busId); if (! cfg->isActive()) continue; if (bus == buses.end()){ diff --git a/lib/iictask/GwQMP6988.cpp b/lib/iictask/GwQMP6988.cpp index 1f83199..638bf0e 100644 --- a/lib/iictask/GwQMP6988.cpp +++ b/lib/iictask/GwQMP6988.cpp @@ -4,7 +4,7 @@ #define PRFX2 "QMP698812" #define PRFX3 "QMP698821" #define PRFX4 "QMP698822" -class QMP6988Config : public SensorBase{ +class QMP6988Config : public IICSensorBase{ public: String prNam="Pressure"; bool prAct=true; @@ -76,7 +76,7 @@ class QMP6988Config : public SensorBase{ } }; -void registerQMP6988(GwApi *api,SensorList &sensors){ +void registerQMP6988(GwApi *api,IICSensorList &sensors){ GwLog *logger=api->getLogger(); #if defined(GWQMP6988) || defined(GWQMP698811) { @@ -113,5 +113,5 @@ void registerQMP6988(GwApi *api,SensorList &sensors){ } #else - void registerQMP6988(GwApi *api,SensorList &sensors){} + void registerQMP6988(GwApi *api,IICSensorList &sensors){} #endif \ No newline at end of file diff --git a/lib/iictask/GwQMP6988.h b/lib/iictask/GwQMP6988.h index 43e6426..5d64454 100644 --- a/lib/iictask/GwQMP6988.h +++ b/lib/iictask/GwQMP6988.h @@ -18,5 +18,5 @@ #ifdef _GWQMP6988 #include "QMP6988.h" #endif -void registerQMP6988(GwApi *api,SensorList &sensors); +void registerQMP6988(GwApi *api,IICSensorList &sensors); #endif \ No newline at end of file diff --git a/lib/iictask/GwSHT3X.cpp b/lib/iictask/GwSHT3X.cpp index e98647c..8305220 100644 --- a/lib/iictask/GwSHT3X.cpp +++ b/lib/iictask/GwSHT3X.cpp @@ -5,7 +5,7 @@ #define PRFX2 "SHT3X12" #define PRFX3 "SHT3X21" #define PRFX4 "SHT3X22" -class SHT3XConfig : public SensorBase{ +class SHT3XConfig : public IICSensorBase{ public: String tmNam; String huNam; @@ -104,7 +104,7 @@ class SHT3XConfig : public SensorBase{ intv*=1000; } }; -void registerSHT3X(GwApi *api,SensorList &sensors){ +void registerSHT3X(GwApi *api,IICSensorList &sensors){ GwLog *logger=api->getLogger(); #if defined(GWSHT3X) || defined (GWSHT3X11) { @@ -140,7 +140,7 @@ void registerSHT3X(GwApi *api,SensorList &sensors){ #endif } #else -void registerSHT3X(GwApi *api,SensorList &sensors){ +void registerSHT3X(GwApi *api,IICSensorList &sensors){ } diff --git a/lib/iictask/GwSHT3X.h b/lib/iictask/GwSHT3X.h index 085cab7..263d23e 100644 --- a/lib/iictask/GwSHT3X.h +++ b/lib/iictask/GwSHT3X.h @@ -18,5 +18,5 @@ #ifdef _GWSHT3X #include "SHT3X.h" #endif -void registerSHT3X(GwApi *api,SensorList &sensors); +void registerSHT3X(GwApi *api,IICSensorList &sensors); #endif \ No newline at end of file diff --git a/lib/sensors/GwSensor.h b/lib/sensors/GwSensor.h new file mode 100644 index 0000000..4988dd8 --- /dev/null +++ b/lib/sensors/GwSensor.h @@ -0,0 +1,49 @@ +/* + (C) Andreas Vogel andreas@wellenvogel.de + This code is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + This code is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#ifndef _GWSENSORS_H +#define _GWSENSORS_H +#include "GwApi.h" +#include "GwLog.h" +template +class SensorBase{ + public: + using BusType=BUS; + int busId=0; + int iid=99; //N2K instanceId + int addr=-1; + String prefix; + long intv=0; + bool ok=false; + virtual void readConfig(GwConfigHandler *cfg)=0; + SensorBase(GwApi *api,const String &prfx):prefix(prfx){ + } + virtual bool isActive(){return false;}; + virtual bool initDevice(GwApi *api,BUS *wire){return false;}; + virtual bool preinit(GwApi * api){return false;} + virtual void measure(GwApi * api,BUS *wire, int counterId){}; + virtual ~SensorBase(){} +}; + +template +class SensorList : public std::vector*>{ + public: + void add(GwApi *api, SensorBase *sensor){ + sensor->readConfig(api->getConfig()); + api->getLogger()->logDebug(GwLog::LOG,"configured sensor %s, status %d",sensor->prefix.c_str(),(int)sensor->ok); + this->push_back(sensor); + } + using std::vector*>::vector; +}; +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index c862767..0c33a9a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,5 @@ /* + (C) Andreas Vogel andreas@wellenvogel.de This code is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either