Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Portar GUI a QT #54

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Firmware/aire_apollo/.vscode/*
.vscode/*
metrics/__pycache__/*
**/__pycache__/

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
100 changes: 100 additions & 0 deletions Software/qt_gui_json/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QTimer
import pyqtgraph as pg
from random import random, randint
# from plot import Plot
from window import MainWindow
import sys
import functools
from itertools import count
import paho.mqtt.client as mqtt

'''
Example data received
raw_data = b'{ "type" : "ventilatorConfig", "rpm" : 15,"pMax" : 50.0,"pPeak" : 45.0,"pPEEP" : 10,"vTidal" : 400,"ieRatio" : 0.33,"iRiseTime" : 1.0}
'''
#TODO: change from bytestring to string

class Plot(pg.PlotWidget):
def __init__(self, index):
super().__init__()
self.index = index
self.pseudotime = count()
self.buffer = 100
self.x = [next(self.pseudotime)]
self.y = [randint(0,8)]
print("Initialized ", self.x, self.y)
self.setBackground('w')
pen = pg.mkPen(color=(255, 0, 0))
self.data_line = self.plot(self.x, self.y, pen=pen)

def plot_graph(self, data):
self.x.append(next(self.pseudotime))
self.y.append(data)
print("Updated ", self.x, self.y)
if len(self.x) > self.buffer:
# print('buffer length achieved')
self.x = self.x[1:]
self.y = self.y[1:]
self.data_line.setData(self.x, self.y)



def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("topic/test")

def on_message(client, userdata, msg):
print(msg.payload.decode())
plot_graphs(unpg, otropg, yotro, otrouanmortaim)

def plot_graphs(plot1,plot2,plot3,plot4):
raw_data = {
"type" : "ventilatorConfig",
"rpm" : random(),
"pMax" : random(),
"pPeak" : random(),
"pPEEP" : random(),
"vTidal" : random(),
"ieRatio" : random(),
"iRiseTime" : random()
}
print('plot_graphs')

plot1.plot_graph(raw_data['pMax'])
plot2.plot_graph(raw_data['pPeak'])
plot3.plot_graph(raw_data['pPEEP'])
plot4.plot_graph(raw_data['rpm'])


if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
broker_ip = "192.168.1.39" # TODO: parameterize if necessary

unpg = Plot(0)
otropg = Plot(1)
yotro = Plot(2)
otrouanmortaim = Plot(3)

window.layout.addWidget(unpg)
window.layout.addWidget(otropg)
window.layout.addWidget(yotro)
window.layout.addWidget(otrouanmortaim)
# timer = QTimer()
# timer.setInterval(100)
# plot_process = functools.partial(plot_graphs,plot1=unpg, plot2=otropg, plot3=yotro,plot4=otrouanmortaim)
# timer.timeout.connect(plot_process)
# timer.start()

client = mqtt.Client()
client.connect(broker_ip,1883,60)

client.on_connect = on_connect
client.on_message = on_message
client.loop_forever()

window.show()

sys.exit(app.exec_())
5 changes: 5 additions & 0 deletions Software/qt_gui_json/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
PyQt5 # pip install PyQt5==5.10
PyQt5-sip
pyqtgraph
pyserial
python-socketio
22 changes: 22 additions & 0 deletions Software/qt_gui_json/window.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *


class MainWindow(QWidget):

def __init__(self):
super().__init__()

self.title = 'Apollo Ventilator App'
self.left = 100
self.top = 100
self.width = 720
self.height = 500
self.initUI()

def initUI(self):

self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top,self.width,self.height)
self.layout = QGridLayout(self)
45 changes: 45 additions & 0 deletions Software/qt_gui_matplotlib_poc2/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env python

from PyQt5 import QtWidgets, uic

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
import matplotlib.pyplot as plt

import sys


class GUI(QtWidgets.QMainWindow):
__doc__ = \
"""
From:
- https://nitratine.net/blog/post/how-to-import-a-pyqt5-ui-file-in-a-python-gui/
- https://stackoverflow.com/questions/12459811/how-to-embed-matplotlib-in-pyqt-for-dummies
"""

def __init__(self):
super(GUI, self).__init__()
uic.loadUi('app.ui', self)
# Configure matplotlib figure > attach to FigureCanvas > attach to NavigationToolbar
self.matplot_figure = plt.Figure()
self.matplot_canvas = FigureCanvas(self.matplot_figure)

