diff --git a/lib/iictask/GwBMP280.cpp b/lib/iictask/GwBMP280.cpp index c1b1729..c295cd9 100644 --- a/lib/iictask/GwBMP280.cpp +++ b/lib/iictask/GwBMP280.cpp @@ -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(); diff --git a/lib/iictask/GwIicSensors.h b/lib/iictask/GwIicSensors.h index bade5d0..4937daa 100644 --- a/lib/iictask/GwIicSensors.h +++ b/lib/iictask/GwIicSensors.h @@ -11,9 +11,9 @@ class TwoWire; #endif -using BusType=TwoWire; -using IICSensorList=SensorList; -using IICSensorBase=SensorBase; +using BUSTYPE=TwoWire; +using IICSensorList=SensorList; +using IICSensorBase=SensorTemplate; template diff --git a/lib/iictask/GwIicTask.cpp b/lib/iictask/GwIicTask.cpp index 22a7167..81dc0cc 100644 --- a/lib/iictask/GwIicTask.cpp +++ b/lib/iictask/GwIicTask.cpp @@ -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()){ diff --git a/lib/sensors/GwSensor.h b/lib/sensors/GwSensor.h index da9b560..1f85d3b 100644 --- a/lib/sensors/GwSensor.h +++ b/lib/sensors/GwSensor.h @@ -16,9 +16,14 @@ #define _GWSENSORS_H #include "GwApi.h" #include "GwLog.h" -template 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 *(GwApi *api,const String &prfx)>; + using Creator=std::function; 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 -class SensorList : public std::vector*>{ +template +class SensorTemplate : public SensorBase{ public: - void add(GwApi *api, SensorBase *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{ + 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; + using std::vector::vector; }; diff --git a/lib/spitask/GwSpiSensor.h b/lib/spitask/GwSpiSensor.h index 3d516a5..88127c6 100644 --- a/lib/spitask/GwSpiSensor.h +++ b/lib/spitask/GwSpiSensor.h @@ -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{ +class SSISensor : public SensorTemplate{ std::unique_ptr device; protected: int bits=12; @@ -125,17 +125,17 @@ class SSISensor : public SensorBase{ } 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(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; +using SpiSensorList=SensorList; #define GWSPI1_HOST SPI2_HOST #define GWSPI2_HOST SPI3_HOST #endif \ No newline at end of file