From d904d15ecea745367ff8b048b5ca4bdc8690318e Mon Sep 17 00:00:00 2001 From: andreas Date: Sun, 17 Nov 2024 14:41:28 +0100 Subject: [PATCH] intermediate: sensor config initializer list --- lib/iictask/GwBMP280.cpp | 110 ++++++++++++++++++++------------------ lib/iictask/GwIicTask.cpp | 1 + lib/sensors/GwSensor.h | 57 ++++++++++++++++++++ 3 files changed, 116 insertions(+), 52 deletions(-) diff --git a/lib/iictask/GwBMP280.cpp b/lib/iictask/GwBMP280.cpp index 08c1517..a6108bc 100644 --- a/lib/iictask/GwBMP280.cpp +++ b/lib/iictask/GwBMP280.cpp @@ -15,11 +15,16 @@ #include #endif #ifdef _GWBMP280 -#define TYPE "BMP280" -#define PRFX1 TYPE "11" -#define PRFX2 TYPE "12" -#define PRFX3 TYPE "21" -#define PRFX4 TYPE "22" + + +/** + * we need a forward declaration here as the config list has to go before the + * class implementation + */ + +class BMP280Config; +GwSensorConfigInitializerList configs; + class BMP280Config : public IICSensorBase{ public: bool prAct=true; @@ -33,14 +38,14 @@ class BMP280Config : public IICSensorBase{ float prOff=0; Adafruit_BMP280 *device=nullptr; uint32_t sensorId=-1; - BMP280Config(GwApi * api, const String &prfx):IICSensorBase(TYPE,api,prfx){ + BMP280Config(GwApi * api, const String &prfx):IICSensorBase("BMP280",api,prfx){ } virtual bool isActive(){return prAct||tmAct;} virtual bool initDevice(GwApi *api,TwoWire *wire){ GwLog *logger=api->getLogger(); device= new Adafruit_BMP280(wire); if (! device->begin(addr)){ - LOG_DEBUG(GwLog::ERROR,"unable to initialize %s at %d",prefix.c_str(),addr); + LOG_DEBUG(GwLog::ERROR,"unable to initialize %s at 0x%x",prefix.c_str(),addr); delete device; device=nullptr; return false; @@ -85,85 +90,86 @@ class BMP280Config : public IICSensorBase{ sendN2kEnvironmentalParameters(api, *this, temperature, humidity, computed,counterId); } } - #define CFGBMP280(prefix) \ - CFG_GET(prAct,prefix); \ - CFG_GET(tmAct,prefix);\ - CFG_GET(tmSrc,prefix);\ - CFG_GET(iid,prefix);\ - CFG_GET(intv,prefix);\ - CFG_GET(tmNam,prefix);\ - CFG_GET(prNam,prefix);\ - CFG_GET(tmOff,prefix);\ - CFG_GET(prOff,prefix); - + virtual void readConfig(GwConfigHandler *cfg) override { - if (prefix == PRFX1) - { - busId = 1; - addr = 0x76; - CFGBMP280(BMP28011); - ok=true; - } - if (prefix == PRFX2) - { - busId = 1; - addr = 0x77; - CFGBMP280(BMP28012); - ok=true; - } - if (prefix == PRFX3) - { - busId = 2; - addr = 0x76; - CFGBMP280(BMP28021); - ok=true; - } - if (prefix == PRFX4) - { - busId = 2; - addr = 0x77; - CFGBMP280(BMP28022); - ok=true; - } - intv *= 1000; + configs.readConfig(this,cfg); } }; -static IICSensorBase::Creator creator([](GwApi *api, const String &prfx){ + +static IICSensorBase::Creator creator([](GwApi *api, const String &prfx)->BMP280Config*{ + if (! configs.knowsPrefix(prfx)){ + return nullptr; + } return new BMP280Config(api,prfx); }); IICSensorBase::Creator registerBMP280(GwApi *api){ #if defined(GWBMP280) || defined(GWBMP28011) { - api->addSensor(creator(api,PRFX1)); + api->addSensor(creator(api,"BMP28011")); CHECK_IIC1(); #pragma message "GWBMP28011 defined" } #endif #if defined(GWBMP28012) { - api->addSensor(creator(api,PRFX2)); + api->addSensor(creator(api,"BMP28012")); CHECK_IIC1(); #pragma message "GWBMP28012 defined" } #endif #if defined(GWBMP28021) { - api->addSensor(creator(api,PRFX3)); + api->addSensor(creator(api,"BMP28021")); CHECK_IIC2(); #pragma message "GWBMP28021 defined" } #endif #if defined(GWBMP28022) { - api->addSensor(creator(api,PRFX4)); + api->addSensor(creator(api,"BMP28022")); CHECK_IIC1(); #pragma message "GWBMP28022 defined" } #endif return creator; } + +/** + * a define for the readConfig function + * we use a define here as we want to be able to check the config + * definitions at compile time +*/ +#define CFGBMP280P(s, prefix, bus, baddr) \ + CFG_SGET(s, prAct, prefix); \ + CFG_SGET(s, tmAct, prefix); \ + CFG_SGET(s, tmSrc, prefix); \ + CFG_SGET(s, iid, prefix); \ + CFG_SGET(s, intv, prefix); \ + CFG_SGET(s, tmNam, prefix); \ + CFG_SGET(s, prNam, prefix); \ + CFG_SGET(s, tmOff, prefix); \ + CFG_SGET(s, prOff, prefix); \ + s->busId = bus; \ + s->addr = baddr; \ + s->ok = true; \ + s->intv*=1000; + +/** + * a config initializer for our sensor +*/ +#define SCBMP280(list, prefix, bus, addr) \ + GWSENSORCONFIG(list, BMP280Config, prefix, [](BMP280Config *s, GwConfigHandler *cfg) { CFGBMP280P(s, prefix, bus, addr); }); + +/** + * four possible sensor configs +*/ +SCBMP280(configs, BMP28011, 1, 0x76); +SCBMP280(configs, BMP28012, 1, 0x77); +SCBMP280(configs, BMP28021, 2, 0x76); +SCBMP280(configs, BMP28022, 2, 0x77); + #else IICSensorBase::Creator registerBMP280(GwApi *api){ return IICSensorBase::Creator(); diff --git a/lib/iictask/GwIicTask.cpp b/lib/iictask/GwIicTask.cpp index 949d18e..e815054 100644 --- a/lib/iictask/GwIicTask.cpp +++ b/lib/iictask/GwIicTask.cpp @@ -60,6 +60,7 @@ static void addGroveItems(std::vector &creators,GwApi *a { if (! creator) continue; auto *scfg = creator(api, prfx); + if (scfg == nullptr) continue; scfg->readConfig(api->getConfig()); if (scfg->ok) { diff --git a/lib/sensors/GwSensor.h b/lib/sensors/GwSensor.h index b133897..e1617ea 100644 --- a/lib/sensors/GwSensor.h +++ b/lib/sensors/GwSensor.h @@ -14,6 +14,7 @@ */ #ifndef _GWSENSORS_H #define _GWSENSORS_H +#include "GwConfigItem.h" #include #include #include @@ -69,6 +70,62 @@ class SensorList : public std::vector{ using std::vector::vector; }; +/** + * helper classes for a simplified sensor configuration + * by creating a list of type GwSensorConfigInitializerList we can populate + * it by static instances of GwSensorConfigInitializer (using GWSENSORCONFIG ) that each has + * a function that populates the sensor config from the config data. + * For using sensors this is not really necessary - but it can simplify the code for a sensor + * if you want to support a couple of instances on different buses. + * By using this approach you still can write functions using the CFG_SGET macros that will check + * your config definitions at compile time. + * +*/ +template +class GwSensorConfig{ + public: + using ReadConfig=std::function; + ReadConfig configReader; + String prefix; + GwSensorConfig(const String &prfx,ReadConfig f):prefix(prfx),configReader(f){ + } + bool readConfig(T* s,GwConfigHandler *cfg){ + if (s == nullptr) return false; + configReader(s,cfg); + return s->ok; + } +}; + +template +class GwSensorConfigInitializer : public GwInitializer>{ + public: + using GwInitializer>::GwInitializer; +}; +template +class GwSensorConfigInitializerList : public GwInitializer>::List{ + public: + using GwInitializer>::List::List; + bool readConfig(T *s,GwConfigHandler *cfg){ + for (auto &&scfg:*this){ + if (scfg.readConfig(s,cfg)) return true; + } + return false; + } + bool knowsPrefix(const String &prefix){ + for (auto &&scfg:*this){ + if (scfg.prefix == prefix) return true; + } + return false; + } +}; + +#define CFG_SGET(s, name, prefix) \ + cfg->getValue(s->name, GwConfigDefinitions::prefix##name) + +#define GWSENSORCONFIG(list,type,prefix,initFunction) \ + GwSensorConfigInitializer __init ## type ## prefix(list,GwSensorConfig(#prefix,initFunction)); + + #define CFG_GET(name,prefix) \ cfg->getValue(name, GwConfigDefinitions::prefix ## name)