1
0
mirror of https://github.com/thooge/esp32-nmea2000-obp60.git synced 2025-12-14 06:23:07 +01:00

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

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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