make SensorBase more generic
This commit is contained in:
parent
8a5bdd710e
commit
b3d065abd1
|
@ -21,7 +21,7 @@
|
||||||
#define PRFX2 "BME28012"
|
#define PRFX2 "BME28012"
|
||||||
#define PRFX3 "BME28021"
|
#define PRFX3 "BME28021"
|
||||||
#define PRFX4 "BME28022"
|
#define PRFX4 "BME28022"
|
||||||
class BME280Config : public SensorBase{
|
class BME280Config : public IICSensorBase{
|
||||||
public:
|
public:
|
||||||
bool prAct=true;
|
bool prAct=true;
|
||||||
bool tmAct=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)
|
#if defined(GWBME280) || defined(GWBME28011)
|
||||||
{
|
{
|
||||||
BME280Config *cfg=new BME280Config(api,PRFX1);
|
BME280Config *cfg=new BME280Config(api,PRFX1);
|
||||||
|
@ -173,7 +173,7 @@ void registerBME280(GwApi *api,SensorList &sensors){
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
void registerBME280(GwApi *api,SensorList &sensors){
|
void registerBME280(GwApi *api,IICSensorList &sensors){
|
||||||
}
|
}
|
||||||
|
|
||||||
#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,SensorList &sensors);
|
void registerBME280(GwApi *api,IICSensorList &sensors);
|
||||||
#endif
|
#endif
|
|
@ -4,12 +4,16 @@
|
||||||
#include "N2kMessages.h"
|
#include "N2kMessages.h"
|
||||||
#include "GwXdrTypeMappings.h"
|
#include "GwXdrTypeMappings.h"
|
||||||
#include "GwHardware.h"
|
#include "GwHardware.h"
|
||||||
|
#include "GwSensor.h"
|
||||||
#ifdef _GWIIC
|
#ifdef _GWIIC
|
||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
#else
|
#else
|
||||||
class TwoWire;
|
class TwoWire;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
using BusType=TwoWire;
|
||||||
|
using IICSensorList=SensorList<BusType>;
|
||||||
|
using IICSensorBase=SensorBase<BusType>;
|
||||||
#define CFG_GET(name,prefix) \
|
#define CFG_GET(name,prefix) \
|
||||||
cfg->getValue(name, GwConfigDefinitions::prefix ## name)
|
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_IIC1() checkDef(GWIIC_SCL,GWIIC_SDA)
|
||||||
#define CHECK_IIC2() checkDef(GWIIC_SCL2,GWIIC_SDA2)
|
#define CHECK_IIC2() checkDef(GWIIC_SCL2,GWIIC_SDA2)
|
||||||
|
|
|
@ -26,7 +26,7 @@
|
||||||
|
|
||||||
void runIicTask(GwApi *api);
|
void runIicTask(GwApi *api);
|
||||||
|
|
||||||
static SensorList sensors;
|
static IICSensorList sensors;
|
||||||
|
|
||||||
void initIicTask(GwApi *api){
|
void initIicTask(GwApi *api){
|
||||||
GwLog *logger=api->getLogger();
|
GwLog *logger=api->getLogger();
|
||||||
|
@ -124,7 +124,7 @@ void runIicTask(GwApi *api){
|
||||||
GwIntervalRunner timers;
|
GwIntervalRunner timers;
|
||||||
int counterId=api->addCounter("iicsensors");
|
int counterId=api->addCounter("iicsensors");
|
||||||
for (auto it=sensors.begin();it != sensors.end();it++){
|
for (auto it=sensors.begin();it != sensors.end();it++){
|
||||||
SensorBase *cfg=*it;
|
IICSensorBase *cfg=*it;
|
||||||
auto bus=buses.find(cfg->busId);
|
auto bus=buses.find(cfg->busId);
|
||||||
if (! cfg->isActive()) continue;
|
if (! cfg->isActive()) continue;
|
||||||
if (bus == buses.end()){
|
if (bus == buses.end()){
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
#define PRFX2 "QMP698812"
|
#define PRFX2 "QMP698812"
|
||||||
#define PRFX3 "QMP698821"
|
#define PRFX3 "QMP698821"
|
||||||
#define PRFX4 "QMP698822"
|
#define PRFX4 "QMP698822"
|
||||||
class QMP6988Config : public SensorBase{
|
class QMP6988Config : public IICSensorBase{
|
||||||
public:
|
public:
|
||||||
String prNam="Pressure";
|
String prNam="Pressure";
|
||||||
bool prAct=true;
|
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();
|
GwLog *logger=api->getLogger();
|
||||||
#if defined(GWQMP6988) || defined(GWQMP698811)
|
#if defined(GWQMP6988) || defined(GWQMP698811)
|
||||||
{
|
{
|
||||||
|
@ -113,5 +113,5 @@ void registerQMP6988(GwApi *api,SensorList &sensors){
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
void registerQMP6988(GwApi *api,SensorList &sensors){}
|
void registerQMP6988(GwApi *api,IICSensorList &sensors){}
|
||||||
#endif
|
#endif
|
|
@ -18,5 +18,5 @@
|
||||||
#ifdef _GWQMP6988
|
#ifdef _GWQMP6988
|
||||||
#include "QMP6988.h"
|
#include "QMP6988.h"
|
||||||
#endif
|
#endif
|
||||||
void registerQMP6988(GwApi *api,SensorList &sensors);
|
void registerQMP6988(GwApi *api,IICSensorList &sensors);
|
||||||
#endif
|
#endif
|
|
@ -5,7 +5,7 @@
|
||||||
#define PRFX2 "SHT3X12"
|
#define PRFX2 "SHT3X12"
|
||||||
#define PRFX3 "SHT3X21"
|
#define PRFX3 "SHT3X21"
|
||||||
#define PRFX4 "SHT3X22"
|
#define PRFX4 "SHT3X22"
|
||||||
class SHT3XConfig : public SensorBase{
|
class SHT3XConfig : public IICSensorBase{
|
||||||
public:
|
public:
|
||||||
String tmNam;
|
String tmNam;
|
||||||
String huNam;
|
String huNam;
|
||||||
|
@ -104,7 +104,7 @@ class SHT3XConfig : public SensorBase{
|
||||||
intv*=1000;
|
intv*=1000;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
void registerSHT3X(GwApi *api,SensorList &sensors){
|
void registerSHT3X(GwApi *api,IICSensorList &sensors){
|
||||||
GwLog *logger=api->getLogger();
|
GwLog *logger=api->getLogger();
|
||||||
#if defined(GWSHT3X) || defined (GWSHT3X11)
|
#if defined(GWSHT3X) || defined (GWSHT3X11)
|
||||||
{
|
{
|
||||||
|
@ -140,7 +140,7 @@ void registerSHT3X(GwApi *api,SensorList &sensors){
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
void registerSHT3X(GwApi *api,SensorList &sensors){
|
void registerSHT3X(GwApi *api,IICSensorList &sensors){
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,5 +18,5 @@
|
||||||
#ifdef _GWSHT3X
|
#ifdef _GWSHT3X
|
||||||
#include "SHT3X.h"
|
#include "SHT3X.h"
|
||||||
#endif
|
#endif
|
||||||
void registerSHT3X(GwApi *api,SensorList &sensors);
|
void registerSHT3X(GwApi *api,IICSensorList &sensors);
|
||||||
#endif
|
#endif
|
|
@ -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
|
|
@ -1,4 +1,5 @@
|
||||||
/*
|
/*
|
||||||
|
(C) Andreas Vogel andreas@wellenvogel.de
|
||||||
This code is free software; you can redistribute it and/or
|
This code is free software; you can redistribute it and/or
|
||||||
modify it under the terms of the GNU Lesser General Public
|
modify it under the terms of the GNU Lesser General Public
|
||||||
License as published by the Free Software Foundation; either
|
License as published by the Free Software Foundation; either
|
||||||
|
|
Loading…
Reference in New Issue