Use QML
This commit is contained in:
parent
53b81d6ae4
commit
6977daaca1
@ -1,88 +1,41 @@
|
||||
import enum
|
||||
import csv
|
||||
import os.path
|
||||
import time
|
||||
from typing import cast
|
||||
|
||||
from PySide6.QtCore import QTimer
|
||||
from PySide6.QtWidgets import (
|
||||
QWidget,
|
||||
QGridLayout,
|
||||
QHBoxLayout,
|
||||
QVBoxLayout,
|
||||
QLabel,
|
||||
QPushButton,
|
||||
QFileDialog,
|
||||
)
|
||||
from PySide6.QtCharts import QChart, QChartView, QLineSeries
|
||||
from PySide6.QtCore import QTimer, QObject
|
||||
from PySide6.QtWidgets import QFileDialog
|
||||
from PySide6.QtQml import QQmlApplicationEngine
|
||||
|
||||
from .profile_handler import ProfileHandler, ProfileState
|
||||
|
||||
|
||||
class GUI(QWidget):
|
||||
class GUI(QObject):
|
||||
_profile_handler: ProfileHandler
|
||||
_engine: QQmlApplicationEngine
|
||||
_win: QObject
|
||||
_update_timer: QTimer
|
||||
_profile_chooser_btn: QObject
|
||||
_start_pause_btn: QObject
|
||||
_stop_btn: QObject
|
||||
|
||||
def __init__(self, profile_handler: ProfileHandler):
|
||||
super().__init__(None)
|
||||
|
||||
profile_label = QLabel("Profile:")
|
||||
self.profile = QLabel("None")
|
||||
self.profile.setStyleSheet("font-style: italic")
|
||||
self.profile_chooser = QPushButton("Choose")
|
||||
self.profile_chooser.clicked.connect(self._choose_profile)
|
||||
profile_choice = QHBoxLayout()
|
||||
profile_choice.addWidget(profile_label)
|
||||
profile_choice.addWidget(self.profile)
|
||||
profile_choice.addWidget(self.profile_chooser)
|
||||
self._engine = QQmlApplicationEngine()
|
||||
qml_path = os.path.join(
|
||||
os.path.dirname(__file__), "..", "ui", "main_window.qml"
|
||||
)
|
||||
self._engine.load(qml_path)
|
||||
self._win = self._engine.rootObjects()[0]
|
||||
self._win.setProperty("profile_handler", profile_handler)
|
||||
|
||||
chart_view = QChartView()
|
||||
self.chart = QChart()
|
||||
chart_view.setChart(self.chart)
|
||||
|
||||
self.start_pause = QPushButton("START")
|
||||
self.start_pause.setDisabled(True)
|
||||
self.start_pause.clicked.connect(self._start_pause)
|
||||
self.stop = QPushButton("STOP")
|
||||
self.stop.setDisabled(True)
|
||||
self.stop.clicked.connect(self._stop)
|
||||
profile_buttons = QHBoxLayout()
|
||||
profile_buttons.addWidget(self.start_pause)
|
||||
profile_buttons.addWidget(self.stop)
|
||||
|
||||
profile_layout = QVBoxLayout()
|
||||
profile_layout.addLayout(profile_choice)
|
||||
profile_layout.addWidget(chart_view)
|
||||
profile_layout.addLayout(profile_buttons)
|
||||
|
||||
current_label = QLabel("Current [A]:")
|
||||
self.current = QLabel("0")
|
||||
time_label = QLabel("Time [min]:")
|
||||
self.time = QLabel("00:00 / 00:00")
|
||||
self.time.setStyleSheet("font-family: monospace")
|
||||
voltage_label = QLabel("Voltage [V]:")
|
||||
self.voltage = QLabel("[0.00, 0.00]")
|
||||
temperature_label = QLabel("Temperature [°C]:")
|
||||
self.temperature = QLabel("[00.0, 00.0]")
|
||||
|
||||
info_layout = QGridLayout()
|
||||
info_layout.addWidget(current_label, 0, 0)
|
||||
info_layout.addWidget(self.current, 0, 1)
|
||||
info_layout.addWidget(time_label, 1, 0)
|
||||
info_layout.addWidget(self.time, 1, 1)
|
||||
info_layout.addWidget(voltage_label, 2, 0)
|
||||
info_layout.addWidget(self.voltage, 2, 1)
|
||||
info_layout.addWidget(temperature_label, 3, 0)
|
||||
info_layout.addWidget(self.temperature, 3, 1)
|
||||
|
||||
layout = QHBoxLayout()
|
||||
layout.addLayout(profile_layout)
|
||||
layout.addLayout(info_layout)
|
||||
|
||||
self.setLayout(layout)
|
||||
self.setWindowTitle("Load Controller")
|
||||
self._profile_chooser_btn = self._win.findChild(QObject, "profile_chooser")
|
||||
self._profile_chooser_btn.clicked.connect(self._choose_profile)
|
||||
|
||||
self._start_pause_btn = self._win.findChild(QObject, "start_pause")
|
||||
self._start_pause_btn.clicked.connect(self._start_pause)
|
||||
self._stop_btn = self._win.findChild(QObject, "stop")
|
||||
self._stop_btn.clicked.connect(self._stop)
|
||||
self._profile_handler = profile_handler
|
||||
self._profile_handler.current_changed.connect(self._update_current)
|
||||
self._profile_handler.finished.connect(self._stop)
|
||||
|
||||
self._update_timer = QTimer()
|
||||
@ -90,7 +43,7 @@ class GUI(QWidget):
|
||||
self._update_timer.start(100)
|
||||
|
||||
def _choose_profile(self):
|
||||
dlg = QFileDialog(parent=self)
|
||||
dlg = QFileDialog()
|
||||
dlg.setFileMode(QFileDialog.FileMode.ExistingFile)
|
||||
dlg.setNameFilter("Profiles (*.csv)")
|
||||
if not dlg.exec():
|
||||
@ -98,58 +51,31 @@ class GUI(QWidget):
|
||||
|
||||
profile_path = dlg.selectedFiles()[0]
|
||||
print(f"Profile: {profile_path}")
|
||||
self.profile.setText(profile_path)
|
||||
self.profile.setStyleSheet("font-style: normal")
|
||||
self._profile_handler.load_profile(profile_path)
|
||||
|
||||
series = QLineSeries()
|
||||
for t, i in self._profile_handler.load_profile(profile_path):
|
||||
series.append(t, i)
|
||||
# with open(profile_path) as fh:
|
||||
# t = 0
|
||||
# for l in csv.reader(fh):
|
||||
# i = float(l[0])
|
||||
# series.append(t, i)
|
||||
# t += float(l[1])
|
||||
# series.append(t, i)
|
||||
# series.append()
|
||||
self.chart.removeAllSeries()
|
||||
self.chart.addSeries(series)
|
||||
self.chart.createDefaultAxes()
|
||||
self.start_pause.setDisabled(False)
|
||||
|
||||
def _update_current(self, current: float):
|
||||
self.current.setText(f"{current:02.1f}")
|
||||
self._win.setProperty("profile", profile_path)
|
||||
self._win.findChild(QObject, "start_pause").setProperty("enabled", True)
|
||||
|
||||
def _start_pause(self):
|
||||
if self._profile_handler.state == ProfileState.STOPPED:
|
||||
self._profile_handler.start()
|
||||
self.start_pause.setText("PAUSE")
|
||||
self.stop.setDisabled(False)
|
||||
self.profile_chooser.setDisabled(True)
|
||||
self._start_pause_btn.setProperty("text", "PAUSE")
|
||||
self._stop_btn.setProperty("enabled", True)
|
||||
self._profile_chooser_btn.setProperty("enabled", False)
|
||||
# TODO: Start logging
|
||||
elif self._profile_handler.state == ProfileState.RUNNING:
|
||||
self._profile_handler.pause()
|
||||
self.start_pause.setText("RESUME")
|
||||
self._start_pause_btn.setProperty("text", "RESUME")
|
||||
else:
|
||||
self._profile_handler.resume()
|
||||
self.start_pause.setText("PAUSE")
|
||||
self._start_pause_btn.setProperty("text", "PAUSE")
|
||||
|
||||
def _stop(self):
|
||||
self.start_pause.setText("START")
|
||||
self.stop.setDisabled(True)
|
||||
self.profile_chooser.setDisabled(False)
|
||||
self._start_pause_btn.setProperty("text", "START")
|
||||
self._stop_btn.setProperty("enabled", False)
|
||||
self._profile_chooser_btn.setProperty("enabled", True)
|
||||
|
||||
def _update(self):
|
||||
if self._profile_handler.state == ProfileState.RUNNING:
|
||||
dt = time.time() - self._profile_handler.profile_start
|
||||
if len(self.chart.series()) == 1:
|
||||
series = QLineSeries()
|
||||
series.append(dt, 0)
|
||||
series.append(dt, 100)
|
||||
self.chart.addSeries(series)
|
||||
self.chart.createDefaultAxes()
|
||||
else:
|
||||
series = cast(QLineSeries, self.chart.series()[-1])
|
||||
series.removePoints(0, 2)
|
||||
series.append(dt, 0)
|
||||
series.append(dt, 100)
|
||||
self._win.setProperty("profileTime", dt)
|
||||
|
@ -1,7 +1,7 @@
|
||||
import serial
|
||||
import time
|
||||
|
||||
from PySide6.QtCore import QObject
|
||||
from PySide6.QtCore import QObject, Slot, QTimer
|
||||
|
||||
from .profile_handler import ProfileHandler
|
||||
|
||||
@ -11,23 +11,28 @@ class Load(QObject):
|
||||
|
||||
_current: float
|
||||
|
||||
_timer: QTimer
|
||||
_profile_handler: ProfileHandler
|
||||
|
||||
def __init__(self, uart_path: str, profile_handler: ProfileHandler):
|
||||
super().__init__(None)
|
||||
self.dev = serial.Serial(uart_path, 115200)
|
||||
self._current = 0
|
||||
|
||||
profile_handler.current_changed.connect(self._update_current)
|
||||
self._profile_handler = profile_handler
|
||||
self._profile_handler.currentChanged.connect(self._update_current)
|
||||
self._timer = QTimer()
|
||||
self._timer.timeout.connect(self._update_load)
|
||||
self._timer.start(100)
|
||||
|
||||
@Slot()
|
||||
def _update_current(self, current):
|
||||
self._current = current
|
||||
|
||||
def do_work(self):
|
||||
while True:
|
||||
time.sleep(0.1)
|
||||
def _update_load(self):
|
||||
curr_quants = round(self._current / 0.1)
|
||||
assert curr_quants <= 0xFFFF
|
||||
|
||||
curr_quants = round(self._current / 0.1)
|
||||
assert curr_quants <= 0xFFFF
|
||||
|
||||
msb = curr_quants >> 8
|
||||
lsb = curr_quants & 0xFF
|
||||
self.dev.write(bytes((msb, lsb)))
|
||||
msb = curr_quants >> 8
|
||||
lsb = curr_quants & 0xFF
|
||||
self.dev.write(bytes((msb, lsb)))
|
||||
|
@ -12,7 +12,8 @@ class ProfileState(enum.Enum):
|
||||
|
||||
|
||||
class ProfileHandler(QObject):
|
||||
current_changed = Signal(float)
|
||||
currentChanged = Signal(float)
|
||||
profileChanged = Signal(list)
|
||||
finished = Signal()
|
||||
|
||||
state: ProfileState
|
||||
@ -20,7 +21,7 @@ class ProfileHandler(QObject):
|
||||
_timer: QTimer
|
||||
|
||||
profile_start: float
|
||||
_profile: list[tuple[float, float]]
|
||||
_profile: list[list[float]]
|
||||
_pause_time: float
|
||||
_last_current: float
|
||||
|
||||
@ -36,16 +37,17 @@ class ProfileHandler(QObject):
|
||||
self._pause_time = 0
|
||||
self._last_current = 0
|
||||
|
||||
def load_profile(self, path: str) -> list[tuple[float, float]]:
|
||||
def load_profile(self, path: str):
|
||||
with open(path) as fh:
|
||||
result = []
|
||||
t = 0
|
||||
for l in csv.reader(fh):
|
||||
i = float(l[0])
|
||||
result.append((t, i))
|
||||
result.append([t, i])
|
||||
t += float(l[1])
|
||||
result.append((t, i))
|
||||
result.append([t, i])
|
||||
self._profile = result
|
||||
self.profileChanged.emit(result)
|
||||
return result
|
||||
|
||||
def start(self):
|
||||
@ -55,13 +57,13 @@ class ProfileHandler(QObject):
|
||||
self.state = ProfileState.RUNNING
|
||||
current = self._profile[0][1]
|
||||
self._last_current = current
|
||||
self.current_changed.emit(current)
|
||||
self.currentChanged.emit(current)
|
||||
|
||||
def pause(self):
|
||||
assert self.state == ProfileState.RUNNING
|
||||
|
||||
self.state = ProfileState.PAUSED
|
||||
self.current_changed.emit(0)
|
||||
self.currentChanged.emit(0)
|
||||
self._last_current = 0
|
||||
self._pause_time = time.time()
|
||||
|
||||
@ -76,7 +78,7 @@ class ProfileHandler(QObject):
|
||||
assert self.state != ProfileState.STOPPED
|
||||
|
||||
self.state = ProfileState.STOPPED
|
||||
self.current_changed.emit(0)
|
||||
self.currentChanged.emit(0)
|
||||
self._last_current = 0
|
||||
|
||||
def _update(self):
|
||||
@ -85,11 +87,11 @@ class ProfileHandler(QObject):
|
||||
try:
|
||||
current = next(t[1] for t in self._profile if t[0] >= dt)
|
||||
if current != self._last_current:
|
||||
self.current_changed.emit(current)
|
||||
self.currentChanged.emit(current)
|
||||
self._last_current = current
|
||||
except StopIteration:
|
||||
self.state = ProfileState.STOPPED
|
||||
self.finished.emit()
|
||||
else:
|
||||
self._last_current = 0
|
||||
self.current_changed.emit(0)
|
||||
self.currentChanged.emit(0)
|
||||
|
5
main.py
5
main.py
@ -15,13 +15,8 @@ def main(argv: list[str]) -> int:
|
||||
app = QApplication(sys.argv)
|
||||
profile_handler = ProfileHandler()
|
||||
gui = GUI(profile_handler)
|
||||
gui.show()
|
||||
|
||||
load = Load(argv[1], profile_handler)
|
||||
load_thread = QThread()
|
||||
load.moveToThread(load_thread)
|
||||
load_thread.started.connect(load.do_work)
|
||||
load_thread.start()
|
||||
|
||||
return app.exec()
|
||||
|
||||
|
11
requirements.txt
Normal file
11
requirements.txt
Normal file
@ -0,0 +1,11 @@
|
||||
black==22.12.0
|
||||
click==8.1.3
|
||||
mypy-extensions==0.4.3
|
||||
pathspec==0.10.3
|
||||
platformdirs==2.6.2
|
||||
pymodbus==3.0.2
|
||||
pyserial==3.5
|
||||
PySide6==6.4.1
|
||||
PySide6-Addons==6.4.1
|
||||
PySide6-Essentials==6.4.1
|
||||
shiboken6==6.4.1
|
159
ui/main_window.qml
Normal file
159
ui/main_window.qml
Normal file
@ -0,0 +1,159 @@
|
||||
import QtQuick 2.15
|
||||
import QtQuick.Controls 2.15
|
||||
import QtQuick.Layouts 2.15
|
||||
import QtCharts 2.15
|
||||
|
||||
ApplicationWindow {
|
||||
visible: true
|
||||
width: 600
|
||||
height: 500
|
||||
title: "Load Controller"
|
||||
|
||||
property QtObject profile_handler
|
||||
property string profile: "None"
|
||||
property real profileTime: 0
|
||||
property real current: 0
|
||||
property string time: "00:00 / 00:00"
|
||||
property real voltage_min: 0
|
||||
property real voltage_max: 0
|
||||
property real temp_min: 0
|
||||
property real temp_max: 0
|
||||
|
||||
Connections {
|
||||
target: profile_handler
|
||||
|
||||
function onCurrentChanged(new_current) {
|
||||
console.log("currentChanged");
|
||||
current = new_current;
|
||||
}
|
||||
|
||||
function onProfileChanged(data) {
|
||||
profileSeries.clear();
|
||||
var last_x = 0;
|
||||
for (var point of data) {
|
||||
profileSeries.append(point[0], point[1]);
|
||||
last_x = point[0];
|
||||
}
|
||||
axisX.max = last_x;
|
||||
axisX.applyNiceNumbers();
|
||||
}
|
||||
}
|
||||
|
||||
onProfileTimeChanged: {
|
||||
currentTimeSeries.clear()
|
||||
currentTimeSeries.append(profileTime, 0)
|
||||
currentTimeSeries.append(profileTime, 100)
|
||||
}
|
||||
|
||||
RowLayout {
|
||||
id: layout
|
||||
anchors.fill: parent
|
||||
spacing: 6
|
||||
ColumnLayout {
|
||||
RowLayout {
|
||||
Text {
|
||||
text: "Profile:"
|
||||
}
|
||||
Text {
|
||||
text: profile
|
||||
}
|
||||
Button {
|
||||
objectName: "profile_chooser"
|
||||
text: "Choose"
|
||||
}
|
||||
}
|
||||
ChartView {
|
||||
Layout.fillWidth: true
|
||||
Layout.fillHeight: true
|
||||
Layout.minimumWidth: 400
|
||||
Layout.minimumHeight: 400
|
||||
|
||||
antialiasing: true
|
||||
legend.visible: false
|
||||
|
||||
ValueAxis {
|
||||
id: axisX
|
||||
min: 0
|
||||
titleText: "Time [s]"
|
||||
}
|
||||
ValueAxis {
|
||||
id: axisY
|
||||
min: 0
|
||||
max: 100
|
||||
tickCount: 11
|
||||
titleText: "Current [A]"
|
||||
}
|
||||
LineSeries {
|
||||
id: profileSeries
|
||||
axisX: axisX
|
||||
axisY: axisY
|
||||
}
|
||||
LineSeries {
|
||||
id: currentTimeSeries
|
||||
axisX: axisX
|
||||
axisY: axisY
|
||||
XYPoint {x: 0; y: 0}
|
||||
XYPoint {x: 0; y: 100}
|
||||
visible: true
|
||||
}
|
||||
}
|
||||
RowLayout {
|
||||
Button {
|
||||
objectName: "start_pause"
|
||||
text: "START"
|
||||
enabled: false
|
||||
}
|
||||
Button {
|
||||
objectName: "stop"
|
||||
text: "STOP"
|
||||
enabled: false
|
||||
}
|
||||
}
|
||||
}
|
||||
GridLayout {
|
||||
Text {
|
||||
text: "Current [A]:"
|
||||
Layout.row: 0
|
||||
Layout.column: 0
|
||||
}
|
||||
Text {
|
||||
text: current.toFixed(1)
|
||||
Layout.row: 0
|
||||
Layout.column: 1
|
||||
}
|
||||
Text {
|
||||
text: "Time [min]:"
|
||||
Layout.row: 1
|
||||
Layout.column: 0
|
||||
}
|
||||
Text {
|
||||
text: time
|
||||
Layout.row: 1
|
||||
Layout.column: 1
|
||||
}
|
||||
Text {
|
||||
text: "Voltage [V]:"
|
||||
Layout.row: 2
|
||||
Layout.column: 0
|
||||
}
|
||||
Text {
|
||||
text: "[" + voltage_min.toFixed(2) + ", " + voltage_max.toFixed(2) + "]"
|
||||
Layout.row: 2
|
||||
Layout.column: 1
|
||||
}
|
||||
Text {
|
||||
text: "Temperature [°C]:"
|
||||
Layout.row: 3
|
||||
Layout.column: 0
|
||||
}
|
||||
Text {
|
||||
text: "[" + temp_min.toFixed(1) + ", " + temp_max.toFixed(1) + "]"
|
||||
Layout.row: 3
|
||||
Layout.column: 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
minimumWidth: layout.Layout.minimumWidth
|
||||
minimumHeight: layout.Layout.minimumHeight
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user