Skip to content

Commit

Permalink
restructure SSI sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
andreas committed Nov 25, 2024
1 parent d5a1c9c commit 38acd5a
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 75 deletions.
1 change: 0 additions & 1 deletion lib/iictask/GwBME280.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ class BME280Config : public IICSensorBase{
virtual bool preinit(GwApi * api){
GwLog *logger=api->getLogger();
LOG_DEBUG(GwLog::LOG,"%s configured",prefix.c_str());
api->addCapability(prefix,"true");
addPressureXdr(api,*this);
addTempXdr(api,*this);
addHumidXdr(api,*this);
Expand Down
1 change: 0 additions & 1 deletion lib/iictask/GwBMP280.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ class BMP280Config : public IICSensorBase{
virtual bool preinit(GwApi * api){
GwLog *logger=api->getLogger();
LOG_DEBUG(GwLog::LOG,"%s configured",prefix.c_str());
api->addCapability(prefix,"true");
addPressureXdr(api,*this);
addTempXdr(api,*this);
return isActive();
Expand Down
1 change: 0 additions & 1 deletion lib/iictask/GwQMP6988.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class QMP6988Config : public IICSensorBase{
virtual bool preinit(GwApi * api){
GwLog *logger=api->getLogger();
LOG_DEBUG(GwLog::LOG,"QMP6988 configured");
api->addCapability(prefix,"true");
addPressureXdr(api,*this);
return isActive();
}
Expand Down
1 change: 0 additions & 1 deletion lib/iictask/GwSHT3X.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class SHT3XConfig : public IICSensorBase{
virtual bool preinit(GwApi * api){
GwLog *logger=api->getLogger();
LOG_DEBUG(GwLog::LOG,"%s configured",prefix.c_str());
api->addCapability(prefix,"true");
addHumidXdr(api,*this);
addTempXdr(api,*this);
return isActive();
Expand Down
133 changes: 65 additions & 68 deletions lib/spitask/GWDMS22B.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,64 @@
#define CHECK_BUS(BUS) \
checkDef("missing config for " #BUS,GW ## BUS ## _CLK ,GW ## BUS ## _MISO);


class GWDMS22B;
static GwSensorConfigInitializerList<GWDMS22B> configs;


class GWDMS22B : public SSISensor{
public:
int zero=2047;
bool invt=false;
String zeroConfigName;
GWDMS22B(GwApi *api,const String &prfx):SSISensor(api,prfx){}
virtual bool preinit(GwApi * api){
GwLog *logger=api->getLogger();
LOG_DEBUG(GwLog::LOG,"DMS22B configured, prefix=%s, intv=%f, active=%d",prefix.c_str(),fintv,(int)act);
return act;
}
virtual void measure(GwApi * api,BUSTYPE *bus, int counterId){
GwLog *logger=api->getLogger();
uint32_t value=0;
esp_err_t res=readData(value);
if (res != ESP_OK){
LOG_DEBUG(GwLog::ERROR,"unable to measure %s: %d",prefix.c_str(),(int)res);
}
double resolved=(((int)value-zero)*360.0/mask);
if (invt) resolved=-resolved;
LOG_DEBUG(GwLog::DEBUG,"measure %s : %d, resolved: %f",prefix.c_str(),value,(float)resolved);
tN2kMsg msg;
SetN2kRudder(msg,DegToRad(resolved),iid);
api->sendN2kMessage(msg);
api->increment(counterId,prefix);
api->setCalibrationValue(zeroConfigName,(double)value);
}

virtual void readConfig(GwConfigHandler *cfg){
if (ok) return;
configs.readConfig(this,cfg);
}
};

#define ADD22B(PRFX,BUS) \
{\
CHECK_BUS(BUS); \
GWDMS22B *dms=new GWDMS22B(api,#PRFX,GW ## BUS ## _HOST);\
GWDMS22B *dms=new GWDMS22B(api,#PRFX);\
api->addSensor(dms,true); \
}

void registerDMS22B(GwApi *api){
#ifdef GWDMS22B11
#define ADD22B11 ADD22B(DMS22B11,SPI1)
ADD22B(DMS22B11,SPI1)
#ifndef GWDMS22B11_CS
#define GWDMS22B11_CS -1
#endif
#else
#define GWDMS22B11_CS -1
#define ADD22B11
#endif

#ifdef GWDMS22B12
#define ADD22B12 ADD22B(DMS22B12,SPI1)
ADD22B(DMS22B12,SPI1)
#ifndef GWDMS22B12_CS
#error "you need to define GWDMS22B12_CS"
#endif
Expand All @@ -48,22 +87,20 @@
#endif
#else
#define GWDMS22B12_CS -1
#define ADD22B12
#endif


#ifdef GWDMS22B21
#define ADD22B21 ADD22B(DMS22B21,SPI2)
ADD22B(DMS22B21,SPI2)
#ifndef GWDMS22B21_CS
#define GWDMS22B21_CS -1
#endif
#else
#define GWDMS22B21_CS -1
#define ADD22B21
#endif

#ifdef GWDMS22B22
#define ADD22B22 ADD22B(DMS22B22,SPI2)
ADD22B(DMS22B22,SPI2)
#ifndef GWDMS22B22_CS
#error "you need to define GWDMS22B22_CS"
#endif
Expand All @@ -72,68 +109,28 @@
#endif
#else
#define GWDMS22B22_CS -1
#define ADD22B22
#endif
}




class GWDMS22B : public SSISensor{
int zero=2047;
bool invt=false;
String zeroConfigName;
public:
GWDMS22B(GwApi *api,const String &prfx, int host):SSISensor(api,prfx,host){}
virtual bool preinit(GwApi * api){
GwLog *logger=api->getLogger();
LOG_DEBUG(GwLog::LOG,"DMS22B configured, prefix=%s, intv=%f, active=%d",prefix.c_str(),fintv,(int)act);
api->addCapability(prefix,"true");
return act;
}
virtual void measure(GwApi * api,BusType *bus, int counterId){
GwLog *logger=api->getLogger();
uint32_t value=0;
esp_err_t res=readData(value);
if (res != ESP_OK){
LOG_DEBUG(GwLog::ERROR,"unable to measure %s: %d",prefix.c_str(),(int)res);
}
double resolved=(((int)value-zero)*360.0/mask);
if (invt) resolved=-resolved;
LOG_DEBUG(GwLog::DEBUG,"measure %s : %d, resolved: %f",prefix.c_str(),value,(float)resolved);
tN2kMsg msg;
SetN2kRudder(msg,DegToRad(resolved),iid);
api->sendN2kMessage(msg);
api->increment(counterId,prefix);
api->setCalibrationValue(zeroConfigName,(double)value);
}
#define DMS22B(PRFX,...) \
if (prefix == #PRFX) {\
CFG_GET(act,PRFX); \
CFG_GET(iid,PRFX); \
CFG_GET(fintv,PRFX); \
CFG_GET(zero,PRFX); \
zeroConfigName=GwConfigDefinitions::PRFX ## zero;\
CFG_GET(invt,PRFX); \
bits=12; \
clock=500000; \
cs=GW ## PRFX ## _CS; \
__VA_ARGS__ \
}

#define CFGDMS22B(s,PRFX,bus,csv) \
CFG_SGET(s,act,PRFX); \
CFG_SGET(s,iid,PRFX); \
CFG_SGET(s,fintv,PRFX); \
CFG_SGET(s,zero,PRFX); \
s->zeroConfigName=GwConfigDefinitions::PRFX ## zero;\
CFG_SGET(s,invt,PRFX); \
s->bits=12; \
s->clock=500000; \
s->cs=csv; \
s->busId=bus; \
s->intv=1000*s->fintv; \
s->ok=true;

virtual void readConfig(GwConfigHandler *cfg){
DMS22B(DMS22B11);
DMS22B(DMS22B12);
DMS22B(DMS22B21);
DMS22B(DMS22B22);
intv=1000*fintv;
}
};

void registerDMS22B(GwApi *api){
ADD22B11
ADD22B12
ADD22B21
ADD22B22
#define SCDMS22B(prefix,bus) \
GWSENSORDEF(configs,GWDMS22B,CFGDMS22B,prefix,GW ## bus ## _HOST,GW ## prefix ## _CS)

}
SCDMS22B(DMS22B11,SPI1);
SCDMS22B(DMS22B12,SPI1);
SCDMS22B(DMS22B21,SPI2);
SCDMS22B(DMS22B22,SPI2);
6 changes: 3 additions & 3 deletions lib/spitask/GwSpiSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,14 @@ class SSIDevice{

class SSISensor : public SensorTemplate<BUSTYPE,SensorBase::SPI>{
std::unique_ptr<SSIDevice> device;
protected:
public:
int bits=12;
int mask=0xffff;
int cs=-1;
int clock=0;
bool act=false;
float fintv=0;
protected:
virtual bool initSSI(GwLog*logger,const SPIBus *bus,
int clock,int cs, int bits){
mask= (1 << bits)-1;
Expand All @@ -125,9 +126,8 @@ class SSISensor : public SensorTemplate<BUSTYPE,SensorBase::SPI>{
}

public:
SSISensor(GwApi *api,const String &prfx, int host):SensorTemplate<BUSTYPE,SensorBase::SPI>(api,prfx)
SSISensor(GwApi *api,const String &prfx):SensorTemplate<BUSTYPE,SensorBase::SPI>(api,prfx)
{
busId=host;
}
virtual bool isActive(){return act;};
virtual bool initDevice(GwApi *api,BUSTYPE *bus){
Expand Down

0 comments on commit 38acd5a

Please sign in to comment.