diff --git a/Dockerfile b/Dockerfile index b91d2f8..f6a71dc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,4 +10,5 @@ RUN apt -y update && \ emacs nano \ cmake git \ gdb gdbserver && \ - pip3 install ampy rshell + pip3 install adafruit-ampy rshell + diff --git a/src/CustomTemplate.cpp b/src/CustomTemplate.cpp deleted file mode 100644 index 5480381..0000000 --- a/src/CustomTemplate.cpp +++ /dev/null @@ -1,22 +0,0 @@ - -#include "CustomTemplate.h" - -std::string DataTypeValue::render() { - - std::string name = "dataTypeMode"; - std::shared_ptr mode = this->tpl->valueByName[name]; - std::string val; - if (mode) val = mode->render(); - return val; -}; - -bool DataTypeValue::isTrue() const { - return true; //!values.empty(); -}; - -DataTypeTemplate &DataTypeTemplate::setValue( std::string name, DataTypeValue value ) { - - valueByName[ name ] = std::make_shared( std::move(value) ); - std::shared_ptr value = valueByName["dataTypeMode"]; - return *this; -} ; diff --git a/src/CustomTemplate.h b/src/CustomTemplate.h deleted file mode 100644 index 488ee69..0000000 --- a/src/CustomTemplate.h +++ /dev/null @@ -1,28 +0,0 @@ - -#ifndef __CUSTOM_TEMPLATE_H__ -#define __CUSTOM_TEMPLATE_H__ - -#include "Jinja2CppLight.h" -#include "DataTypeCatalog.h" - -class DataTypeTemplate; - -class DataTypeValue : public Jinja2CppLight::Value { - public: - DataTypeDefinition *data; - DataTypeTemplate *tpl; - - DataTypeValue( DataTypeDefinition* _data ) { - this->data = _data; - } - - virtual std::string render(); - bool isTrue() const; -}; - -class DataTypeTemplate : public Jinja2CppLight::Template { - - DataTypeTemplate &setValue( std::string name, DataTypeValue value ); -}; - -#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 2e07c52..835e321 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,9 +35,10 @@ std::string buildSelfParams(DataTypeDefinition *dataDef); void buildDataCatalog(const char* baseType, const char* typeDir); -void createPythonArtifacts(std::string templateFilename); -void createPythonArtifact(std::string filename, DataTypeDefinition *typeDef); +void createPythonArtifacts(std::string outDir, std::string templateFilename); +void createPythonArtifact(std::string outDir, std::string filename, DataTypeDefinition *typeDef); +void build_DTI_table_for_type(std::ofstream &file, string baseType, int index, bool isSubType); std::map g_typeLocatorMap; @@ -110,12 +111,40 @@ extern "C" int main (int argc, char **argv) g_DataTypeCatalog.print(); std::string filename = templateName; - createPythonArtifacts(filename); + createPythonArtifacts(std::string(outDir),filename); + std::ofstream dti_file; + std::string dti_filename = std::string(outDir) + "/" + std::string(baseType) + ".dti"; + dti_file.open (dti_filename); + build_DTI_table_for_type(dti_file, baseType, 0, false); + dti_file.close(); cout << "\r\n End \r\n"; }; +void build_DTI_table_for_type(std::ofstream &file, string baseType, int index, bool isSubType) { + + int initialIndex = index; + int subIndex = 0; + + std::map dataMap = g_DataTypeCatalog.getDataTypeCatalog(); + + DataTypeDefinition *type = dataMap.at(string(baseType)); + + std::list fields = type->getFields(); + for (auto const& i : fields) { + + if (isSubType) { subIndex++; } + else if (initialIndex == 0) { index++; } + + file << index << ", " << subIndex << ", " << i.typeName << ", " << + i.typePrefix << ", " << i.valueName << ", " << i.valueType << "\r\n"; + + if (i.valueType == rt_ros) build_DTI_table_for_type(file, i.typeName, index, true); + + } +} + void buildDataCatalog(const char* baseType, const char* typeDir) { g_DataTypeCatalog.addDataTypeDefinition(new DataTypeDefinition(baseType, "")); @@ -136,23 +165,30 @@ void buildDataCatalog(const char* baseType, const char* typeDir) { } } -void createPythonArtifacts(std::string templateFilename) { +void createPythonArtifacts(std::string outDir, std::string templateFilename) { std::map dataMap = g_DataTypeCatalog.getDataTypeCatalog(); - for (auto typeDef : dataMap) { - createPythonArtifact(templateFilename, typeDef.second); + for (auto dataMapEntry : dataMap) { + + DataTypeDefinition *typeDef = dataMapEntry.second; + createPythonArtifact(outDir, templateFilename, typeDef); + } }; -void createPythonArtifact(std::string filename, DataTypeDefinition *typeDef) { +void createPythonArtifact(std::string outDir, std::string filename, DataTypeDefinition *typeDef) { + std::ifstream t(filename, std::ios::binary); + if (t.is_open()) { t.seekg(0, std::ios::end); size_t size = t.tellg(); std::string buffer(size, ' '); t.seekg(0); t.read(&buffer[0], size); + t.close(); + Jinja2CppLight::Template mytemplate(buffer); mytemplate.setValue("type", typeDef->getTypeName()); @@ -161,7 +197,11 @@ void createPythonArtifact(std::string filename, DataTypeDefinition *typeDef) { mytemplate.setValue("selfParams", buildSelfParams(typeDef)); string result = mytemplate.render(); - cout << result; + + std::string outFilename = outDir + "/" + typeDef->getTypeName() + ".py"; + std::ofstream outFile(outFilename); + outFile << result; + outFile.close(); } } diff --git a/test/output/Twist.dti b/test/output/Twist.dti new file mode 100644 index 0000000..8a9ac5b --- /dev/null +++ b/test/output/Twist.dti @@ -0,0 +1,8 @@ +1, 0, Vector3, , linear, 117 +1, 1, float64, , x, 114 +1, 2, float64, , y, 114 +1, 3, float64, , z, 114 +2, 0, Vector3, , angular, 117 +2, 1, float64, , x, 114 +2, 2, float64, , y, 114 +2, 3, float64, , z, 114 diff --git a/test/output/Twist.py b/test/output/Twist.py new file mode 100644 index 0000000..e5316b9 --- /dev/null +++ b/test/output/Twist.py @@ -0,0 +1,17 @@ + +From Vector3 import Vector3 + + +class Twist: + def __init__(self, linear:Vector3, angular:Vector3): + self.linear:Vector3 = linear + self.angular:Vector3 = angular + + + # + # Marshal data to ROS + # + def publishToROS(self, endPoint:str): + pass + + diff --git a/test/output/Vector3.py b/test/output/Vector3.py new file mode 100644 index 0000000..2e27ef7 --- /dev/null +++ b/test/output/Vector3.py @@ -0,0 +1,18 @@ + +From float64 import float64 + + +class Vector3: + def __init__(self, x:float64, y:float64, z:float64): + self.x:float64 = x + self.y:float64 = y + self.z:float64 = z + + + # + # Marshal data to ROS + # + def publishToROS(self, endPoint:str): + pass + + diff --git a/test/outputTwist.dti b/test/outputTwist.dti new file mode 100644 index 0000000..8a9ac5b --- /dev/null +++ b/test/outputTwist.dti @@ -0,0 +1,8 @@ +1, 0, Vector3, , linear, 117 +1, 1, float64, , x, 114 +1, 2, float64, , y, 114 +1, 3, float64, , z, 114 +2, 0, Vector3, , angular, 117 +2, 1, float64, , x, 114 +2, 2, float64, , y, 114 +2, 3, float64, , z, 114