From 899aa3fe4ad02dcf0ac6a6e925ce0b4ff2e3146d Mon Sep 17 00:00:00 2001 From: andreas Date: Fri, 3 Nov 2023 18:19:16 +0100 Subject: [PATCH] move BME280 to separate file --- lib/iictask/GwBME280.cpp | 200 ++++++++++++++++++++++ lib/iictask/GwBME280.h | 5 + lib/iictask/GwIicSensors.h | 140 ++++++++++++++++ lib/iictask/GwIicTask.cpp | 332 +------------------------------------ 4 files changed, 347 insertions(+), 330 deletions(-) create mode 100644 lib/iictask/GwBME280.cpp create mode 100644 lib/iictask/GwBME280.h create mode 100644 lib/iictask/GwIicSensors.h diff --git a/lib/iictask/GwBME280.cpp b/lib/iictask/GwBME280.cpp new file mode 100644 index 0000000..ca8121b --- /dev/null +++ b/lib/iictask/GwBME280.cpp @@ -0,0 +1,200 @@ +#include "GwBME280.h" +#ifdef _GWIIC + #if defined(GWBME280) || defined(GWBME2801) || defined(GWBME2802)|| defined(GWBME2803)|| defined(GWBME2804) + #define _GWBME280 + #else + #undef _GWBME280 + #endif +#else + #undef _GWBME280 + #undef GWBME280 + #undef GWBME2801 + #undef GWBME2802 + #undef GWBME2803 + #undef GWBME2804 +#endif +#ifdef _GWBME280 + #include +#endif +#ifdef _GWBME280 +class BME280Config : public SensorBase{ + public: + bool prAct=true; + bool tmAct=true; + bool huAct=true; + tN2kTempSource tmSrc=tN2kTempSource::N2kts_InsideTemperature; + tN2kHumiditySource huSrc=tN2kHumiditySource::N2khs_InsideHumidity; + tN2kPressureSource prSrc=tN2kPressureSource::N2kps_Atmospheric; + String tmNam="Temperature"; + String huNam="Humidity"; + String prNam="Pressure"; + float tmOff=0; + float prOff=0; + Adafruit_BME280 *device=nullptr; + uint32_t sensorId=-1; + BME280Config(GwApi * api, const String &prfx):SensorBase(api,prfx){ + } + virtual bool isActive(){return prAct||huAct||tmAct;} + virtual bool initDevice(GwApi *api,TwoWire *wire){ + GwLog *logger=api->getLogger(); + device= new Adafruit_BME280(); + if (! device->begin(addr,wire)){ + LOG_DEBUG(GwLog::ERROR,"unable to initialize %s at %d",prefix.c_str(),addr); + delete device; + device=nullptr; + return false; + } + if (tmOff != 0){ + device->setTemperatureCompensation(tmOff); + } + sensorId=device->sensorID(); + LOG_DEBUG(GwLog::LOG, "initialized %s at %d, sensorId 0x%x", prefix.c_str(), addr, sensorId); + return (huAct && sensorId == 0x60) || tmAct || prAct; + } + virtual bool preinit(GwApi * api){ + GwLog *logger=api->getLogger(); + LOG_DEBUG(GwLog::LOG,"%s configured",prefix.c_str()); + api->addCapability(prefix,"true"); + addPressureXdr(api,*this); + addTempXdr(api,*this); + addHumidXdr(api,*this); + return isActive(); + } + virtual void measure(GwApi *api, TwoWire *wire, int counterId) + { + if (!device) + return; + GwLog *logger = api->getLogger(); + if (prAct) + { + float pressure = device->readPressure(); + float computed = pressure + prOff; + LOG_DEBUG(GwLog::DEBUG, "%s measure %2.0fPa, computed %2.0fPa", prefix.c_str(), pressure, computed); + sendN2kPressure(api, *this, computed, counterId); + } + if (tmAct) + { + float temperature = device->readTemperature(); // offset is handled internally + temperature = CToKelvin(temperature); + LOG_DEBUG(GwLog::DEBUG, "%s measure temp=%2.1f", prefix.c_str(), temperature); + sendN2kTemperature(api, *this, temperature, counterId); + } + if (huAct && sensorId == 0x60) + { + float humidity = device->readHumidity(); + LOG_DEBUG(GwLog::DEBUG, "%s read humid=%02.0f", prefix.c_str(), humidity); + sendN2kHumidity(api, *this, humidity, counterId); + } + } + virtual void readConfig(GwConfigHandler *cfg) override + { + if (prefix == "BME2801") + { + busId = 1; + addr = 0x76; + #undef CG + #define CG(name) CFG_GET(name, BME2801) + CG(prAct); + CG(tmAct); + CG(huAct); + CG(tmSrc); + CG(huSrc); + CG(iid); + CG(intv); + CG(tmNam); + CG(huNam); + CG(prNam); + CG(tmOff); + CG(prOff); + ok=true; + } + if (prefix == "BME2802") + { + busId = 1; + addr = 0x77; + #undef CG + #define CG(name) CFG_GET(name, BME2802) + CG(prAct); + CG(tmAct); + CG(huAct); + CG(tmSrc); + CG(huSrc); + CG(iid); + CG(intv); + CG(tmNam); + CG(huNam); + CG(prNam); + CG(tmOff); + CG(prOff); + ok=true; + } + if (prefix == "BME2803") + { + busId = 2; + addr = 0x76; + #undef CG + #define CG(name) CFG_GET(name, BME2803) + CG(prAct); + CG(tmAct); + CG(huAct); + CG(tmSrc); + CG(huSrc); + CG(iid); + CG(intv); + CG(tmNam); + CG(huNam); + CG(prNam); + CG(tmOff); + CG(prOff); + ok=true; + } + if (prefix == "BME2804") + { + busId = 1; + addr = 0x77; + #undef CG + #define CG(name) CFG_GET(name, BME2804) + CG(prAct); + CG(tmAct); + CG(huAct); + CG(tmSrc); + CG(huSrc); + CG(iid); + CG(intv); + CG(tmNam); + CG(huNam); + CG(prNam); + CG(tmOff); + CG(prOff); + ok=true; + } + intv *= 1000; + } +}; + +void registerBME280(GwApi *api,SensorList &sensors){ + GwLog *logger=api->getLogger(); + #if defined(GWBME280) || defined(GWBME2801) + BME280Config *cfg=new BME280Config(api,"BME2801"); + sensors.add(api,cfg); + LOG_DEBUG(GwLog::LOG,"%s configured %d",cfg->prefix.c_str(),(int)cfg->ok); + #endif + #if defined(GWBME2802) + BME280Config *cfg=new BME280Config(api,"BME2802"); + sensors.add(api,cfg); + #endif + #if defined(GWBME2803) + BME280Config *cfg=new BME280Config(api,"BME2803"); + sensors.add(api,cfg); + #endif + #if defined(GWBME2804) + BME280Config *cfg=new BME280Config(api,"BME2804"); + sensors.add(api,cfg); + #endif +} +#else +void registerBME280(GwApi *api,SensorList &sensors){ +} + +#endif + diff --git a/lib/iictask/GwBME280.h b/lib/iictask/GwBME280.h new file mode 100644 index 0000000..03bfe57 --- /dev/null +++ b/lib/iictask/GwBME280.h @@ -0,0 +1,5 @@ +#ifndef _GWBME280_H +#define _GWBME280_H +#include "GwIicSensors.h" +void registerBME280(GwApi *api,SensorList &sensors); +#endif \ No newline at end of file diff --git a/lib/iictask/GwIicSensors.h b/lib/iictask/GwIicSensors.h new file mode 100644 index 0000000..ad1007b --- /dev/null +++ b/lib/iictask/GwIicSensors.h @@ -0,0 +1,140 @@ +#ifndef _GWIICSENSSORS_H +#define _GWIICSENSSORS_H +#include "GwApi.h" +#include "N2kMessages.h" +#include "GwXdrTypeMappings.h" +#include "GwHardware.h" +#ifdef _GWIIC + #include +#endif +#ifndef GWIIC_SDA + #define GWIIC_SDA -1 +#endif +#ifndef GWIIC_SCL + #define GWIIC_SCL -1 +#endif +#ifndef GWIIC_SDA2 + #define GWIIC_SDA2 -1 +#endif +#ifndef GWIIC_SCL2 + #define GWIIC_SCL2 -1 +#endif + +#define CFG_GET(name,prefix) \ + cfg->getValue(name, GwConfigDefinitions::prefix ## name) + +template +bool addPressureXdr(GwApi *api, CFG &cfg) +{ + if (! cfg.prAct) return false; + if (cfg.prNam.isEmpty()){ + api->getLogger()->logDebug(GwLog::LOG, "pressure active for %s, no xdr mapping", cfg.prefix.c_str()); + return true; + } + api->getLogger()->logDebug(GwLog::LOG, "adding pressure xdr mapping for %s", cfg.prefix.c_str()); + GwXDRMappingDef xdr; + xdr.category = GwXDRCategory::XDRPRESSURE; + xdr.direction = GwXDRMappingDef::M_FROM2K; + xdr.selector = (int)cfg.prSrc; + xdr.instanceId = cfg.iid; + xdr.instanceMode = GwXDRMappingDef::IS_SINGLE; + xdr.xdrName = cfg.prNam; + api->addXdrMapping(xdr); + return true; +} + +template +bool addTempXdr(GwApi *api, CFG &cfg) +{ + if (!cfg.tmAct) return false; + if (cfg.tmNam.isEmpty()){ + api->getLogger()->logDebug(GwLog::LOG, "temperature active for %s, no xdr mapping", cfg.prefix.c_str()); + return true; + } + api->getLogger()->logDebug(GwLog::LOG, "adding temperature xdr mapping for %s", cfg.prefix.c_str()); + GwXDRMappingDef xdr; + xdr.category = GwXDRCategory::XDRTEMP; + xdr.direction = GwXDRMappingDef::M_FROM2K; + xdr.field = GWXDRFIELD_TEMPERATURE_ACTUALTEMPERATURE; + xdr.selector = (int)cfg.tmSrc; + xdr.instanceMode = GwXDRMappingDef::IS_SINGLE; + xdr.instanceId = cfg.iid; + xdr.xdrName = cfg.tmNam; + api->addXdrMapping(xdr); + return true; +} + +template +bool addHumidXdr(GwApi *api, CFG &cfg) +{ + if (! cfg.huAct) return false; + if (cfg.huNam.isEmpty()){ + api->getLogger()->logDebug(GwLog::LOG, "humidity active for %s, no xdr mapping", cfg.prefix.c_str()); + return true; + } + api->getLogger()->logDebug(GwLog::LOG, "adding humidity xdr mapping for %s", cfg.prefix.c_str()); + GwXDRMappingDef xdr; + xdr.category = GwXDRCategory::XDRHUMIDITY; + xdr.direction = GwXDRMappingDef::M_FROM2K; + xdr.field = GWXDRFIELD_HUMIDITY_ACTUALHUMIDITY; + xdr.selector = (int)cfg.huSrc; + xdr.instanceMode = GwXDRMappingDef::IS_SINGLE; + xdr.instanceId = cfg.iid; + xdr.xdrName = cfg.huNam; + api->addXdrMapping(xdr); + return true; +} + +template +void sendN2kHumidity(GwApi *api,CFG &cfg,double value, int counterId){ + tN2kMsg msg; + SetN2kHumidity(msg,1,cfg.iid,cfg.huSrc,value); + api->sendN2kMessage(msg); + api->increment(counterId,cfg.prefix+String("hum")); +} + +template +void sendN2kPressure(GwApi *api,CFG &cfg,double value, int counterId){ + tN2kMsg msg; + SetN2kPressure(msg,1,cfg.iid,cfg.prSrc,value); + api->sendN2kMessage(msg); + api->increment(counterId,cfg.prefix+String("press")); +} + +template +void sendN2kTemperature(GwApi *api,CFG &cfg,double value, int counterId){ + tN2kMsg msg; + SetN2kTemperature(msg,1,cfg.iid,cfg.tmSrc,value); + api->sendN2kMessage(msg); + api->increment(counterId,cfg.prefix+String("temp")); +} + + +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()); + push_back(sensor); + } + using std::vector::vector; +}; + +#endif \ No newline at end of file diff --git a/lib/iictask/GwIicTask.cpp b/lib/iictask/GwIicTask.cpp index c320efb..4d544ac 100644 --- a/lib/iictask/GwIicTask.cpp +++ b/lib/iictask/GwIicTask.cpp @@ -1,8 +1,9 @@ #include "GwIicTask.h" +#include "GwIicSensors.h" #include "GwHardware.h" +#include "GwBME280.h" #include #ifdef _GWIIC - #include #if defined(GWSHT3X) || defined(GWSHT3X1) || defined(GWSHT3X2) || defined(GWSHT3X2) || defined(GWSHT3X4) #define _GWSHT3X #else @@ -13,11 +14,6 @@ #else #undef _GWQMP6988 #endif - #if defined(GWBME280) || defined(GWBME2801) || defined(GWBME2802)|| defined(GWBME2803)|| defined(GWBME2804) - #define _GWBME280 - #else - #undef _GWBME280 - #endif #else #undef _GWSHT3X #undef GWSHT3X @@ -31,12 +27,6 @@ #undef GWQMP69882 #undef GWQMP69883 #undef GWQMP69884 - #undef _GWBME280 - #undef GWBME280 - #undef GWBME2801 - #undef GWBME2802 - #undef GWBME2803 - #undef GWBME2804 #endif #ifdef _GWSHT3X #include "SHT3X.h" @@ -45,144 +35,7 @@ #include "QMP6988.h" #endif #include "GwTimer.h" -#include "N2kMessages.h" #include "GwHardware.h" -#include "GwXdrTypeMappings.h" -#ifdef _GWBME280 - #include -#endif - - -template -bool addPressureXdr(GwApi *api, CFG &cfg) -{ - if (! cfg.prAct) return false; - if (cfg.prNam.isEmpty()){ - api->getLogger()->logDebug(GwLog::LOG, "pressure active for %s, no xdr mapping", cfg.prefix.c_str()); - return true; - } - api->getLogger()->logDebug(GwLog::LOG, "adding pressure xdr mapping for %s", cfg.prefix.c_str()); - GwXDRMappingDef xdr; - xdr.category = GwXDRCategory::XDRPRESSURE; - xdr.direction = GwXDRMappingDef::M_FROM2K; - xdr.selector = (int)cfg.prSrc; - xdr.instanceId = cfg.iid; - xdr.instanceMode = GwXDRMappingDef::IS_SINGLE; - xdr.xdrName = cfg.prNam; - api->addXdrMapping(xdr); - return true; -} - -template -bool addTempXdr(GwApi *api, CFG &cfg) -{ - if (!cfg.tmAct) return false; - if (cfg.tmNam.isEmpty()){ - api->getLogger()->logDebug(GwLog::LOG, "temperature active for %s, no xdr mapping", cfg.prefix.c_str()); - return true; - } - api->getLogger()->logDebug(GwLog::LOG, "adding temperature xdr mapping for %s", cfg.prefix.c_str()); - GwXDRMappingDef xdr; - xdr.category = GwXDRCategory::XDRTEMP; - xdr.direction = GwXDRMappingDef::M_FROM2K; - xdr.field = GWXDRFIELD_TEMPERATURE_ACTUALTEMPERATURE; - xdr.selector = (int)cfg.tmSrc; - xdr.instanceMode = GwXDRMappingDef::IS_SINGLE; - xdr.instanceId = cfg.iid; - xdr.xdrName = cfg.tmNam; - api->addXdrMapping(xdr); - return true; -} - -template -bool addHumidXdr(GwApi *api, CFG &cfg) -{ - if (! cfg.huAct) return false; - if (cfg.huNam.isEmpty()){ - api->getLogger()->logDebug(GwLog::LOG, "humidity active for %s, no xdr mapping", cfg.prefix.c_str()); - return true; - } - api->getLogger()->logDebug(GwLog::LOG, "adding humidity xdr mapping for %s", cfg.prefix.c_str()); - GwXDRMappingDef xdr; - xdr.category = GwXDRCategory::XDRHUMIDITY; - xdr.direction = GwXDRMappingDef::M_FROM2K; - xdr.field = GWXDRFIELD_HUMIDITY_ACTUALHUMIDITY; - xdr.selector = (int)cfg.huSrc; - xdr.instanceMode = GwXDRMappingDef::IS_SINGLE; - xdr.instanceId = cfg.iid; - xdr.xdrName = cfg.huNam; - api->addXdrMapping(xdr); - return true; -} - -template -void sendN2kHumidity(GwApi *api,CFG &cfg,double value, int counterId){ - tN2kMsg msg; - SetN2kHumidity(msg,1,cfg.iid,cfg.huSrc,value); - api->sendN2kMessage(msg); - api->increment(counterId,cfg.prefix+String("hum")); -} - -template -void sendN2kPressure(GwApi *api,CFG &cfg,double value, int counterId){ - tN2kMsg msg; - SetN2kPressure(msg,1,cfg.iid,cfg.prSrc,value); - api->sendN2kMessage(msg); - api->increment(counterId,cfg.prefix+String("press")); -} - -template -void sendN2kTemperature(GwApi *api,CFG &cfg,double value, int counterId){ - tN2kMsg msg; - SetN2kTemperature(msg,1,cfg.iid,cfg.tmSrc,value); - api->sendN2kMessage(msg); - api->increment(counterId,cfg.prefix+String("temp")); -} - - -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()); - push_back(sensor); - } - using std::vector::vector; -}; - -#ifndef GWIIC_SDA - #define GWIIC_SDA -1 -#endif -#ifndef GWIIC_SCL - #define GWIIC_SCL -1 -#endif -#ifndef GWIIC_SDA2 - #define GWIIC_SDA2 -1 -#endif -#ifndef GWIIC_SCL2 - #define GWIIC_SCL2 -1 -#endif - -#define CFG_GET(name,prefix) \ - cfg->getValue(name, GwConfigDefinitions::prefix ## name) - #ifdef _GWSHT3X class SHT3XConfig : public SensorBase{ @@ -401,187 +254,6 @@ class QMP6988Config : public SensorBase{ }; #endif -#ifdef _GWBME280 -class BME280Config : public SensorBase{ - public: - bool prAct=true; - bool tmAct=true; - bool huAct=true; - tN2kTempSource tmSrc=tN2kTempSource::N2kts_InsideTemperature; - tN2kHumiditySource huSrc=tN2kHumiditySource::N2khs_InsideHumidity; - tN2kPressureSource prSrc=tN2kPressureSource::N2kps_Atmospheric; - String tmNam="Temperature"; - String huNam="Humidity"; - String prNam="Pressure"; - float tmOff=0; - float prOff=0; - Adafruit_BME280 *device=nullptr; - uint32_t sensorId=-1; - BME280Config(GwApi * api, const String &prfx):SensorBase(api,prfx){ - } - virtual bool isActive(){return prAct||huAct||tmAct;} - virtual bool initDevice(GwApi *api,TwoWire *wire){ - GwLog *logger=api->getLogger(); - device= new Adafruit_BME280(); - if (! device->begin(addr,wire)){ - LOG_DEBUG(GwLog::ERROR,"unable to initialize %s at %d",prefix.c_str(),addr); - delete device; - device=nullptr; - return false; - } - if (tmOff != 0){ - device->setTemperatureCompensation(tmOff); - } - sensorId=device->sensorID(); - LOG_DEBUG(GwLog::LOG, "initialized %s at %d, sensorId 0x%x", prefix.c_str(), addr, sensorId); - return (huAct && sensorId == 0x60) || tmAct || prAct; - } - virtual bool preinit(GwApi * api){ - GwLog *logger=api->getLogger(); - LOG_DEBUG(GwLog::LOG,"%s configured",prefix.c_str()); - api->addCapability(prefix,"true"); - addPressureXdr(api,*this); - addTempXdr(api,*this); - addHumidXdr(api,*this); - return isActive(); - } - virtual void measure(GwApi *api, TwoWire *wire, int counterId) - { - if (!device) - return; - GwLog *logger = api->getLogger(); - if (prAct) - { - float pressure = device->readPressure(); - float computed = pressure + prOff; - LOG_DEBUG(GwLog::DEBUG, "%s measure %2.0fPa, computed %2.0fPa", prefix.c_str(), pressure, computed); - sendN2kPressure(api, *this, computed, counterId); - } - if (tmAct) - { - float temperature = device->readTemperature(); // offset is handled internally - temperature = CToKelvin(temperature); - LOG_DEBUG(GwLog::DEBUG, "%s measure temp=%2.1f", prefix.c_str(), temperature); - sendN2kTemperature(api, *this, temperature, counterId); - } - if (huAct && sensorId == 0x60) - { - float humidity = device->readHumidity(); - LOG_DEBUG(GwLog::DEBUG, "%s read humid=%02.0f", prefix.c_str(), humidity); - sendN2kHumidity(api, *this, humidity, counterId); - } - } - virtual void readConfig(GwConfigHandler *cfg) override - { - if (prefix == "BME2801") - { - busId = 1; - addr = 0x76; - #undef CG - #define CG(name) CFG_GET(name, BME2801) - CG(prAct); - CG(tmAct); - CG(huAct); - CG(tmSrc); - CG(huSrc); - CG(iid); - CG(intv); - CG(tmNam); - CG(huNam); - CG(prNam); - CG(tmOff); - CG(prOff); - ok=true; - } - if (prefix == "BME2802") - { - busId = 1; - addr = 0x77; - #undef CG - #define CG(name) CFG_GET(name, BME2802) - CG(prAct); - CG(tmAct); - CG(huAct); - CG(tmSrc); - CG(huSrc); - CG(iid); - CG(intv); - CG(tmNam); - CG(huNam); - CG(prNam); - CG(tmOff); - CG(prOff); - ok=true; - } - if (prefix == "BME2803") - { - busId = 2; - addr = 0x76; - #undef CG - #define CG(name) CFG_GET(name, BME2803) - CG(prAct); - CG(tmAct); - CG(huAct); - CG(tmSrc); - CG(huSrc); - CG(iid); - CG(intv); - CG(tmNam); - CG(huNam); - CG(prNam); - CG(tmOff); - CG(prOff); - ok=true; - } - if (prefix == "BME2804") - { - busId = 1; - addr = 0x77; - #undef CG - #define CG(name) CFG_GET(name, BME2804) - CG(prAct); - CG(tmAct); - CG(huAct); - CG(tmSrc); - CG(huSrc); - CG(iid); - CG(intv); - CG(tmNam); - CG(huNam); - CG(prNam); - CG(tmOff); - CG(prOff); - ok=true; - } - intv *= 1000; - } -}; - -void registerBME280(GwApi *api,SensorList &sensors){ - GwLog *logger=api->getLogger(); - #if defined(GWBME280) || defined(GWBME2801) - BME280Config *cfg=new BME280Config(api,"BME2801"); - sensors.add(api,cfg); - LOG_DEBUG(GwLog::LOG,"%s configured %d",cfg->prefix.c_str(),(int)cfg->ok); - #endif - #if defined(GWBME2802) - BME280Config *cfg=new BME280Config(api,"BME2802"); - sensors.add(api,cfg); - #endif - #if defined(GWBME2803) - BME280Config *cfg=new BME280Config(api,"BME2803"); - sensors.add(api,cfg); - #endif - #if defined(GWBME2804) - BME280Config *cfg=new BME280Config(api,"BME2804"); - sensors.add(api,cfg); - #endif -} -#else -void registerBME280(GwApi *api,SensorList &sensors){ -} - -#endif void runIicTask(GwApi *api);