Skip to content

Commit

Permalink
intermediate: sensor config initializer list
Browse files Browse the repository at this point in the history
  • Loading branch information
andreas committed Nov 17, 2024
1 parent 1ab70ac commit d904d15
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 52 deletions.
110 changes: 58 additions & 52 deletions lib/iictask/GwBMP280.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@
#include <Adafruit_BMP280.h>
#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<BMP280Config> configs;

class BMP280Config : public IICSensorBase{
public:
bool prAct=true;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions lib/iictask/GwIicTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ static void addGroveItems(std::vector<IICSensorBase::Creator> &creators,GwApi *a
{
if (! creator) continue;
auto *scfg = creator(api, prfx);
if (scfg == nullptr) continue;
scfg->readConfig(api->getConfig());
if (scfg->ok)
{
Expand Down
57 changes: 57 additions & 0 deletions lib/sensors/GwSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
#ifndef _GWSENSORS_H
#define _GWSENSORS_H
#include "GwConfigItem.h"
#include <Arduino.h>
#include <memory>
#include <vector>
Expand Down Expand Up @@ -69,6 +70,62 @@ class SensorList : public std::vector<SensorBase::Ptr>{
using std::vector<SensorBase::Ptr>::vector;
};

/**
* helper classes for a simplified sensor configuration
* by creating a list of type GwSensorConfigInitializerList<SensorClass> 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 <typename T>
class GwSensorConfig{
public:
using ReadConfig=std::function<void(T*,GwConfigHandler*)>;
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<typename T>
class GwSensorConfigInitializer : public GwInitializer<GwSensorConfig<T>>{
public:
using GwInitializer<GwSensorConfig<T>>::GwInitializer;
};
template<typename T>
class GwSensorConfigInitializerList : public GwInitializer<GwSensorConfig<T>>::List{
public:
using GwInitializer<GwSensorConfig<T>>::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<type> __init ## type ## prefix(list,GwSensorConfig<type>(#prefix,initFunction));



#define CFG_GET(name,prefix) \
cfg->getValue(name, GwConfigDefinitions::prefix ## name)
Expand Down

0 comments on commit d904d15

Please sign in to comment.