load-controller/load_controller/load.py

48 lines
1.2 KiB
Python
Raw Normal View History

2023-01-03 22:57:18 +01:00
import serial
import time
2023-01-04 19:33:20 +01:00
from PySide6.QtCore import QObject, Slot, QTimer
2023-01-04 00:22:27 +01:00
from .profile_handler import ProfileHandler
2023-01-03 22:57:18 +01:00
class Load(QObject):
dev: serial.Serial
2023-01-26 13:58:18 +01:00
_error: bool
2023-01-04 00:22:27 +01:00
_current: float
2023-01-04 19:33:20 +01:00
_timer: QTimer
_profile_handler: ProfileHandler
2023-01-04 00:22:27 +01:00
def __init__(self, uart_path: str, profile_handler: ProfileHandler):
super().__init__(None)
self.dev = serial.Serial(uart_path, 115200)
2023-01-26 13:58:18 +01:00
self._error = False
2023-01-04 00:22:27 +01:00
self._current = 0
2023-01-04 19:33:20 +01:00
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)
2023-01-04 00:22:27 +01:00
2023-01-26 13:58:18 +01:00
def set_error(self, error):
self._error = error
2023-01-04 19:33:20 +01:00
@Slot()
2023-01-04 00:22:27 +01:00
def _update_current(self, current):
self._current = current
2023-01-03 22:57:18 +01:00
2023-01-04 19:33:20 +01:00
def _update_load(self):
2023-01-26 13:58:18 +01:00
current = 0 if self._error else self._current
curr_quants = round(current / 0.1)
2023-01-04 19:33:20 +01:00
assert curr_quants <= 0xFFFF
2023-01-04 00:22:27 +01:00
2023-01-04 19:33:20 +01:00
msb = curr_quants >> 8
lsb = curr_quants & 0xFF
self.dev.write(bytes((msb, lsb)))
r = self.dev.read_all()
read_bytes = 0 if r is None else len(r)
print(f"Read {read_bytes} bytes from load controller")