From ffdfd917ae9c795155e986a673b864b7635f4422 Mon Sep 17 00:00:00 2001 From: andreas Date: Tue, 24 Oct 2023 18:47:39 +0200 Subject: [PATCH] generate XDR field and selector defines --- extra_script.py | 33 ++++++++++++++++++++++++++++++- lib/exampletask/GwExampleTask.cpp | 16 ++++++++++++--- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/extra_script.py b/extra_script.py index 95ccbb2..969935f 100644 --- a/extra_script.py +++ b/extra_script.py @@ -7,6 +7,7 @@ import inspect import json import glob from datetime import datetime +import re Import("env") #print(env.Dump()) OWN_FILE="extra_script.py" @@ -172,7 +173,8 @@ def generateCfg(inFile,outFile,impl): data+="#endif\n" writeFileIfChanged(outFile,data) - +def labelFilter(label): + return re.sub("[^a-zA-Z0-9]","",re.sub("\([0-9]*\)","",label)) def generateXdrMappings(fp,oh,inFile=''): jdoc=json.load(fp) oh.write("static GwXDRTypeMapping* typeMappings[]={\n") @@ -211,6 +213,35 @@ def generateXdrMappings(fp,oh,inFile=''): oh.write(" new GwXDRTypeMapping(%d,%d,%d) /*%s:%s*/"%(cid,id,tc,cat,l)) oh.write("\n") oh.write("};\n") + for cat in jdoc: + item=jdoc[cat] + cid=item.get('id') + if cid is None: + continue + selectors=item.get('selector') + if selectors is not None: + for selector in selectors: + label=selector.get('l') + value=selector.get('v') + if label is not None and value is not None: + label=labelFilter(label) + define=("GWXDRSEL_%s_%s"%(cat,label)).upper() + oh.write(" #define %s %s\n"%(define,value)) + fields=item.get('fields') + if fields is not None: + idx=0 + for field in fields: + v=field.get('v') + if v is None: + v=idx + else: + v=int(v) + label=field.get('l') + if v is not None and label is not None: + define=("GWXDRFIELD_%s_%s"%(cat,labelFilter(label))).upper(); + oh.write(" #define %s %s\n"%(define,str(v))) + idx+=1 + userTaskDirs=[] diff --git a/lib/exampletask/GwExampleTask.cpp b/lib/exampletask/GwExampleTask.cpp index d6901d8..59e3959 100644 --- a/lib/exampletask/GwExampleTask.cpp +++ b/lib/exampletask/GwExampleTask.cpp @@ -7,6 +7,7 @@ #include "GWConfig.h" #include #include "N2kMessages.h" +#include "GwXdrTypeMappings.h" /** * INVALID!!! - the next interface declaration will not work * as it is not in the correct header file @@ -39,11 +40,20 @@ void exampleInit(GwApi *api){ if (!voltageTransducer.isEmpty()){ int instance=api->getConfig()->getInt(GwConfigDefinitions::exInstanceId); GwXDRMappingDef xdr; + //we send a battery message - so it is category battery xdr.category=GwXDRCategory::XDRBAT; + //we only need a conversion from N2K to NMEA0183 xdr.direction=GwXDRMappingDef::Direction::M_FROM2K; - xdr.field=0; //refer to xdrconfig.json to pick up the field id (the index in the field list) - in this case 0 for Voltage - xdr.selector=0; //refer to xdrconfig.json - there is no selector under Battery, so we can leave it empty - xdr.instanceMode=GwXDRMappingDef::IS_SINGLE; //we just map exactly our instance + //you can find the XDR field and selector definitions + //in the generated GwXdrTypeMappings.h + xdr.field=GWXDRFIELD_BATTERY_BATTERYVOLTAGE; + //xdr.selector=-1; //refer to xdrconfig.json - there is no selector under Battery, so we can leave it empty + //we just map exactly our instance + xdr.instanceMode=GwXDRMappingDef::IS_SINGLE; + + //those are the user configured values + //this instance is the one we use for the battery instance when we set up + //the N2K message xdr.instanceId=instance; xdr.xdrName=voltageTransducer; if (!api->addXdrMapping(xdr)){