restructure SensorBase
This commit is contained in:
parent
70fb1b3633
commit
85e1a0e5f0
|
@ -33,8 +33,8 @@ class BMP280Config : public IICSensorBase{
|
|||
float prOff=0;
|
||||
Adafruit_BMP280 *device=nullptr;
|
||||
uint32_t sensorId=-1;
|
||||
BMP280Config(GwApi * api, const String &prfx):SensorBase(TYPE,api,prfx){
|
||||
}
|
||||
BMP280Config(GwApi * api, const String &prfx):IICSensorBase(TYPE,api,prfx){
|
||||
}
|
||||
virtual bool isActive(){return prAct||tmAct;}
|
||||
virtual bool initDevice(GwApi *api,TwoWire *wire){
|
||||
GwLog *logger=api->getLogger();
|
||||
|
|
|
@ -11,9 +11,9 @@
|
|||
class TwoWire;
|
||||
#endif
|
||||
|
||||
using BusType=TwoWire;
|
||||
using IICSensorList=SensorList<BusType>;
|
||||
using IICSensorBase=SensorBase<BusType>;
|
||||
using BUSTYPE=TwoWire;
|
||||
using IICSensorList=SensorList;
|
||||
using IICSensorBase=SensorTemplate<BUSTYPE,SensorBase::IIC>;
|
||||
|
||||
|
||||
template <class CFG>
|
||||
|
|
|
@ -184,8 +184,8 @@ void runIicTask(GwApi *api){
|
|||
bool runLoop=false;
|
||||
GwIntervalRunner timers;
|
||||
int counterId=api->addCounter("iicsensors");
|
||||
for (auto it=sensors.begin();it != sensors.end();it++){
|
||||
IICSensorBase *cfg=*it;
|
||||
for (auto &&cfg:sensors){
|
||||
if (cfg->busType != SensorBase::IIC) continue;
|
||||
auto bus=buses.find(cfg->busId);
|
||||
if (! cfg->isActive()) continue;
|
||||
if (bus == buses.end()){
|
||||
|
|
|
@ -16,9 +16,14 @@
|
|||
#define _GWSENSORS_H
|
||||
#include "GwApi.h"
|
||||
#include "GwLog.h"
|
||||
template<typename BUS>
|
||||
class SensorBase{
|
||||
public:
|
||||
using BusType=enum{
|
||||
IIC=0,
|
||||
SPI=1,
|
||||
UNKNOWN=-1
|
||||
};
|
||||
BusType busType=BusType::UNKNOWN;
|
||||
int busId=0;
|
||||
int iid=99; //N2K instanceId
|
||||
int addr=-1;
|
||||
|
@ -27,25 +32,41 @@ class SensorBase{
|
|||
long intv=0;
|
||||
bool ok=false;
|
||||
virtual void readConfig(GwConfigHandler *cfg)=0;
|
||||
SensorBase(const String &tn,GwApi *api,const String &prfx):type(tn),prefix(prfx){
|
||||
SensorBase(BusType bt,const String &tn,GwApi *api,const String &prfx)
|
||||
:busType(bt),type(tn),prefix(prfx){
|
||||
}
|
||||
using Creator=std::function<SensorBase<BUS> *(GwApi *api,const String &prfx)>;
|
||||
using Creator=std::function<SensorBase *(GwApi *api,const String &prfx)>;
|
||||
virtual bool isActive(){return false;};
|
||||
virtual bool initDevice(GwApi *api,BUS *wire){return false;};
|
||||
virtual bool initDevice(GwApi *api,void *bus){return false;};
|
||||
virtual bool preinit(GwApi * api){return false;}
|
||||
virtual void measure(GwApi * api,BUS *wire, int counterId){};
|
||||
virtual void measure(GwApi * api,void *bus, int counterId){};
|
||||
virtual ~SensorBase(){}
|
||||
};
|
||||
|
||||
template<typename BUS>
|
||||
class SensorList : public std::vector<SensorBase<BUS>*>{
|
||||
template<typename BUS,SensorBase::BusType bt>
|
||||
class SensorTemplate : public SensorBase{
|
||||
public:
|
||||
void add(GwApi *api, SensorBase<BUS> *sensor){
|
||||
SensorTemplate(const String &tn,GwApi *api,const String &prfx)
|
||||
:SensorBase(bt,tn,api,prfx){}
|
||||
virtual bool initDevice(GwApi *api,BUS *bus){return false;};
|
||||
virtual bool initDevice(GwApi *api,void *bus){
|
||||
if (busType != bt) return false;
|
||||
return initDevice(api,(BUS*)bus);
|
||||
}
|
||||
virtual void measure(GwApi * api,void *bus, int counterId){
|
||||
if (busType != bt) return;
|
||||
measure(api,(BUS*)bus,counterId);
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
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);
|
||||
this->push_back(sensor);
|
||||
}
|
||||
using std::vector<SensorBase<BUS>*>::vector;
|
||||
using std::vector<SensorBase*>::vector;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -48,7 +48,7 @@ class SPIBus{
|
|||
spi_host_device_t host() const { return hd;}
|
||||
};
|
||||
|
||||
using BusType=SPIBus;
|
||||
using BUSTYPE=SPIBus;
|
||||
|
||||
class SSIDevice{
|
||||
spi_device_handle_t spi;
|
||||
|
@ -90,7 +90,7 @@ class SSIDevice{
|
|||
};
|
||||
|
||||
|
||||
class SSISensor : public SensorBase<BusType>{
|
||||
class SSISensor : public SensorTemplate<BUSTYPE,SensorBase::SPI>{
|
||||
std::unique_ptr<SSIDevice> device;
|
||||
protected:
|
||||
int bits=12;
|
||||
|
@ -125,17 +125,17 @@ class SSISensor : public SensorBase<BusType>{
|
|||
}
|
||||
|
||||
public:
|
||||
SSISensor(const String &type,GwApi *api,const String &prfx, int host):SensorBase(type,api,prfx)
|
||||
SSISensor(const String &type,GwApi *api,const String &prfx, int host):SensorTemplate<BUSTYPE,SensorBase::SPI>(type,api,prfx)
|
||||
{
|
||||
busId=host;
|
||||
}
|
||||
virtual bool isActive(){return act;};
|
||||
virtual bool initDevice(GwApi *api,BusType *bus){
|
||||
virtual bool initDevice(GwApi *api,BUSTYPE *bus){
|
||||
return initSSI(api->getLogger(),bus, clock,cs,bits);
|
||||
};
|
||||
|
||||
};
|
||||
using SpiSensorList=SensorList<BusType>;
|
||||
using SpiSensorList=SensorList;
|
||||
#define GWSPI1_HOST SPI2_HOST
|
||||
#define GWSPI2_HOST SPI3_HOST
|
||||
#endif
|
Loading…
Reference in New Issue