make SensorBase more generic

This commit is contained in:
andreas 2024-03-01 15:13:11 +01:00
parent 8a5bdd710e
commit b3d065abd1
10 changed files with 68 additions and 41 deletions

View File

@ -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

View File

@ -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

View File

@ -4,12 +4,16 @@
#include "N2kMessages.h"
#include "GwXdrTypeMappings.h"
#include "GwHardware.h"
#include "GwSensor.h"
#ifdef _GWIIC
#include <Wire.h>
#else
class TwoWire;
#endif
using BusType=TwoWire;
using IICSensorList=SensorList<BusType>;
using IICSensorBase=SensorBase<BusType>;
#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<SensorBase*>{
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<SensorBase*>::vector;
};
#define CHECK_IIC1() checkDef(GWIIC_SCL,GWIIC_SDA)
#define CHECK_IIC2() checkDef(GWIIC_SCL2,GWIIC_SDA2)

View File

@ -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()){

View File

@ -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

View File

@ -18,5 +18,5 @@
#ifdef _GWQMP6988
#include "QMP6988.h"
#endif
void registerQMP6988(GwApi *api,SensorList &sensors);
void registerQMP6988(GwApi *api,IICSensorList &sensors);
#endif

View File

@ -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){
}

View File

@ -18,5 +18,5 @@
#ifdef _GWSHT3X
#include "SHT3X.h"
#endif
void registerSHT3X(GwApi *api,SensorList &sensors);
void registerSHT3X(GwApi *api,IICSensorList &sensors);
#endif

49
lib/sensors/GwSensor.h Normal file
View File

@ -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<typename BUS>
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<typename BUS>
class SensorList : public std::vector<SensorBase<BUS>*>{
public:
void add(GwApi *api, SensorBase<BUS> *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<SensorBase<BUS>*>::vector;
};
#endif

View File

@ -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