[python] improve create_module with ABI bindings

This commit is contained in:
Gautier Hattenberger
2020-04-02 23:32:47 +02:00
committed by Freek van Tienen
parent b2c947fa10
commit ff29e8b4dd
8 changed files with 220 additions and 9 deletions
+2 -1
View File
@@ -1,10 +1,11 @@
all:
pyuic5 create_module_ui.ui -o create_module_ui.py
pyuic5 datalink_ui.ui -o datalink_ui.py
pyuic5 abi_ui.ui -o abi_ui.py
pyuic5 event_ui.ui -o event_ui.py
pyuic5 init_ui.ui -o init_ui.py
pyuic5 periodic_ui.ui -o periodic_ui.py
clean_gui:
rm create_module_ui.py datalink_ui.py event_ui.py init_ui.py periodic_ui.py
rm create_module_ui.py datalink_ui.py abi_ui.py event_ui.py init_ui.py periodic_ui.py
+64
View File
@@ -0,0 +1,64 @@
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'abi_ui.ui'
#
# Created by: PyQt5 UI code generator 5.10.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Abi(object):
def setupUi(self, Abi):
Abi.setObjectName("Abi")
Abi.resize(753, 88)
self.verticalLayout = QtWidgets.QVBoxLayout(Abi)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.label_11 = QtWidgets.QLabel(Abi)
self.label_11.setStyleSheet("font: bold")
self.label_11.setAlignment(QtCore.Qt.AlignCenter)
self.label_11.setObjectName("label_11")
self.horizontalLayout.addWidget(self.label_11)
self.remove_button = QtWidgets.QToolButton(Abi)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("remove.svg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.remove_button.setIcon(icon)
self.remove_button.setObjectName("remove_button")
self.horizontalLayout.addWidget(self.remove_button)
self.verticalLayout.addLayout(self.horizontalLayout)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.horizontalLayout_20 = QtWidgets.QHBoxLayout()
self.horizontalLayout_20.setObjectName("horizontalLayout_20")
self.label_16 = QtWidgets.QLabel(Abi)
self.label_16.setObjectName("label_16")
self.horizontalLayout_20.addWidget(self.label_16)
self.abi_edit = QtWidgets.QLineEdit(Abi)
self.abi_edit.setObjectName("abi_edit")
self.horizontalLayout_20.addWidget(self.abi_edit)
self.horizontalLayout_2.addLayout(self.horizontalLayout_20)
self.horizontalLayout_19 = QtWidgets.QHBoxLayout()
self.horizontalLayout_19.setObjectName("horizontalLayout_19")
self.label_15 = QtWidgets.QLabel(Abi)
self.label_15.setObjectName("label_15")
self.horizontalLayout_19.addWidget(self.label_15)
self.message_combo = QtWidgets.QComboBox(Abi)
self.message_combo.setEditable(True)
self.message_combo.setObjectName("message_combo")
self.horizontalLayout_19.addWidget(self.message_combo)
self.horizontalLayout_2.addLayout(self.horizontalLayout_19)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.retranslateUi(Abi)
QtCore.QMetaObject.connectSlotsByName(Abi)
def retranslateUi(self, Abi):
_translate = QtCore.QCoreApplication.translate
Abi.setWindowTitle(_translate("Abi", "Datalink"))
self.label_11.setText(_translate("Abi", "Abi Message"))
self.remove_button.setText(_translate("Abi", "remove"))
self.label_16.setText(_translate("Abi", "base name"))
self.label_15.setText(_translate("Abi", "Message"))
+85
View File
@@ -0,0 +1,85 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Abi</class>
<widget class="QWidget" name="Abi">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>753</width>
<height>88</height>
</rect>
</property>
<property name="windowTitle">
<string>Datalink</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label_11">
<property name="styleSheet">
<string notr="true">font: bold</string>
</property>
<property name="text">
<string>Abi Message</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="remove_button">
<property name="text">
<string>remove</string>
</property>
<property name="icon">
<iconset>
<normaloff>remove.svg</normaloff>remove.svg</iconset>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_20">
<item>
<widget class="QLabel" name="label_16">
<property name="text">
<string>base name</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="abi_edit"/>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_19">
<item>
<widget class="QLabel" name="label_15">
<property name="text">
<string>Message</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="message_combo">
<property name="editable">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
+3 -1
View File
@@ -3,7 +3,7 @@ from init_ui import Ui_Init
from periodic_ui import Ui_Periodic
from event_ui import Ui_Event
from datalink_ui import Ui_Datalink
from abi_ui import Ui_Abi
class ComponentWidget(QtWidgets.QFrame):
def __init__(self, comp_type):
@@ -18,4 +18,6 @@ class ComponentWidget(QtWidgets.QFrame):
self.ui = Ui_Periodic()
if self.comp_type == "Datalink":
self.ui = Ui_Datalink()
if self.comp_type == "Abi":
self.ui = Ui_Abi()
self.ui.setupUi(self)
+33 -1
View File
@@ -23,12 +23,13 @@ class CreateModGUI(Ui_CreateModule_Window):
self.components = []
def build(self):
self.scrollAreaWidgetContents.setStyleSheet("QFrame#Init, QFrame#Datalink, QFrame#Periodic,QFrame#Event { border: 2px solid grey}")
self.scrollAreaWidgetContents.setStyleSheet("QFrame#Init, QFrame#Datalink, QFrame#Periodic,QFrame#Event,QFrame#Abi { border: 2px solid grey}")
self.create_button.clicked.connect(self.create_mod)
self.init_button.clicked.connect(lambda: self.add_component("Init"))
self.periodic_button.clicked.connect(lambda: self.add_component("Periodic"))
self.event_button.clicked.connect(lambda: self.add_component("Event"))
self.datalink_button.clicked.connect(lambda: self.add_component("Datalink"))
self.abi_button.clicked.connect(lambda: self.add_component("Abi"))
self.directory_combo.addItem("")
for directory in sorted([f.name for f in os.scandir(PPRZ_SRC + "/sw/airborne/modules") if f.is_dir()]):
self.directory_combo.addItem(directory)
@@ -45,6 +46,24 @@ class CreateModGUI(Ui_CreateModule_Window):
return messages
return []
@staticmethod
def get_abi_messages(msg_name=None):
abi_xml = PPRZ_HOME + "/conf/abi.xml"
root = etree.parse(abi_xml).getroot()
classes = root.findall("msg_class")
for c in classes:
if c.attrib["name"] == "airborne":
msgs = c.findall("message")
if msg_name is None:
messages = [m.attrib["name"] for m in msgs]
return messages
else:
for m in msgs:
if m.attrib["name"] == msg_name:
fields = [(f.attrib["name"], f.attrib["type"]) for f in m.findall("field")]
return fields
return []
def add_component(self, comp_name):
component = components.ComponentWidget(comp_name)
if component.comp_type == "Datalink":
@@ -52,6 +71,11 @@ class CreateModGUI(Ui_CreateModule_Window):
component.ui.message_combo.addItem("")
for m in messages:
component.ui.message_combo.addItem(m)
if component.comp_type == "Abi":
messages = self.get_abi_messages()
component.ui.message_combo.addItem("")
for m in messages:
component.ui.message_combo.addItem(m)
component.ui.remove_button.clicked.connect(lambda: self.remove_component(component))
self.scroll_area_layout.insertWidget(self.scroll_area_layout.count()-1, component)
self.components.append(component)
@@ -117,6 +141,14 @@ class CreateModGUI(Ui_CreateModule_Window):
self.statusbar.setStyleSheet("background-color:rgb(255,148,148);")
return
fc.add_datalink(datalink, msg)
if comp.comp_type == "Abi":
abi_base = comp.ui.abi_edit.text()
msg = comp.ui.message_combo.currentText()
if abi_base == "" or msg == "":
self.statusbar.showMessage("Please fill the Abi Message base name and Message fields!")
self.statusbar.setStyleSheet("background-color:rgb(255,148,148);")
return
fc.add_abi(abi_base, msg, self.get_abi_messages(msg_name=msg))
self.statusbar.setStyleSheet("background-color:rgb(155,232,155);")
licence = self.licence_comboBox.currentText()
files = fc.get_filenames()
+6 -2
View File
@@ -11,7 +11,7 @@ from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_CreateModule_Window(object):
def setupUi(self, CreateModule_Window):
CreateModule_Window.setObjectName("CreateModule_Window")
CreateModule_Window.resize(875, 553)
CreateModule_Window.resize(875, 700)
self.centralwidget = QtWidgets.QWidget(CreateModule_Window)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
@@ -99,7 +99,7 @@ class Ui_CreateModule_Window(object):
self.components_area.setWidgetResizable(True)
self.components_area.setObjectName("components_area")
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 859, 209))
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 859, 356))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
self.scroll_area_layout = QtWidgets.QVBoxLayout(self.scrollAreaWidgetContents)
self.scroll_area_layout.setObjectName("scroll_area_layout")
@@ -124,6 +124,9 @@ class Ui_CreateModule_Window(object):
self.datalink_button = QtWidgets.QPushButton(self.centralwidget)
self.datalink_button.setObjectName("datalink_button")
self.horizontalLayout_7.addWidget(self.datalink_button)
self.abi_button = QtWidgets.QPushButton(self.centralwidget)
self.abi_button.setObjectName("abi_button")
self.horizontalLayout_7.addWidget(self.abi_button)
spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_7.addItem(spacerItem1)
self.verticalLayout.addLayout(self.horizontalLayout_7)
@@ -158,5 +161,6 @@ class Ui_CreateModule_Window(object):
self.periodic_button.setText(_translate("CreateModule_Window", "Periodic"))
self.event_button.setText(_translate("CreateModule_Window", "Event"))
self.datalink_button.setText(_translate("CreateModule_Window", "Datalink"))
self.abi_button.setText(_translate("CreateModule_Window", "Abi Message"))
self.create_button.setText(_translate("CreateModule_Window", "Create Module"))
+10 -3
View File
@@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>875</width>
<height>553</height>
<height>700</height>
</rect>
</property>
<property name="windowTitle">
@@ -165,7 +165,7 @@
<x>0</x>
<y>0</y>
<width>859</width>
<height>209</height>
<height>356</height>
</rect>
</property>
<layout class="QVBoxLayout" name="scroll_area_layout">
@@ -188,7 +188,7 @@
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_7" stretch="0,0,0,0,0,0">
<layout class="QHBoxLayout" name="horizontalLayout_7" stretch="0,0,0,0,0,0,0">
<item>
<widget class="QLabel" name="label_6">
<property name="text">
@@ -224,6 +224,13 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="abi_button">
<property name="text">
<string>Abi Message</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
+17 -1
View File
@@ -36,6 +36,7 @@ Periodic = namedtuple('Periodic', ['fun', 'freq', 'start', 'stop', 'autorun'])
Event = namedtuple('Event', ['fun'])
Init = namedtuple('Init', ['fun'])
Datalink = namedtuple('Datalink', ['fun', 'message', 'fun_c'])
Abi = namedtuple('Abi', ['base', 'message', 'params'])
class FilesCreate:
@@ -52,6 +53,7 @@ class FilesCreate:
self.periodics = []
self.events = []
self.datalinks = []
self.abi_bindings = []
@property
def name(self):
@@ -123,6 +125,11 @@ class FilesCreate:
fun_c = fun + "(uint8_t* buf)"
self.datalinks.append(Datalink(fun=fun_xml, message=message, fun_c=fun_c))
def add_abi(self, base, message, fields):
fields = [("sender_id", "uint8_t")] + fields
params = ", ".join(["{} {}".format(t, f) for (f, t) in fields])
self.abi_bindings.append(Abi(base=base, message=message, params=params))
def build_xml(self):
# set name
self.xml.attrib["name"] = self.name
@@ -182,8 +189,17 @@ class FilesCreate:
dir=self.directory, name=self.name, author=self.author, email=self.email, description=self.description)
include = "#include \"modules/{dir}/{name}.h\"".format(dir=self.directory, name=self.name)
declarations = ""
for abi_binding in self.abi_bindings:
declarations += "#ifndef {}_ID\n#define {}_ID ABI_BROADCAST\n#endif\n\n".format(abi_binding.base.upper(), abi_binding.base.upper())
declarations += "static abi_event {}_ev;\n\n".format(abi_binding.base)
declarations += "static void {}_cb({})\n{{\n // your abi callback code here\n}}\n\n".format(abi_binding.base, abi_binding.params)
for init in self.inits:
declarations += "void {}(void)\n{{\n // your init code here\n}}\n\n".format(init.fun)
declarations += "void {}(void)\n{{\n // your init code here\n".format(init.fun)
if len(self.abi_bindings) > 0:
declarations += "\n // Abi messages bindings\n"
for abi_binding in self.abi_bindings:
declarations += " AbiBindMsg{0}({1}_ID, &{2}_ev, {2}_cb);\n".format(abi_binding.message, abi_binding.base.upper(), abi_binding.base)
declarations += "}\n\n"
for periodic in self.periodics:
declarations += "void {}(void)\n{{\n // your periodic code here.\n // freq = {} Hz\n}}\n\n".format(
periodic.fun, periodic.freq)