mirror of
https://github.com/thooge/esp32-nmea2000-obp60.git
synced 2025-12-14 06:23:07 +01:00
multiple groves for iic #1
This commit is contained in:
@@ -3,6 +3,23 @@
|
||||
#include "GwApi.h"
|
||||
#include "N2kMessages.h"
|
||||
#include "GwXdrTypeMappings.h"
|
||||
#ifdef _IIC_GROOVE_LIST
|
||||
class IICGrove{
|
||||
public:
|
||||
String base;
|
||||
String grove;
|
||||
String suffix;
|
||||
IICGrove(const String &b,const String &g,const String &s):
|
||||
base(b),grove(g),suffix(s){}
|
||||
String item(const String &grove, const String &bus){
|
||||
if (grove == this->grove) return base+bus+suffix;
|
||||
return "";
|
||||
}
|
||||
};
|
||||
static std::vector<IICGrove> iicGroveList;
|
||||
#define GROOVE_IIC(base,grove,suffix) \
|
||||
static GwInitializer<IICGrove> base ## grove ## suffix(iicGroveList,IICGrove(#base,#grove,#suffix));
|
||||
#endif
|
||||
#include "GwHardware.h"
|
||||
#include "GwSensor.h"
|
||||
#ifdef _GWIIC
|
||||
|
||||
@@ -6,6 +6,9 @@
|
||||
#include "GwSHT3X.h"
|
||||
#include <map>
|
||||
|
||||
#include "GwTimer.h"
|
||||
#include "GwHardware.h"
|
||||
|
||||
#ifndef GWIIC_SDA
|
||||
#define GWIIC_SDA -1
|
||||
#endif
|
||||
@@ -19,11 +22,6 @@
|
||||
#define GWIIC_SCL2 -1
|
||||
#endif
|
||||
|
||||
#include "GwTimer.h"
|
||||
#include "GwHardware.h"
|
||||
|
||||
|
||||
|
||||
void runIicTask(GwApi *api);
|
||||
|
||||
static IICSensorList sensors;
|
||||
@@ -54,6 +52,39 @@ void runIicTask(GwApi *api){
|
||||
return;
|
||||
}
|
||||
#else
|
||||
bool initWireDo(GwLog *logger, TwoWire &wire, int num, const String &dummy, int scl, int sda)
|
||||
{
|
||||
if (sda < 0 || scl < 0)
|
||||
{
|
||||
LOG_DEBUG(GwLog::ERROR, "IIC %d invalid config sda=%d,scl=%d",
|
||||
num, sda, scl);
|
||||
return false;
|
||||
}
|
||||
bool rt = Wire.begin(sda, scl);
|
||||
if (!rt)
|
||||
{
|
||||
LOG_DEBUG(GwLog::ERROR, "unable to initialize IIC %d at sad=%d,scl=%d",
|
||||
num, sda, scl);
|
||||
return rt;
|
||||
}
|
||||
LOG_DEBUG(GwLog::ERROR, "initialized IIC %d at sda=%d,scl=%d",
|
||||
num,sda,scl);
|
||||
return true;
|
||||
}
|
||||
bool initWire(GwLog *logger, TwoWire &wire, int num){
|
||||
if (num == 1){
|
||||
#ifdef _GWI_IIC1
|
||||
return initWireDo(logger,wire,num,_GWI_IIC1);
|
||||
#endif
|
||||
return initWireDo(logger,wire,num,"",GWIIC_SDA,GWIIC_SCL);
|
||||
}
|
||||
if (num == 2){
|
||||
#ifdef _GWI_IIC2
|
||||
return initWireDo(logger,wire,num,_GWI_IIC2);
|
||||
#endif
|
||||
return initWireDo(logger,wire,num,"",GWIIC_SDA2,GWIIC_SCL2);
|
||||
}
|
||||
}
|
||||
void runIicTask(GwApi *api){
|
||||
GwLog *logger=api->getLogger();
|
||||
std::map<int,TwoWire *> buses;
|
||||
@@ -66,50 +97,15 @@ void runIicTask(GwApi *api){
|
||||
{
|
||||
case 1:
|
||||
{
|
||||
if (GWIIC_SDA < 0 || GWIIC_SCL < 0)
|
||||
{
|
||||
LOG_DEBUG(GwLog::ERROR, "IIC 1 invalid config sda=%d,scl=%d",
|
||||
(int)GWIIC_SDA, (int)GWIIC_SCL);
|
||||
}
|
||||
else
|
||||
{
|
||||
bool rt = Wire.begin(GWIIC_SDA, GWIIC_SCL);
|
||||
if (!rt)
|
||||
{
|
||||
LOG_DEBUG(GwLog::ERROR, "unable to initialize IIC 1 at sad=%d,scl=%d",
|
||||
(int)GWIIC_SDA, (int)GWIIC_SCL);
|
||||
}
|
||||
else
|
||||
{
|
||||
buses[busId] = &Wire;
|
||||
LOG_DEBUG(GwLog::ERROR, "initialized IIC 1 at sda=%d,scl=%d",
|
||||
(int)GWIIC_SDA, (int)GWIIC_SCL);
|
||||
}
|
||||
if (initWire(logger,Wire,1)){
|
||||
buses[busId] = &Wire;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
{
|
||||
if (GWIIC_SDA2 < 0 || GWIIC_SCL2 < 0)
|
||||
{
|
||||
LOG_DEBUG(GwLog::ERROR, "IIC 2 invalid config sda=%d,scl=%d",
|
||||
(int)GWIIC_SDA2, (int)GWIIC_SCL2);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
bool rt = Wire1.begin(GWIIC_SDA2, GWIIC_SCL2);
|
||||
if (!rt)
|
||||
{
|
||||
LOG_DEBUG(GwLog::ERROR, "unable to initialize IIC 2 at sda=%d,scl=%d",
|
||||
(int)GWIIC_SDA2, (int)GWIIC_SCL2);
|
||||
}
|
||||
else
|
||||
{
|
||||
buses[busId] = &Wire1;
|
||||
LOG_DEBUG(GwLog::LOG, "initialized IIC 2 at sda=%d,scl=%d",
|
||||
(int)GWIIC_SDA2, (int)GWIIC_SCL2);
|
||||
}
|
||||
if (initWire(logger,Wire1,2)){
|
||||
buses[busId] = &Wire1;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#define _IIC_GROOVE_LIST
|
||||
#include "GwSHT3X.h"
|
||||
|
||||
#ifdef _GWSHT3X
|
||||
@@ -5,6 +6,8 @@
|
||||
#define PRFX2 "SHT3X12"
|
||||
#define PRFX3 "SHT3X21"
|
||||
#define PRFX4 "SHT3X22"
|
||||
static void addGroveItems(GwApi *api, IICSensorList &sensors, const String &bus,const String &grove, int, int);
|
||||
|
||||
class SHT3XConfig : public IICSensorBase{
|
||||
public:
|
||||
String tmNam;
|
||||
@@ -77,6 +80,7 @@ class SHT3XConfig : public IICSensorBase{
|
||||
CFG_GET(tmSrc,prefix);
|
||||
|
||||
virtual void readConfig(GwConfigHandler *cfg){
|
||||
if (ok) return;
|
||||
if (prefix == PRFX1){
|
||||
busId=1;
|
||||
addr=0x44;
|
||||
@@ -138,6 +142,34 @@ void registerSHT3X(GwApi *api,IICSensorList &sensors){
|
||||
#pragma message "GWSHT3X22 defined"
|
||||
}
|
||||
#endif
|
||||
#ifdef _GWI_IIC1
|
||||
addGroveItems(api,sensors,"1",_GWI_IIC1);
|
||||
#endif
|
||||
#ifdef _GWI_IIC2
|
||||
addGroveItems(api,sensors,"2",_GWI_IIC2);
|
||||
#endif
|
||||
};
|
||||
static void addGroveItems(GwApi *api, IICSensorList &sensors, const String &bus,const String &grove, int, int)
|
||||
{
|
||||
GwLog *logger=api->getLogger();
|
||||
for (auto &&init : iicGroveList)
|
||||
{
|
||||
String prfx = init.item(grove, bus);
|
||||
if (!prfx.isEmpty())
|
||||
{
|
||||
SHT3XConfig *scfg = new SHT3XConfig(api, prfx);
|
||||
scfg->readConfig(api->getConfig());
|
||||
if (scfg->ok)
|
||||
{
|
||||
LOG_DEBUG(GwLog::LOG, "adding %s from grove config", prfx.c_str());
|
||||
sensors.add(api, scfg);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_DEBUG(GwLog::ERROR, "invalid grove sensor config %s", prfx.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#else
|
||||
void registerSHT3X(GwApi *api,IICSensorList &sensors){
|
||||
|
||||
@@ -4,8 +4,6 @@
|
||||
#ifdef _GWIIC
|
||||
#if defined(GWSHT3X) || defined(GWSHT3X11) || defined(GWSHT3X12) || defined(GWSHT3X21) || defined(GWSHT3X22)
|
||||
#define _GWSHT3X
|
||||
#else
|
||||
#undef _GWSHT3X
|
||||
#endif
|
||||
#else
|
||||
#undef _GWSHT3X
|
||||
|
||||
Reference in New Issue
Block a user