From e806971713f31b1d37013d3f57bb690eb00ecb95 Mon Sep 17 00:00:00 2001 From: andreas Date: Wed, 20 Mar 2024 17:43:19 +0100 Subject: [PATCH] multiple groves for iic #1 --- lib/hardware/GwHardware.h | 80 ++++++++++++++++++++++++++++++------ lib/iictask/GwIicSensors.h | 17 ++++++++ lib/iictask/GwIicTask.cpp | 84 ++++++++++++++++++-------------------- lib/iictask/GwSHT3X.cpp | 32 +++++++++++++++ lib/iictask/GwSHT3X.h | 2 - 5 files changed, 157 insertions(+), 58 deletions(-) diff --git a/lib/hardware/GwHardware.h b/lib/hardware/GwHardware.h index 86c0114..c80cb57 100644 --- a/lib/hardware/GwHardware.h +++ b/lib/hardware/GwHardware.h @@ -40,6 +40,9 @@ #ifndef CFG_INIT #define CFG_INIT(...) #endif +#ifndef GROOVE_IIC + #define GROOVE_IIC(...) +#endif //general definitions for M5AtomLite //hint for groove pins: //according to some schematics the numbering is 1,2,3(VCC),4(GND) @@ -301,26 +304,79 @@ #ifdef M5_ENV3 #ifndef M5_GROOVEIIC - #define M5_GROOVEIIC M5_ENV3 + #define M5_GROOVEIIC #endif - #ifndef GWSHT3X - #define GWSHT3X -1 + GROOVE_IIC(GWSHT3X,Z,1) + GROOVE_IIC(GWQMP6988,Z,1) + #define _GWSHT3X +#endif +#ifdef M5_ENV3_A + #ifndef M5_GROOVEIIC_A + #define M5_GROOVEIIC_A #endif - #ifndef GWQMP6988 - #define GWQMP6988 -1 + GROOVE_IIC(GWSHT3X,A,1) + GROOVE_IIC(GWQMP6988,A,1) + #define _GWSHT3X +#endif +#ifdef M5_ENV3_B + #ifndef M5_GROOVEIIC_B + #define M5_GROOVEIIC_B #endif + GROOVE_IIC(GWSHT3X,B,1) + GROOVE_IIC(GWQMP6988,B,1) + #define _GWSHT3X +#endif +#ifdef M5_ENV3_C + #ifndef M5_GROOVEIIC_C + #define M5_GROOVEIIC_C + #endif + GROOVE_IIC(GWSHT3X,C,1) + GROOVE_IIC(GWQMP6988,C,1) + #define _GWSHT3X #endif #ifdef M5_GROOVEIIC - GROOVE_USE(M5_GROOVEIIC) - #ifdef GWIIC_SCL - #error "you cannot define both GWIIC_SCL and M5_GROOVEIIC" + GWRESOURCE_USE(GROOVE,M5_GROOVEIIC) + #ifndef _GWI_IIC1 + #define _GWI_IIC1 "Z",GROOVE_PIN_1,GROOVE_PIN_2 + #elif ! defined(_GWI_IIC2) + #define _GWI_IIC2 "Z",GROOVE_PIN_1,GROOVE_PIN_2 + #else + #error "both iic buses already in use" #endif - #define GWIIC_SCL GROOVE_PIN_1 - #ifdef GWIIC_SDA - #error "you cannot define both GWIIC_SDA and M5_GROOVEIIC" +#endif +#ifdef M5_GROOVEIIC_A + GWRESOURCE_USE(GROOVA,M5_GROOVEIIC_A) + #ifndef _GWI_IIC1 + #define _GWI_IIC1 A,GROOVEA_PIN_1,GROOVEA_PIN_2 + #elif ! defined(_GWI_IIC2) + #define _GWI_IIC2 A,GROOVEA_PIN_1,GROOVEA_PIN_2 + #else + #error "both iic buses already in use" #endif - #define GWIIC_SDA GROOVE_PIN_2 +#endif +#ifdef M5_GROOVEIIC_B + GWRESOURCE_USE(GROOVB,M5_GROOVEIIC_B) + #ifndef _GWI_IIC1 + #define _GWI_IIC1 B,GROOVEB_PIN_1,GROOVEB_PIN_2 + #elif ! defined(_GWI_IIC2) + #define _GWI_IIC2 B,GROOVEB_PIN_1,GROOVEB_PIN_2 + #else + #error "both iic buses already in use" + #endif +#endif +#ifdef M5_GROOVEIIC_C + GWRESOURCE_USE(GROOVC,M5_GROOVEIIC_C) + #ifndef _GWI_IIC1 + #define _GWI_IIC1 C,GROOVEC_PIN_1,GROOVEC_PIN_2 + #elif ! defined(_GWI_IIC2) + #define _GWI_IIC2 C,GROOVEC_PIN_1,GROOVEC_PIN_2 + #else + #error "both iic buses already in use" + #endif +#endif +#if defined(_GWI_IIC1) || defined (_GWI_IIC2) + #define _GWIIC #endif #ifdef _GWI_SERIAL_GROOVE diff --git a/lib/iictask/GwIicSensors.h b/lib/iictask/GwIicSensors.h index c3d21b4..dda38b9 100644 --- a/lib/iictask/GwIicSensors.h +++ b/lib/iictask/GwIicSensors.h @@ -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 iicGroveList; + #define GROOVE_IIC(base,grove,suffix) \ + static GwInitializer base ## grove ## suffix(iicGroveList,IICGrove(#base,#grove,#suffix)); +#endif #include "GwHardware.h" #include "GwSensor.h" #ifdef _GWIIC diff --git a/lib/iictask/GwIicTask.cpp b/lib/iictask/GwIicTask.cpp index 9698592..a9982ec 100644 --- a/lib/iictask/GwIicTask.cpp +++ b/lib/iictask/GwIicTask.cpp @@ -6,6 +6,9 @@ #include "GwSHT3X.h" #include +#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 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; diff --git a/lib/iictask/GwSHT3X.cpp b/lib/iictask/GwSHT3X.cpp index 8305220..705012a 100644 --- a/lib/iictask/GwSHT3X.cpp +++ b/lib/iictask/GwSHT3X.cpp @@ -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){ diff --git a/lib/iictask/GwSHT3X.h b/lib/iictask/GwSHT3X.h index 263d23e..b1926af 100644 --- a/lib/iictask/GwSHT3X.h +++ b/lib/iictask/GwSHT3X.h @@ -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