Talk to Nucleo running babystack-tempsensing

This commit is contained in:
2023-01-16 21:42:04 +01:00
parent 6977daaca1
commit 1020d8731e
3 changed files with 53 additions and 5 deletions

View File

@ -1,7 +1,7 @@
import os.path
import time
from PySide6.QtCore import QTimer, QObject
from PySide6.QtCore import QTimer, QObject, Signal
from PySide6.QtWidgets import QFileDialog
from PySide6.QtQml import QQmlApplicationEngine
@ -17,7 +17,7 @@ class GUI(QObject):
_start_pause_btn: QObject
_stop_btn: QObject
def __init__(self, profile_handler: ProfileHandler):
def __init__(self, profile_handler: ProfileHandler, temperaturesUpdated: Signal):
super().__init__(None)
self._engine = QQmlApplicationEngine()
@ -42,6 +42,8 @@ class GUI(QObject):
self._update_timer.timeout.connect(self._update)
self._update_timer.start(100)
temperaturesUpdated.connect(self._updateTemperatures)
def _choose_profile(self):
dlg = QFileDialog()
dlg.setFileMode(QFileDialog.FileMode.ExistingFile)
@ -79,3 +81,7 @@ class GUI(QObject):
if self._profile_handler.state == ProfileState.RUNNING:
dt = time.time() - self._profile_handler.profile_start
self._win.setProperty("profileTime", dt)
def _updateTemperatures(self, temperatures: list[int]):
self._win.setProperty("temp_min", min(temperatures))
self._win.setProperty("temp_max", max(temperatures))

View File

@ -0,0 +1,35 @@
import sys
import serial
import struct
from PySide6.QtCore import QObject, Signal
START_OF_TEMPS = bytes((0xFF, 0xFF))
N_SENSORS = 12
TEMP_QUANT = 0.0625 # °C/quant
class Temperatures(QObject):
temperaturesUpdated = Signal(list)
_temps: list[float]
def __init__(self, uart_path: str):
super().__init__(parent=None)
self.dev = serial.Serial(uart_path, 115200)
self._temps = [0.0] * N_SENSORS
def run(self):
while True:
self.dev.read_until(START_OF_TEMPS)
data = self.dev.read(N_SENSORS * 2)
temps = struct.unpack(f">{N_SENSORS}h", data)
for i, t in enumerate(temps):
if (t & 0x0F) != 0:
print(
f"WARN: temperature had a non-zero least-significant nibble: {t:04x}",
file=sys.stderr,
)
else:
self._temps[i] = (t >> 4) * TEMP_QUANT
self.temperaturesUpdated.emit(self._temps)