self.qt_plot_widget = self.findChild(QtWidgets.QWidget, 'plot_widget')

self.show()


class App(object):
__doc__ = """"""

def __init__(self):
self.qt_app = QtWidgets.QApplication(sys.argv)
self.ui = GUI()

def run(self):
self.qt_app.exec_()


if __name__ == "__main__":
app = App()
app.run()
46 changes: 46 additions & 0 deletions Software/qt_gui_matplotlib_poc2/app.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1175</width>
<height>640</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="MainWidget">
<widget class="QWidget" name="gridLayoutWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>1151</width>
<height>621</height>
</rect>
</property>
<layout class="QGridLayout" name="MainGrid">
<item row="0" column="0">
<widget class="QWidget" name="DrawWidget" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="0" column="1">
<layout class="QFormLayout" name="MainForm"/>
</item>
</layout>
</widget>
</widget>
</widget>
<resources/>
<connections/>
</ui>
8 changes: 8 additions & 0 deletions Software/qt_gui_matplotlib_poc2/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# PyQt5==5.14.0
# PyQt5-sip
# PyQtChart
# pyqtgraph
# pyserial

matplotlib==3.2.1
numpy==1.18.2
Empty file.
41 changes: 41 additions & 0 deletions Software/qt_gui_pyqtgraph/classes/plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import pyqtgraph as pg
import sys
from random import randint
from .utils import serial_ports_get
from PyQt5.QtCore import QTimer


class CustomWidget(pg.PlotWidget):
def __init__(self, index):
super().__init__()
self.index = index

self.x = list(range(0,200))
self.y = [randint(0,8) for _ in range(200)]
self.setBackground('w')
pen = pg.mkPen(color=(255, 0, 0))
self.data_line = self.plot(self.x, self.y, pen=pen)
# self.timer = QTimer()
# self.timer.setInterval(2)
# self.timer.timeout.connect(self.update_plot_data)
# self.timer.start()

def plot_graph(self):
self.data_line.setData(self.x, self.y)


def update_plot_data(self, data):
self.x = self.x[1:] # Remove the first y element.
self.x.append(self.x[-1] + 1) # Add a new value 1 higher than the last.

self.y = self.y[1:] # Remove the first
# if len(selfdata) > 0:
try:
self.y.append(data)
# self.data_line.setData(self.x, self.y)
except IndexError:
print('error getting data ')
except ValueError:
print('error converting data ')
except:
print('other kind of error ', data, sys.exc_info()[0])
45 changes: 45 additions & 0 deletions Software/qt_gui_pyqtgraph/classes/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import serial
import socketio
import sys
import glob


def serial_ports_get():
if sys.platform.startswith('win'):
ports = ['COM%s' % (i + 1) for i in range(256)]
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
ports = glob.glob('/dev/tty[A-Za-z]*')
elif sys.platform.startswith('darwin'):
ports = glob.glob('/dev/tty.*')
else:
raise EnvironmentError('Unsupported platform')
result = []
for port in ports:
try:
s = serial.Serial(port)
s.close()
result.append(port)
except (OSError, serial.SerialException):
pass
return result


def filter_data_from_serial_port(self):
if self.serial_port.read() == b'D':
serial_line = b'D' + self.serial_port.readline()
serial_data = str(serial_line).split(':')
print(serial_data)
return serial_data
else:
return []


def filter_data_from_socketio():
serial_data = []
@s.on('serialPortData')
def on_message(data):
if 'DATA' in data:
serial_line = data.split("'")[3]
serial_data = str(serial_line).split(':')
print(data)
return serial_data
27 changes: 27 additions & 0 deletions Software/qt_gui_pyqtgraph/classes/window.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *


class MainWindow(QWidget):

def __init__(self):
super().__init__()

self.title = 'Apollo Ventilator App'
self.left = 100
self.top = 100
self.width = 720
self.height = 500
self.initUI()

def initUI(self):

self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top,self.width,self.height)
self.layout = QGridLayout(self)

# self.layout.addWidget(self.unpg)
# self.layout.addWidget(self.otropg)
# self.layout.addWidget(self.yotro)
# self.layout.addWidget(self.otrouanmortaim)
Loading