1
0
mirror of https://github.com/thooge/esp32-nmea2000-obp60.git synced 2025-12-13 05:53:06 +01:00

intermediate: xdr type mapping

This commit is contained in:
wellenvogel
2021-11-19 21:16:04 +01:00
parent c6c33fa3dd
commit fbc955cd53
4 changed files with 574 additions and 0 deletions

View File

@@ -0,0 +1,125 @@
#include "GwXDRMappings.h"
#include "N2kMessages.h"
double PtoBar(double v){
if (N2kIsNA(v)) return v;
return v/100000L;
}
double BarToP(double v){
if (N2kIsNA(v)) return v;
return v*100000L;
}
GwXDRType * types[]={
new GwXDRType(GwXDRType::PRESS,"P","P"),
new GwXDRType(GwXDRType::PRESS,"P","B",
PtoBar,
BarToP),
new GwXDRType(GwXDRType::VOLT,"U","V"),
new GwXDRType(GwXDRType::AMP,"I","A"),
NULL
};
String GwXDRMappingDef::toString(){
String rt="";
rt+=String((int)category);
rt+=",";
rt+=String((int)direction);
rt+=",";
rt+=String(selector);
rt+=",";
rt+=String(field);
rt+=",";
rt+=String(instanceMode);
rt+=",";
rt+=String(instanceId);
return rt;
}
bool GwXDRMappingDef::handleToken(String tok,int index,GwXDRMappingDef *def){
switch(index){
int i;
case 0:
//category
i=tok.toInt();
if (i< XDRTEMP || i > XDRENGINE) return false;
def->category=(GwXDRCategory)i;
return true;
case 1:
//direction
i=tok.toInt();
if (i < GwXDRMappingDef::M_DISABLED ||
i>= GwXDRMappingDef::M_LAST) return false;
def->direction=(GwXDRMappingDef::Direction)i;
return true;
case 2:
//selector
//TODO: check selector?
i=tok.toInt();
def->selector=i;
return true;
case 3:
//field
i=tok.toInt();
def->field=i;
return true;
case 4:
//instance mode
i=tok.toInt();
if (i < GwXDRMappingDef::IS_SINGLE ||
i>= GwXDRMappingDef::IS_LAST) return false;
def->instanceMode=(GwXDRMappingDef::InstanceMode)i;
return true;
case 5:
//instance id
i=tok.toInt();
def->instanceId=i;
return true;
default:
break;
}
return false;
}
GwXDRMappingDef *GwXDRMappingDef::fromString(String s){
int found=0;
int last=0;
int index=0;
GwXDRMappingDef *rt=new GwXDRMappingDef();
while ((found = s.indexOf(',',last)) >= 0){
String tok=s.substring(last,found);
if (!handleToken(tok,index,rt)){
delete rt;
return NULL;
}
last=found+1;
index++;
}
if (last < s.length()){
String tok=s.substring(last);
if (!handleToken(tok,index,rt)){
delete rt;
return NULL;
}
}
return rt;
}
GwXDRMappings::GwXDRMappings(GwLog *logger, GwConfigHandler *config)
{
this->logger = logger;
this->config = config;
}
#define MAX_MAPPINGS 100
void GwXDRMappings::begin(){
char namebuf[10];
for (int i=0;i<MAX_MAPPINGS;i++){
snprintf(namebuf,9,"XDR%d",i);
namebuf[9]=0;
GwConfigInterface *cfg=config->getConfigItem(String(namebuf));
if (cfg){
GwXDRMappingDef *mapping=GwXDRMappingDef::fromString(cfg->asCString());
if (mapping){
LOG_DEBUG(GwLog::DEBUG,"read xdr mapping %s",mapping->toString().c_str());
}
}
}
}

View File

@@ -0,0 +1,135 @@
#ifndef _GWXDRMAPPINGS_H
#define _GWXDRMAPPINGS_H
#include "GwLog.h"
#include "GWConfig.h"
#include <WString.h>
#include <vector>
//enum must match the defines in xdrconfig.json
typedef enum {
XDRTEMP=0,
XDRHUMIDITY=1,
XDRPRSSURE=2,
XDRTIME=3, //unused
XDRFLUID=4,
XDRDCTYPE=5, //unused
XDRBATTYPE=6, //unused
XDRBATCHEM=7, //unused
XDRGEAR=8, //unused
XDRBAT=9,
XDRENGINE=10
} GwXDRCategory;
class GwXDRType{
public:
typedef enum{
PRESS=0,
PERCENT=1,
VOLT=2,
AMP=3
}TypeCode;
typedef double (* convert)(double);
TypeCode code;
String xdrtype;
String xdrunit;
convert tonmea=NULL;
convert fromnmea=NULL;
GwXDRType(TypeCode tc,String xdrtype,String xdrunit,convert fromnmea=NULL,convert tonmea=NULL){
this->code=tc;
this->xdrtype=xdrtype;
this->xdrunit=xdrunit;
this->fromnmea=fromnmea;
this->tonmea=tonmea;
}
};
class GwXDRTypeMapping{
public:
GwXDRCategory category;
int fieldIndex;
GwXDRType::TypeCode type;
GwXDRTypeMapping(GwXDRCategory category,
int fieldIndex,
GwXDRType::TypeCode type){
this->category=category;
this->type=type;
this->fieldIndex=fieldIndex;
}
};
class GwXDRMappingDef{
public:
typedef enum{
IS_SINGLE=0,
IS_IGNORE,
IS_AUTO,
IS_LAST
} InstanceMode;
typedef enum{
M_DISABLED=0,
M_BOTH=1,
M_TO2K=2,
M_FROM2K=3,
M_LAST
} Direction;
String xdrName;
GwXDRCategory category;
int selector=-1;
int field=-1;
InstanceMode instanceMode=IS_AUTO;
int instanceId=-1;
Direction direction=M_BOTH;
GwXDRMappingDef(String xdrName,GwXDRCategory category,
int selector, int field,InstanceMode istanceMode,int instance,
Direction direction){
this->xdrName=xdrName;
this->category=category;
this->selector=selector;
this->field=field;
this->instanceMode=instanceMode;
this->instanceId=instance;
this->direction=direction;
};
GwXDRMappingDef(){
category=XDRTEMP;
}
String toString();
static GwXDRMappingDef *fromString(String s);
private:
static bool handleToken(String tok,int index,GwXDRMappingDef *def);
};
class GwXDRMapping{
public:
GwXDRMappingDef *definition;
GwXDRType *type;
GwXDRMapping(GwXDRMappingDef *definition,GwXDRType *type){
this->definition=definition;
this->type=type;
}
//we allow 100 entities of code,selector and field nid
static long n2kKey(GwXDRType::TypeCode code,int selector,int field){
long rt=(int)code;
if (selector < 0) selector=0;
rt=rt*100+selector;
if (field < 0) field=0;
rt=rt*100*field;
return rt;
}
static String n183key(String xdrName,String xdrType,String xdrUnit){
String rt=xdrName;
rt+=",";
rt+=xdrType;
rt+=",";
rt+=xdrUnit;
return rt;
}
};
class GwXDRMappings{
private:
GwLog *logger;
GwConfigHandler *config;
public:
GwXDRMappings(GwLog *logger,GwConfigHandler *config);
void begin();
};
#endif