intermediate: restructure for sensors via API

This commit is contained in:
andreas 2024-11-16 20:15:09 +01:00
parent 56518d9309
commit f75f8033d1
15 changed files with 173 additions and 77 deletions

View File

@ -9,6 +9,9 @@
#include "GwSynchronized.h" #include "GwSynchronized.h"
#include <map> #include <map>
#include <ESPAsyncWebServer.h> #include <ESPAsyncWebServer.h>
#include "GwSensor.h"
#include <functional>
#include <memory>
class GwApi; class GwApi;
typedef void (*GwUserTaskFunction)(GwApi *); typedef void (*GwUserTaskFunction)(GwApi *);
//API to be used for additional tasks //API to be used for additional tasks
@ -59,6 +62,7 @@ class GwApi{
protected: protected:
virtual bool iset(const String &name, Ptr v) = 0; virtual bool iset(const String &name, Ptr v) = 0;
virtual Ptr iget(const String &name, int &result) = 0; virtual Ptr iget(const String &name, int &result) = 0;
virtual bool iupdate(const String &name,std::function<Ptr(Ptr v)>)=0;
public: public:
template <typename T> template <typename T>
bool set(const T &v){ bool set(const T &v){
@ -73,6 +77,10 @@ class GwApi{
bool claim(const String &task){ bool claim(const String &task){
return true; return true;
} }
template <typename T>
bool update(std::function<bool(T *)>){
return false;
}
}; };
class Status{ class Status{
public: public:
@ -201,7 +209,13 @@ class GwApi{
* @param name: the config name this value is used for * @param name: the config name this value is used for
* @param value: the current value * @param value: the current value
*/ */
virtual void setCalibrationValue(const String &name, double value); virtual void setCalibrationValue(const String &name, double value)=0;
/**
* add a sensor
* depending on the type it will be added to the appropriate task
* @param sensor: created sensor config
*/
virtual void addSensor(SensorBase* sensor,bool readConfig=true){};
/** /**
* not thread safe methods * not thread safe methods
@ -274,9 +288,39 @@ static void checkDef(T... args){};
}\ }\
type *tp=(type*)ptr.get(); \ type *tp=(type*)ptr.get(); \
return type(*tp); \ return type(*tp); \
} } \
template<> \
inline bool GwApi::TaskInterfaces::update(std::function<bool(type *)> f) { \
return iupdate(#type,[f](GwApi::TaskInterfaces::Ptr cp)->GwApi::TaskInterfaces::Ptr{ \
if (cp) { \
f((type *)cp.get()); \
return cp; \
} \
type * et=new type(); \
bool res=f(et); \
if (! res){ \
delete et; \
return GwApi::TaskInterfaces::Ptr(); \
} \
return GwApi::TaskInterfaces::Ptr(et); \
}); \
} \
#ifndef DECLARE_TASKIF #ifndef DECLARE_TASKIF
#define DECLARE_TASKIF(type) DECLARE_TASKIF_IMPL(type) #define DECLARE_TASKIF(type) DECLARE_TASKIF_IMPL(type)
#endif #endif
/**
* do not use this interface directly
* instead use the API function addSensor
*/
class ConfiguredSensors : public GwApi::TaskInterfaces::Base{
public:
SensorList sensors;
};
DECLARE_TASKIF(ConfiguredSensors);
#endif #endif

View File

@ -149,35 +149,31 @@ class BME280Config : public IICSensorBase{
static IICSensorBase::Creator creator([](GwApi *api, const String &prfx){ static IICSensorBase::Creator creator([](GwApi *api, const String &prfx){
return new BME280Config(api,prfx); return new BME280Config(api,prfx);
}); });
IICSensorBase::Creator registerBME280(GwApi *api,IICSensorList &sensors){ IICSensorBase::Creator registerBME280(GwApi *api){
#if defined(GWBME280) || defined(GWBME28011) #if defined(GWBME280) || defined(GWBME28011)
{ {
auto *cfg=creator(api,PRFX1); api->addSensor(creator(api,PRFX1));
sensors.add(api,cfg);
CHECK_IIC1(); CHECK_IIC1();
#pragma message "GWBME28011 defined" #pragma message "GWBME28011 defined"
} }
#endif #endif
#if defined(GWBME28012) #if defined(GWBME28012)
{ {
auto *cfg=creator(api,PRFX2); api->addSensor(creator(api,PRFX2));
sensors.add(api,cfg);
CHECK_IIC1(); CHECK_IIC1();
#pragma message "GWBME28012 defined" #pragma message "GWBME28012 defined"
} }
#endif #endif
#if defined(GWBME28021) #if defined(GWBME28021)
{ {
auto *cfg=creator(api,PRFX3); api->addSensor(creator(api,PRFX3));
sensors.add(api,cfg);
CHECK_IIC2(); CHECK_IIC2();
#pragma message "GWBME28021 defined" #pragma message "GWBME28021 defined"
} }
#endif #endif
#if defined(GWBME28022) #if defined(GWBME28022)
{ {
auto *cfg=creator(api,PRFX4); api->addSensor(creator(api,PRFX4));
sensors.add(api,cfg);
CHECK_IIC1(); CHECK_IIC1();
#pragma message "GWBME28022 defined" #pragma message "GWBME28022 defined"
} }
@ -185,7 +181,7 @@ IICSensorBase::Creator registerBME280(GwApi *api,IICSensorList &sensors){
return creator; return creator;
} }
#else #else
IICSensorBase::Creator registerBME280(GwApi *api,IICSensorList &sensors){ IICSensorBase::Creator registerBME280(GwApi *api){
return IICSensorBase::Creator(); return IICSensorBase::Creator();
} }

View File

@ -1,5 +1,5 @@
#ifndef _GWBME280_H #ifndef _GWBME280_H
#define _GWBME280_H #define _GWBME280_H
#include "GwIicSensors.h" #include "GwIicSensors.h"
IICSensorBase::Creator registerBME280(GwApi *api,IICSensorList &sensors); IICSensorBase::Creator registerBME280(GwApi *api);
#endif #endif

View File

@ -133,39 +133,31 @@ class BMP280Config : public IICSensorBase{
static IICSensorBase::Creator creator([](GwApi *api, const String &prfx){ static IICSensorBase::Creator creator([](GwApi *api, const String &prfx){
return new BMP280Config(api,prfx); return new BMP280Config(api,prfx);
}); });
IICSensorBase::Creator registerBMP280(GwApi *api,IICSensorList &sensors){ IICSensorBase::Creator registerBMP280(GwApi *api){
#if defined(GWBMP280) || defined(GWBMP28011) #if defined(GWBMP280) || defined(GWBMP28011)
{ {
auto *cfg=creator(api,PRFX1); api->addSensor(creator(api,PRFX1));
//BMP280Config *cfg=new BMP280Config(api,PRFX1);
sensors.add(api,cfg);
CHECK_IIC1(); CHECK_IIC1();
#pragma message "GWBMP28011 defined" #pragma message "GWBMP28011 defined"
} }
#endif #endif
#if defined(GWBMP28012) #if defined(GWBMP28012)
{ {
auto *cfg=creator(api,PRFX2); api->addSensor(creator(api,PRFX2));
//BMP280Config *cfg=new BMP280Config(api,PRFX2);
sensors.add(api,cfg);
CHECK_IIC1(); CHECK_IIC1();
#pragma message "GWBMP28012 defined" #pragma message "GWBMP28012 defined"
} }
#endif #endif
#if defined(GWBMP28021) #if defined(GWBMP28021)
{ {
auto *cfg=creator(api,PRFX3); api->addSensor(creator(api,PRFX3));
//BMP280Config *cfg=new BMP280Config(api,PRFX3);
sensors.add(api,cfg);
CHECK_IIC2(); CHECK_IIC2();
#pragma message "GWBMP28021 defined" #pragma message "GWBMP28021 defined"
} }
#endif #endif
#if defined(GWBMP28022) #if defined(GWBMP28022)
{ {
auto *cfg=creator(api,PRFX4); api->addSensor(creator(api,PRFX4));
//BMP280Config *cfg=new BMP280Config(api,PRFX4);
sensors.add(api,cfg);
CHECK_IIC1(); CHECK_IIC1();
#pragma message "GWBMP28022 defined" #pragma message "GWBMP28022 defined"
} }
@ -173,7 +165,7 @@ IICSensorBase::Creator registerBMP280(GwApi *api,IICSensorList &sensors){
return creator; return creator;
} }
#else #else
IICSensorBase::Creator registerBMP280(GwApi *api,IICSensorList &sensors){ IICSensorBase::Creator registerBMP280(GwApi *api){
return IICSensorBase::Creator(); return IICSensorBase::Creator();
} }

View File

@ -1,6 +1,6 @@
#ifndef _GWBMP280_H #ifndef _GWBMP280_H
#define _GWBMP280_H #define _GWBMP280_H
#include "GwIicSensors.h" #include "GwIicSensors.h"
IICSensorBase::Creator registerBMP280(GwApi *api,IICSensorList &sensors); IICSensorBase::Creator registerBMP280(GwApi *api);
#endif #endif

View File

@ -43,8 +43,7 @@ static std::vector<IICGrove> iicGroveList;
void runIicTask(GwApi *api); void runIicTask(GwApi *api);
static IICSensorList sensors; static void addGroveItems(std::vector<IICSensorBase::Creator> &creators,GwApi *api, const String &bus,const String &grove, int, int)
static void addGroveItems(std::vector<IICSensorBase::Creator> &creators,GwApi *api, IICSensorList &sensors, const String &bus,const String &grove, int, int)
{ {
GwLog *logger=api->getLogger(); GwLog *logger=api->getLogger();
for (auto &&init : iicGroveList) for (auto &&init : iicGroveList)
@ -65,7 +64,7 @@ static void addGroveItems(std::vector<IICSensorBase::Creator> &creators,GwApi *a
if (scfg->ok) if (scfg->ok)
{ {
LOG_DEBUG(GwLog::LOG, "adding %s from grove config", prfx.c_str()); LOG_DEBUG(GwLog::LOG, "adding %s from grove config", prfx.c_str());
sensors.add(api, SensorBase::Ptr(scfg)); api->addSensor(scfg,false);
found=true; found=true;
break; break;
} }
@ -90,18 +89,22 @@ void initIicTask(GwApi *api){
bool addTask=false; bool addTask=false;
GwConfigHandler *config=api->getConfig(); GwConfigHandler *config=api->getConfig();
std::vector<IICSensorBase::Creator> creators; std::vector<IICSensorBase::Creator> creators;
creators.push_back(registerSHT3X(api,sensors)); creators.push_back(registerSHT3X(api));
creators.push_back(registerQMP6988(api,sensors)); creators.push_back(registerQMP6988(api));
creators.push_back(registerBME280(api,sensors)); creators.push_back(registerBME280(api));
creators.push_back(registerBMP280(api,sensors)); creators.push_back(registerBMP280(api));
#ifdef _GWI_IIC1 #ifdef _GWI_IIC1
addGroveItems(creators,api,sensors,"1",_GWI_IIC1); addGroveItems(creators,api,"1",_GWI_IIC1);
#endif #endif
#ifdef _GWI_IIC2 #ifdef _GWI_IIC2
addGroveItems(creators,api,sensors,"2",_GWI_IIC2); addGroveItems(creators,api,"2",_GWI_IIC2);
#endif #endif
for (auto it=sensors.begin();it != sensors.end();it++){ //TODO: ensure that we run after other init tasks...
if ((*it)->preinit(api)) addTask=true; int res=-1;
ConfiguredSensors sensorList=api->taskInterfaces()->get<ConfiguredSensors>(res);
for (auto &&it: sensorList.sensors){
if (it->busType != SensorBase::IIC) continue;
if (it->preinit(api)) addTask=true;
} }
if (addTask){ if (addTask){
api->addUserTask(runIicTask,"iicTask",4000); api->addUserTask(runIicTask,"iicTask",4000);
@ -154,8 +157,11 @@ void runIicTask(GwApi *api){
GwLog *logger=api->getLogger(); GwLog *logger=api->getLogger();
std::map<int,TwoWire *> buses; std::map<int,TwoWire *> buses;
LOG_DEBUG(GwLog::LOG,"iic task started"); LOG_DEBUG(GwLog::LOG,"iic task started");
for (auto it=sensors.begin();it != sensors.end();it++){ int res=-1;
int busId=(*it)->busId; ConfiguredSensors sensorList=api->taskInterfaces()->get<ConfiguredSensors>(res);
for (auto &&it : sensorList.sensors){
if (it->busType != SensorBase::IIC) continue;
int busId=it->busId;
auto bus=buses.find(busId); auto bus=buses.find(busId);
if (bus == buses.end()){ if (bus == buses.end()){
switch (busId) switch (busId)
@ -175,7 +181,7 @@ void runIicTask(GwApi *api){
} }
break; break;
default: default:
LOG_DEBUG(GwLog::ERROR, "invalid bus id %d at config %s", busId, (*it)->prefix.c_str()); LOG_DEBUG(GwLog::ERROR, "invalid bus id %d at config %s", busId, it->prefix.c_str());
break; break;
} }
} }
@ -184,7 +190,7 @@ void runIicTask(GwApi *api){
bool runLoop=false; bool runLoop=false;
GwIntervalRunner timers; GwIntervalRunner timers;
int counterId=api->addCounter("iicsensors"); int counterId=api->addCounter("iicsensors");
for (auto &&cfg:sensors){ for (auto && cfg: sensorList.sensors){
if (cfg->busType != SensorBase::IIC) continue; if (cfg->busType != SensorBase::IIC) continue;
auto bus=buses.find(cfg->busId); auto bus=buses.find(cfg->busId);
if (! cfg->isActive()) continue; if (! cfg->isActive()) continue;

View File

@ -4,9 +4,4 @@
#include "GwSensor.h" #include "GwSensor.h"
void initIicTask(GwApi *api); void initIicTask(GwApi *api);
DECLARE_INITFUNCTION(initIicTask); DECLARE_INITFUNCTION(initIicTask);
class IICSensors : public GwApi::TaskInterfaces::Base{
public:
SensorList sensors;
};
DECLARE_TASKIF(IICSensors);
#endif #endif

View File

@ -86,32 +86,28 @@ IICSensorBase::Creator registerQMP6988(GwApi *api,IICSensorList &sensors){
GwLog *logger=api->getLogger(); GwLog *logger=api->getLogger();
#if defined(GWQMP6988) || defined(GWQMP698811) #if defined(GWQMP6988) || defined(GWQMP698811)
{ {
QMP6988Config *scfg=new QMP6988Config(api,PRFX1); api->addSensor(new QMP6988Config(api,PRFX1));
sensors.add(api,scfg);
CHECK_IIC1(); CHECK_IIC1();
#pragma message "GWQMP698811 defined" #pragma message "GWQMP698811 defined"
} }
#endif #endif
#if defined(GWQMP698812) #if defined(GWQMP698812)
{ {
QMP6988Config *scfg=new QMP6988Config(api,PRFX2); api->addSensor(new QMP6988Config(api,PRFX2));
sensors.add(api,scfg);
CHECK_IIC1(); CHECK_IIC1();
#pragma message "GWQMP698812 defined" #pragma message "GWQMP698812 defined"
} }
#endif #endif
#if defined(GWQMP698821) #if defined(GWQMP698821)
{ {
QMP6988Config *scfg=new QMP6988Config(api,PRFX3); api->addSensor(new QMP6988Config(api,PRFX3));
sensors.add(api,scfg);
CHECK_IIC2(); CHECK_IIC2();
#pragma message "GWQMP698821 defined" #pragma message "GWQMP698821 defined"
} }
#endif #endif
#if defined(GWQMP698822) #if defined(GWQMP698822)
{ {
QMP6988Config *scfg=new QMP6988Config(api,PRFX4); api->addSensor(new QMP6988Config(api,PRFX4));
sensors.add(api,scfg);
CHECK_IIC2(); CHECK_IIC2();
#pragma message "GWQMP698822 defined" #pragma message "GWQMP698822 defined"
} }
@ -120,7 +116,7 @@ IICSensorBase::Creator registerQMP6988(GwApi *api,IICSensorList &sensors){
} }
#else #else
IICSensorBase::Creator registerQMP6988(GwApi *api,IICSensorList &sensors){ IICSensorBase::Creator registerQMP6988(GwApi *api){
return IICSensorBase::Creator(); return IICSensorBase::Creator();
} }
#endif #endif

View File

@ -16,5 +16,5 @@
#ifdef _GWQMP6988 #ifdef _GWQMP6988
#include "QMP6988.h" #include "QMP6988.h"
#endif #endif
IICSensorBase::Creator registerQMP6988(GwApi *api,IICSensorList &sensors); IICSensorBase::Creator registerQMP6988(GwApi *api);
#endif #endif

View File

@ -109,36 +109,32 @@ class SHT3XConfig : public IICSensorBase{
IICSensorBase::Creator creator=[](GwApi *api,const String &prfx){ IICSensorBase::Creator creator=[](GwApi *api,const String &prfx){
return new SHT3XConfig(api,prfx); return new SHT3XConfig(api,prfx);
}; };
IICSensorBase::Creator registerSHT3X(GwApi *api,IICSensorList &sensors){ IICSensorBase::Creator registerSHT3X(GwApi *api){
GwLog *logger=api->getLogger(); GwLog *logger=api->getLogger();
#if defined(GWSHT3X) || defined (GWSHT3X11) #if defined(GWSHT3X) || defined (GWSHT3X11)
{ {
auto *scfg=creator(api,PRFX1); api->addSensor(creator(api,PRFX1));
sensors.add(api,scfg);
CHECK_IIC1(); CHECK_IIC1();
#pragma message "GWSHT3X11 defined" #pragma message "GWSHT3X11 defined"
} }
#endif #endif
#if defined(GWSHT3X12) #if defined(GWSHT3X12)
{ {
auto *scfg=creator(api,PRFX2); api->addSensor(creator(api,PRFX2));
sensors.add(api,scfg);
CHECK_IIC1(); CHECK_IIC1();
#pragma message "GWSHT3X12 defined" #pragma message "GWSHT3X12 defined"
} }
#endif #endif
#if defined(GWSHT3X21) #if defined(GWSHT3X21)
{ {
auto *scfg=creator(api,PRFX3); api->addSensor(creator(api,PRFX3));
sensors.add(api,scfg);
CHECK_IIC2(); CHECK_IIC2();
#pragma message "GWSHT3X21 defined" #pragma message "GWSHT3X21 defined"
} }
#endif #endif
#if defined(GWSHT3X22) #if defined(GWSHT3X22)
{ {
auto *scfg=creator(api,PRFX4); api->addSensor(creator(api,PRFX4));
sensors.add(api,scfg);
CHECK_IIC2(); CHECK_IIC2();
#pragma message "GWSHT3X22 defined" #pragma message "GWSHT3X22 defined"
} }
@ -147,7 +143,7 @@ IICSensorBase::Creator registerSHT3X(GwApi *api,IICSensorList &sensors){
}; };
#else #else
IICSensorBase::Creator registerSHT3X(GwApi *api,IICSensorList &sensors){ IICSensorBase::Creator registerSHT3X(GwApi *api){
return IICSensorBase::Creator(); return IICSensorBase::Creator();
} }

View File

@ -16,5 +16,5 @@
#ifdef _GWSHT3X #ifdef _GWSHT3X
#include "SHT3X.h" #include "SHT3X.h"
#endif #endif
IICSensorBase::Creator registerSHT3X(GwApi *api,IICSensorList &sensors); IICSensorBase::Creator registerSHT3X(GwApi *api);
#endif #endif

20
lib/sensors/GwSensor.cpp Normal file
View File

@ -0,0 +1,20 @@
/*
(C) Andreas Vogel andreas@wellenvogel.de
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "GwSensor.h"
#include "GwApi.h"
void SensorList::add(SensorBase::Ptr sensor){
this->push_back(sensor);
}

View File

@ -14,9 +14,11 @@
*/ */
#ifndef _GWSENSORS_H #ifndef _GWSENSORS_H
#define _GWSENSORS_H #define _GWSENSORS_H
#include "GwApi.h" #include <Arduino.h>
#include "GwLog.h"
#include <memory> #include <memory>
#include <vector>
class GwApi;
class GwConfigHandler;
class SensorBase{ class SensorBase{
public: public:
using BusType=enum{ using BusType=enum{
@ -63,16 +65,13 @@ class SensorTemplate : public SensorBase{
class SensorList : public std::vector<SensorBase::Ptr>{ class SensorList : public std::vector<SensorBase::Ptr>{
public: public:
void add(GwApi *api, SensorBase::Ptr sensor){ void add(SensorBase::Ptr sensor);
sensor->readConfig(api->getConfig());
api->getLogger()->logDebug(GwLog::LOG,"configured sensor %s, status %d",sensor->prefix.c_str(),(int)sensor->ok);
this->push_back(sensor);
}
using std::vector<SensorBase::Ptr>::vector; using std::vector<SensorBase::Ptr>::vector;
}; };
#define CFG_GET(name,prefix) \ #define CFG_GET(name,prefix) \
cfg->getValue(name, GwConfigDefinitions::prefix ## name) cfg->getValue(name, GwConfigDefinitions::prefix ## name)
#endif #endif

View File

@ -16,7 +16,7 @@
#ifndef _GWSPISENSOR_H #ifndef _GWSPISENSOR_H
#define _GWSPISENSOR_H #define _GWSPISENSOR_H
#include <driver/spi_master.h> #include <driver/spi_master.h>
#include "GwSensor.h" #include "GwApi.h"
#include <memory> #include <memory>
class SPIBus{ class SPIBus{

View File

@ -98,6 +98,40 @@ class TaskInterfacesStorage{
} }
result = it->second.updates; result = it->second.updates;
return it->second.ptr; return it->second.ptr;
}
bool update(const String &name, std::function<GwApi::TaskInterfaces::Ptr(GwApi::TaskInterfaces::Ptr)>f){
GWSYNCHRONIZED(&lock);
auto vit=values.find(name);
bool rt=false;
int mode=0;
if (vit == values.end()){
mode=1;
auto np=f(GwApi::TaskInterfaces::Ptr());
if (np){
mode=11;
values[name]=TaskDataEntry(np);
rt=true;
}
}
else
{
auto np = f(vit->second.ptr);
mode=2;
if (np)
{
mode=22;
vit->second = np;
vit->second.updates++;
if (vit->second.updates < 0)
{
vit->second.updates = 0;
}
rt=true;
}
}
LOG_DEBUG(GwLog::DEBUG,"TaskApi::update %s (mode %d)returns %d",name.c_str(),mode,(int)rt);
return rt;
} }
}; };
class TaskInterfacesImpl : public GwApi::TaskInterfaces{ class TaskInterfacesImpl : public GwApi::TaskInterfaces{
@ -115,6 +149,9 @@ class TaskInterfacesImpl : public GwApi::TaskInterfaces{
virtual Ptr iget(const String &name, int &result){ virtual Ptr iget(const String &name, int &result){
return storage->get(name,result); return storage->get(name,result);
} }
virtual bool iupdate(const String &name,std::function<Ptr(Ptr v)> f){
return storage->update(name,f);
}
}; };
@ -291,6 +328,21 @@ public:
handler(req); handler(req);
return true; return true;
} }
virtual void addSensor(SensorBase *sb,bool readConfig=true) override{
if (sb == nullptr) return;
SensorBase::Ptr sensor(sb);
if (readConfig) sb->readConfig(this->getConfig());
if (! sensor->ok){
api->getLogger()->logDebug(GwLog::ERROR,"sensor %s nok , bustype=%d",sensor->prefix.c_str(),(int)sensor->busType);
return;
}
bool rt=taskInterfaces()->update<ConfiguredSensors>( [sensor,this](ConfiguredSensors *sensors)->bool{
api->getLogger()->logDebug(GwLog::LOG,"adding sensor %s, type=%d",sensor->prefix,(int)sensor->busType);
sensors->sensors.add(sensor);
return true;
});
api->getLogger()->logDebug(GwLog::LOG,"adding sensor %s returns %d",sensor->prefix.c_str(),(int)rt);
}
}; };
GwUserCode::GwUserCode(GwApiInternal *api,SemaphoreHandle_t *mainLock){ GwUserCode::GwUserCode(GwApiInternal *api,SemaphoreHandle_t *mainLock){