intermediate: restructure SHT3X

This commit is contained in:
andreas 2024-11-19 19:33:18 +01:00
parent 0200352f91
commit 0b565cfdc7
6 changed files with 48 additions and 75 deletions

View File

@ -23,9 +23,9 @@
*/ */
class BMP280Config; class BMP280Config;
GwSensorConfigInitializerList<BMP280Config> configs; static GwSensorConfigInitializerList<BMP280Config> configs;
class BMP280Config : public IICSensorBase<BMP280Config>{ class BMP280Config : public IICSensorBase{
public: public:
bool prAct=true; bool prAct=true;
bool tmAct=true; bool tmAct=true;
@ -38,7 +38,7 @@ class BMP280Config : public IICSensorBase<BMP280Config>{
float prOff=0; float prOff=0;
Adafruit_BMP280 *device=nullptr; Adafruit_BMP280 *device=nullptr;
uint32_t sensorId=-1; uint32_t sensorId=-1;
using IICSensorBase<BMP280Config>::IICSensorBase; using IICSensorBase::IICSensorBase;
virtual bool isActive(){return prAct||tmAct;} virtual bool isActive(){return prAct||tmAct;}
virtual bool initDevice(GwApi *api,TwoWire *wire){ virtual bool initDevice(GwApi *api,TwoWire *wire){
GwLog *logger=api->getLogger(); GwLog *logger=api->getLogger();
@ -92,6 +92,7 @@ class BMP280Config : public IICSensorBase<BMP280Config>{
virtual void readConfig(GwConfigHandler *cfg) override virtual void readConfig(GwConfigHandler *cfg) override
{ {
if (ok) return;
configs.readConfig(this,cfg); configs.readConfig(this,cfg);
} }
}; };

View File

@ -13,8 +13,7 @@
using BUSTYPE=TwoWire; using BUSTYPE=TwoWire;
using IICSensorList=SensorList; using IICSensorList=SensorList;
template<typename Sensor> using IICSensorBase=SensorTemplate<BUSTYPE,SensorBase::IIC>;
using IICSensorBase=SensorTemplate<BUSTYPE,SensorBase::IIC,Sensor>;
template <class CFG> template <class CFG>

View File

@ -1,11 +1,7 @@
#include "GwSHT3X.h" #include "GwSHT3X.h"
#ifdef _GWSHT3X #ifdef _GWSHT3X
#define TYPE "SHT3X" class SHT3XConfig;
#define PRFX1 TYPE "11" static GwSensorConfigInitializerList<SHT3XConfig> configs;
#define PRFX2 TYPE "12"
#define PRFX3 TYPE "21"
#define PRFX4 TYPE "22"
class SHT3XConfig : public IICSensorBase{ class SHT3XConfig : public IICSensorBase{
public: public:
String tmNam; String tmNam;
@ -15,8 +11,7 @@ class SHT3XConfig : public IICSensorBase{
tN2kHumiditySource huSrc; tN2kHumiditySource huSrc;
tN2kTempSource tmSrc; tN2kTempSource tmSrc;
SHT3X *device=nullptr; SHT3X *device=nullptr;
SHT3XConfig(GwApi *api,const String &prefix): using IICSensorBase::IICSensorBase;
SensorBase(TYPE,api,prefix){}
virtual bool isActive(){ virtual bool isActive(){
return tmAct || huAct; return tmAct || huAct;
} }
@ -62,79 +57,43 @@ class SHT3XConfig : public IICSensorBase{
LOG_DEBUG(GwLog::DEBUG, "unable to query %s: %d",prefix.c_str(), rt); 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){ virtual void readConfig(GwConfigHandler *cfg){
if (ok) return; if (ok) return;
if (prefix == PRFX1){ configs.readConfig(this,cfg);
busId=1; return;
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;
} }
}; };
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); return new SHT3XConfig(api,prfx);
}; };
SensorBase::Creator registerSHT3X(GwApi *api){ SensorBase::Creator registerSHT3X(GwApi *api){
GwLog *logger=api->getLogger(); GwLog *logger=api->getLogger();
#if defined(GWSHT3X) || defined (GWSHT3X11) #if defined(GWSHT3X) || defined (GWSHT3X11)
{ {
api->addSensor(creator(api,PRFX1)); api->addSensor(creator(api,"SHT3X11"));
CHECK_IIC1(); CHECK_IIC1();
#pragma message "GWSHT3X11 defined" #pragma message "GWSHT3X11 defined"
} }
#endif #endif
#if defined(GWSHT3X12) #if defined(GWSHT3X12)
{ {
api->addSensor(creator(api,PRFX2)); api->addSensor(creator(api,"SHT3X12"));
CHECK_IIC1(); CHECK_IIC1();
#pragma message "GWSHT3X12 defined" #pragma message "GWSHT3X12 defined"
} }
#endif #endif
#if defined(GWSHT3X21) #if defined(GWSHT3X21)
{ {
api->addSensor(creator(api,PRFX3)); api->addSensor(creator(api,"SHT3X21"));
CHECK_IIC2(); CHECK_IIC2();
#pragma message "GWSHT3X21 defined" #pragma message "GWSHT3X21 defined"
} }
#endif #endif
#if defined(GWSHT3X22) #if defined(GWSHT3X22)
{ {
api->addSensor(creator(api,PRFX4)); api->addSensor(creator(api,"SHT3X22"));
CHECK_IIC2(); CHECK_IIC2();
#pragma message "GWSHT3X22 defined" #pragma message "GWSHT3X22 defined"
} }
@ -142,6 +101,33 @@ SensorBase::Creator registerSHT3X(GwApi *api){
return creator; 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 #else
SensorBase::Creator registerSHT3X(GwApi *api){ SensorBase::Creator registerSHT3X(GwApi *api){
return SensorBase::Creator(); return SensorBase::Creator();

View File

@ -55,6 +55,7 @@ lib_deps =
${sensors.lib_deps} ${sensors.lib_deps}
build_flags= build_flags=
-D GWBMP280G1 -D GWBMP280G1
-D GWSHT3X11
-D M5_GROOVEIIC -D M5_GROOVEIIC
-D M5_CAN_KIT -D M5_CAN_KIT
${env.build_flags} ${env.build_flags}

View File

@ -47,12 +47,8 @@ class SensorBase{
virtual void readConfig(GwConfigHandler *cfg)=0; virtual void readConfig(GwConfigHandler *cfg)=0;
}; };
template<typename BUS,SensorBase::BusType bt,typename Sensor> template<typename BUS,SensorBase::BusType bt>
class SensorTemplate : public SensorBase{ class SensorTemplate : public SensorBase{
public:
using ConfigReader=std::function<bool(Sensor *,GwConfigHandler *)>;
protected:
ConfigReader configReader;
public: public:
SensorTemplate(GwApi *api,const String &prfx) SensorTemplate(GwApi *api,const String &prfx)
:SensorBase(bt,api,prfx){} :SensorBase(bt,api,prfx){}
@ -65,16 +61,6 @@ class SensorTemplate : public SensorBase{
if (busType != bt) return; if (busType != bt) return;
measure(api,(BUS*)bus,counterId); 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;
}
}; };

View File

@ -90,7 +90,7 @@ class SSIDevice{
}; };
class SSISensor : public SensorTemplate<BUSTYPE,SensorBase::SPI, SSISensor>{ class SSISensor : public SensorTemplate<BUSTYPE,SensorBase::SPI>{
std::unique_ptr<SSIDevice> device; std::unique_ptr<SSIDevice> device;
protected: protected:
int bits=12; int bits=12;
@ -125,7 +125,7 @@ class SSISensor : public SensorTemplate<BUSTYPE,SensorBase::SPI, SSISensor>{
} }
public: public:
SSISensor(GwApi *api,const String &prfx, int host):SensorTemplate<BUSTYPE,SensorBase::SPI,SSISensor>(api,prfx) SSISensor(GwApi *api,const String &prfx, int host):SensorTemplate<BUSTYPE,SensorBase::SPI>(api,prfx)
{ {
busId=host; busId=host;
} }