Add temperature sample rate display and log clear button

This commit is contained in:
zhangyisong 2026-08-18 22:56:37 +08:00
parent 3b0c0c321f
commit 0b62356bd7

View File

@ -200,6 +200,8 @@ class PhotomagneticGUI:
self.worker = SerialWorker(self) self.worker = SerialWorker(self)
self._monitor_after_id = None self._monitor_after_id = None
self._last_temp_time = 0.0
self._temp_interval = 0.0
self._build_ui() self._build_ui()
@ -312,6 +314,7 @@ class PhotomagneticGUI:
self._disp_labels = {} self._disp_labels = {}
entries = [ entries = [
("Temperature", "--- °C", "temp"), ("Temperature", "--- °C", "temp"),
("Sample Rate", "---", "sample_rate"),
("Heating State", "---", "heat_state"), ("Heating State", "---", "heat_state"),
("Heating Temp.", "--- °C", "heat_temp"), ("Heating Temp.", "--- °C", "heat_temp"),
("Device ID", "---", "dev_id"), ("Device ID", "---", "dev_id"),
@ -341,6 +344,11 @@ class PhotomagneticGUI:
log_frame = ttk.LabelFrame(right, text="Log", padding=4) log_frame = ttk.LabelFrame(right, text="Log", padding=4)
log_frame.pack(fill=tk.BOTH, expand=True, pady=(4, 0)) log_frame.pack(fill=tk.BOTH, expand=True, pady=(4, 0))
log_toolbar = ttk.Frame(log_frame)
log_toolbar.pack(fill=tk.X, pady=(0, 2))
ttk.Button(log_toolbar, text="Clear", width=6,
command=self._clear_log).pack(side=tk.RIGHT)
self._log_text = scrolledtext.ScrolledText(log_frame, height=12, font=("Consolas", 9), self._log_text = scrolledtext.ScrolledText(log_frame, height=12, font=("Consolas", 9),
wrap=tk.WORD, state=tk.DISABLED) wrap=tk.WORD, state=tk.DISABLED)
self._log_text.pack(fill=tk.BOTH, expand=True) self._log_text.pack(fill=tk.BOTH, expand=True)
@ -520,6 +528,13 @@ class PhotomagneticGUI:
# ── 0x00 TEMP: 设备主动推送, 只更新 live value, 不输出 log ── # ── 0x00 TEMP: 设备主动推送, 只更新 live value, 不输出 log ──
if cmd == Cmd.TEMP: if cmd == Cmd.TEMP:
t = temp_from_data(data) t = temp_from_data(data)
now = time.time()
if self._last_temp_time > 0:
self._temp_interval = now - self._last_temp_time
hz = 1.0 / self._temp_interval if self._temp_interval > 0 else 0
self._disp_labels["sample_rate"].configure(
text=f"{self._temp_interval*1000:.0f}ms ({hz:.1f}Hz)")
self._last_temp_time = now
self._disp_labels["temp"].configure(text=f"{t:.2f} °C") self._disp_labels["temp"].configure(text=f"{t:.2f} °C")
elif cmd == Cmd.GET_ID: elif cmd == Cmd.GET_ID:
@ -569,6 +584,11 @@ class PhotomagneticGUI:
if 0 <= idx < len(self._ntc_labels): if 0 <= idx < len(self._ntc_labels):
self._ntc_labels[idx].configure(text=f"{idx}:{temp:.1f}") self._ntc_labels[idx].configure(text=f"{idx}:{temp:.1f}")
def _clear_log(self):
self._log_text.configure(state=tk.NORMAL)
self._log_text.delete(1.0, tk.END)
self._log_text.configure(state=tk.DISABLED)
def _append_log(self, msg: str): def _append_log(self, msg: str):
self._log_text.configure(state=tk.NORMAL) self._log_text.configure(state=tk.NORMAL)
self._log_text.insert(tk.END, msg + "\n") self._log_text.insert(tk.END, msg + "\n")