Use QML
This commit is contained in:
@ -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)
|
||||
|
||||
Reference in New Issue
Block a user