From 0b565cfdc7a9bfe839dec38bce3f50ca2b0f0d11 Mon Sep 17 00:00:00 2001 From: andreas Date: Tue, 19 Nov 2024 19:33:18 +0100 Subject: [PATCH] intermediate: restructure SHT3X --- lib/iictask/GwBMP280.cpp | 7 +-- lib/iictask/GwIicSensors.h | 3 +- lib/iictask/GwSHT3X.cpp | 92 ++++++++++++++++---------------------- lib/iictask/platformio.ini | 1 + lib/sensors/GwSensor.h | 16 +------ lib/spitask/GwSpiSensor.h | 4 +- 6 files changed, 48 insertions(+), 75 deletions(-) diff --git a/lib/iictask/GwBMP280.cpp b/lib/iictask/GwBMP280.cpp index 1608466..ce6ea63 100644 --- a/lib/iictask/GwBMP280.cpp +++ b/lib/iictask/GwBMP280.cpp @@ -23,9 +23,9 @@ */ class BMP280Config; -GwSensorConfigInitializerList configs; +static GwSensorConfigInitializerList configs; -class BMP280Config : public IICSensorBase{ +class BMP280Config : public IICSensorBase{ public: bool prAct=true; bool tmAct=true; @@ -38,7 +38,7 @@ class BMP280Config : public IICSensorBase{ float prOff=0; Adafruit_BMP280 *device=nullptr; uint32_t sensorId=-1; - using IICSensorBase::IICSensorBase; + using IICSensorBase::IICSensorBase; virtual bool isActive(){return prAct||tmAct;} virtual bool initDevice(GwApi *api,TwoWire *wire){ GwLog *logger=api->getLogger(); @@ -92,6 +92,7 @@ class BMP280Config : public IICSensorBase{ virtual void readConfig(GwConfigHandler *cfg) override { + if (ok) return; configs.readConfig(this,cfg); } }; diff --git a/lib/iictask/GwIicSensors.h b/lib/iictask/GwIicSensors.h index 66e64f4..4937daa 100644 --- a/lib/iictask/GwIicSensors.h +++ b/lib/iictask/GwIicSensors.h @@ -13,8 +13,7 @@ using BUSTYPE=TwoWire; using IICSensorList=SensorList; -template -using IICSensorBase=SensorTemplate; +using IICSensorBase=SensorTemplate; template diff --git a/lib/iictask/GwSHT3X.cpp b/lib/iictask/GwSHT3X.cpp index 7c78f90..1ea7d54 100644 --- a/lib/iictask/GwSHT3X.cpp +++ b/lib/iictask/GwSHT3X.cpp @@ -1,11 +1,7 @@ #include "GwSHT3X.h" #ifdef _GWSHT3X -#define TYPE "SHT3X" -#define PRFX1 TYPE "11" -#define PRFX2 TYPE "12" -#define PRFX3 TYPE "21" -#define PRFX4 TYPE "22" - +class SHT3XConfig; +static GwSensorConfigInitializerList configs; class SHT3XConfig : public IICSensorBase{ public: String tmNam; @@ -15,8 +11,7 @@ class SHT3XConfig : public IICSensorBase{ tN2kHumiditySource huSrc; tN2kTempSource tmSrc; SHT3X *device=nullptr; - SHT3XConfig(GwApi *api,const String &prefix): - SensorBase(TYPE,api,prefix){} + using IICSensorBase::IICSensorBase; virtual bool isActive(){ return tmAct || huAct; } @@ -62,79 +57,43 @@ class SHT3XConfig : public IICSensorBase{ LOG_DEBUG(GwLog::DEBUG, "unable to query %s: %d",prefix.c_str(), rt); } } - /** - * we do not dynamically compute the config names - * just to get compile time errors if something does not fit - * correctly - */ - #define CFG3X(prefix) \ - CFG_GET(tmNam,prefix); \ - CFG_GET(huNam,prefix); \ - CFG_GET(iid,prefix); \ - CFG_GET(tmAct,prefix); \ - CFG_GET(huAct,prefix); \ - CFG_GET(intv,prefix); \ - CFG_GET(huSrc,prefix); \ - CFG_GET(tmSrc,prefix); - + virtual void readConfig(GwConfigHandler *cfg){ if (ok) return; - if (prefix == PRFX1){ - busId=1; - addr=0x44; - CFG3X(SHT3X11); - ok=true; - } - if (prefix == PRFX2){ - busId=1; - addr=0x45; - CFG3X(SHT3X12); - ok=true; - } - if (prefix == PRFX3){ - busId=2; - addr=0x44; - CFG3X(SHT3X21); - ok=true; - } - if (prefix == PRFX4){ - busId=2; - addr=0x45; - CFG3X(SHT3X22); - ok=true; - } - intv*=1000; + configs.readConfig(this,cfg); + return; } }; -SensorBase::Creator creator=[](GwApi *api,const String &prfx){ +SensorBase::Creator creator=[](GwApi *api,const String &prfx)-> SensorBase*{ + if (! configs.knowsPrefix(prfx)) return nullptr; return new SHT3XConfig(api,prfx); }; SensorBase::Creator registerSHT3X(GwApi *api){ GwLog *logger=api->getLogger(); #if defined(GWSHT3X) || defined (GWSHT3X11) { - api->addSensor(creator(api,PRFX1)); + api->addSensor(creator(api,"SHT3X11")); CHECK_IIC1(); #pragma message "GWSHT3X11 defined" } #endif #if defined(GWSHT3X12) { - api->addSensor(creator(api,PRFX2)); + api->addSensor(creator(api,"SHT3X12")); CHECK_IIC1(); #pragma message "GWSHT3X12 defined" } #endif #if defined(GWSHT3X21) { - api->addSensor(creator(api,PRFX3)); + api->addSensor(creator(api,"SHT3X21")); CHECK_IIC2(); #pragma message "GWSHT3X21 defined" } #endif #if defined(GWSHT3X22) { - api->addSensor(creator(api,PRFX4)); + api->addSensor(creator(api,"SHT3X22")); CHECK_IIC2(); #pragma message "GWSHT3X22 defined" } @@ -142,6 +101,33 @@ SensorBase::Creator registerSHT3X(GwApi *api){ return creator; }; +/** + * we do not dynamically compute the config names + * just to get compile time errors if something does not fit + * correctly + */ + #define CFGSHT3X(s,prefix,bus,baddr) \ + CFG_SGET(s,tmNam,prefix); \ + CFG_SGET(s,huNam,prefix); \ + CFG_SGET(s,iid,prefix); \ + CFG_SGET(s,tmAct,prefix); \ + CFG_SGET(s,huAct,prefix); \ + CFG_SGET(s,intv,prefix); \ + CFG_SGET(s,huSrc,prefix); \ + CFG_SGET(s,tmSrc,prefix); \ + s->busId = bus; \ + s->addr = baddr; \ + s->ok = true; \ + s->intv*=1000; + +#define SCSHT3X(list, prefix, bus, addr) \ + GWSENSORCONFIG(list, SHT3XConfig, prefix, [](SHT3XConfig *s, GwConfigHandler *cfg) { CFGSHT3X(s, prefix, bus, addr); }); + +SCSHT3X(configs, SHT3X11,1, 0x44); +SCSHT3X(configs, SHT3X12,1, 0x45); +SCSHT3X(configs, SHT3X21,2, 0x44); +SCSHT3X(configs, SHT3X22,2, 0x45); + #else SensorBase::Creator registerSHT3X(GwApi *api){ return SensorBase::Creator(); diff --git a/lib/iictask/platformio.ini b/lib/iictask/platformio.ini index 609ef5a..db868ea 100644 --- a/lib/iictask/platformio.ini +++ b/lib/iictask/platformio.ini @@ -55,6 +55,7 @@ lib_deps = ${sensors.lib_deps} build_flags= -D GWBMP280G1 + -D GWSHT3X11 -D M5_GROOVEIIC -D M5_CAN_KIT ${env.build_flags} diff --git a/lib/sensors/GwSensor.h b/lib/sensors/GwSensor.h index 0e469e5..3543b43 100644 --- a/lib/sensors/GwSensor.h +++ b/lib/sensors/GwSensor.h @@ -47,12 +47,8 @@ class SensorBase{ virtual void readConfig(GwConfigHandler *cfg)=0; }; -template +template class SensorTemplate : public SensorBase{ - public: - using ConfigReader=std::function; - protected: - ConfigReader configReader; public: SensorTemplate(GwApi *api,const String &prfx) :SensorBase(bt,api,prfx){} @@ -65,16 +61,6 @@ class SensorTemplate : public SensorBase{ if (busType != bt) return; measure(api,(BUS*)bus,counterId); }; - virtual void setConfigReader(ConfigReader r){ - configReader=r; - } - protected: - bool configFromReader(Sensor *s, GwConfigHandler *cfg){ - if (configReader){ - return configReader(s,cfg); - } - return false; - } }; diff --git a/lib/spitask/GwSpiSensor.h b/lib/spitask/GwSpiSensor.h index 9a27522..cedfd81 100644 --- a/lib/spitask/GwSpiSensor.h +++ b/lib/spitask/GwSpiSensor.h @@ -90,7 +90,7 @@ class SSIDevice{ }; -class SSISensor : public SensorTemplate{ +class SSISensor : public SensorTemplate{ std::unique_ptr device; protected: int bits=12; @@ -125,7 +125,7 @@ class SSISensor : public SensorTemplate{ } public: - SSISensor(GwApi *api,const String &prfx, int host):SensorTemplate(api,prfx) + SSISensor(GwApi *api,const String &prfx, int host):SensorTemplate(api,prfx) { busId=host; }