From 22c5b33d5e032915be9fb72e10f0777014778055 Mon Sep 17 00:00:00 2001 From: Christoffer Martinsson Date: Thu, 15 May 2025 10:58:23 +0200 Subject: [PATCH] Code cleanup --- spm6103_viewer.py | 111 +++++++++++++++++++++++++++++++--------------- 1 file changed, 75 insertions(+), 36 deletions(-) diff --git a/spm6103_viewer.py b/spm6103_viewer.py index 85f8b02..23ee33a 100644 --- a/spm6103_viewer.py +++ b/spm6103_viewer.py @@ -2,10 +2,11 @@ import tkinter as tk from tkinter.font import Font import serial import os -import json +import json import threading import time + class SerialManager: def __init__(self, port="/dev/ttyUSB0", baud_rate=115200, reconnect_interval=2): self.port_name = port @@ -23,7 +24,9 @@ class SerialManager: with self.lock: if self.ser is None or not self.ser.is_open: try: - self.ser = serial.Serial(self.port_name, self.baud_rate, timeout=1) + self.ser = serial.Serial( + self.port_name, self.baud_rate, timeout=1 + ) print(f"[INFO] Connected to {self.port_name}") except serial.SerialException as e: print(f"[WARN] Serial port error: {e}") @@ -34,8 +37,8 @@ class SerialManager: with self.lock: if self.ser and self.ser.is_open: try: - self.ser.write((command + '\n').encode('utf-8')) - response = self.ser.readline().decode('utf-8').strip() + self.ser.write((command + "\n").encode("utf-8")) + response = self.ser.readline().decode("utf-8").strip() return response except Exception as e: print(f"[ERROR] Failed to send/receive: {e}") @@ -48,8 +51,10 @@ class SerialManager: if self.ser and self.ser.is_open: self.ser.close() + # Set DPI scale for Wayland -os.environ['GDK_SCALE'] = '2' +os.environ["GDK_SCALE"] = "2" + # Load saved window geometry if exists def load_window_geometry(): @@ -60,6 +65,7 @@ def load_window_geometry(): except FileNotFoundError: return "1080x200+100+100" # Default geometry if no settings file + # Save the window geometry when closing the app def save_window_geometry(): geometry = app.geometry() @@ -67,55 +73,68 @@ def save_window_geometry(): with open("settings.json", "w") as f: json.dump(settings, f) + # Serial configuration -ser = SerialManager(port='/dev/ttyUSB0') +ser = SerialManager(port="/dev/ttyUSB0") + def send_command(cmd): return ser.send_command(cmd) + def get_id(): - return send_command('*IDN?') + return send_command("*IDN?") + def get_multimeter_data(): - rawdata = send_command(f'CONFigure:ALL?') + rawdata = send_command(f"CONFigure:ALL?") return rawdata + def get_powersupply_data(): - rawdata = send_command(f'MEASure:ALL:INFO?') + rawdata = send_command(f"MEASure:ALL:INFO?") return rawdata + def set_res_mode(): - rawdata = send_command(f'[SENSe:]FUNCtion:RESistance') + rawdata = send_command(f"[SENSe:]FUNCtion:RESistance") time.sleep(0.1) - rawdata = send_command(f'[SENSe:]RESistance:RANGe:AUTO ON') + rawdata = send_command(f"[SENSe:]RESistance:RANGe:AUTO ON") return rawdata + def set_res200_mode(): - rawdata = send_command(f'[SENSe:]FUNCtion:RESistance') + rawdata = send_command(f"[SENSe:]FUNCtion:RESistance") time.sleep(0.1) - rawdata = send_command(f'[SENSe:]RESistance:RANGe 200') + rawdata = send_command(f"[SENSe:]RESistance:RANGe 200") return rawdata + def set_cont_mode(): - rawdata = send_command(f'[SENSe:]FUNCtion:CONTinuity') + rawdata = send_command(f"[SENSe:]FUNCtion:CONTinuity") return rawdata + def set_diod_mode(): - rawdata = send_command(f'[SENSe:]FUNCtion:DIODe') + rawdata = send_command(f"[SENSe:]FUNCtion:DIODe") return rawdata + def set_voltdc_mode(): - rawdata = send_command(f'[SENSe:]FUNCtion:VOLTage:DC') + rawdata = send_command(f"[SENSe:]FUNCtion:VOLTage:DC") return rawdata + def set_voltac_mode(): - rawdata = send_command(f'[SENSe:]FUNCtion:VOLTage:AC') + rawdata = send_command(f"[SENSe:]FUNCtion:VOLTage:AC") return rawdata + def set_cap_mode(): - rawdata = send_command(f'[SENSe:]FUNCtion:CAPacitance') + rawdata = send_command(f"[SENSe:]FUNCtion:CAPacitance") return rawdata + # --- GUI Setup --- def fetch_device_info(): # Fetch the device ID and Channel data @@ -125,12 +144,17 @@ def fetch_device_info(): if len(multimeter_data.split(",")) >= 4: # Update the labels multimeter_type_var.set(multimeter_data.split(",")[0]) - multimeter_data_var.set(multimeter_data.split(",")[1].replace("+","").replace("Ohm",'\u2126')) - multimeter_range_var.set(multimeter_data.split(",")[3].replace("Ohm",'\u2126')) + multimeter_data_var.set( + multimeter_data.split(",")[1].replace("+", "").replace("Ohm", "\u2126") + ) + multimeter_range_var.set( + multimeter_data.split(",")[3].replace("Ohm", "\u2126") + ) - # Schedule this function to be called again after 100ms + # Schedule this function to be called again after 100ms app.after(100, fetch_device_info) + app = tk.Tk() app.title("SPM6103") # Load and apply the saved geometry (size + position) @@ -138,7 +162,9 @@ app.geometry(load_window_geometry()) app.configure(bg="black") # For cleanup on exit -app.protocol("WM_DELETE_WINDOW", lambda: [save_window_geometry(), ser.stop(), app.quit()]) +app.protocol( + "WM_DELETE_WINDOW", lambda: [save_window_geometry(), ser.stop(), app.quit()] +) multimeter_type_var = tk.StringVar() multimeter_data_var = tk.StringVar() @@ -151,6 +177,7 @@ fontBig = Font(size=24) resize_after_id = None + def apply_font_resize(): width = app.winfo_width() @@ -158,33 +185,38 @@ def apply_font_resize(): fontMedium.configure(size=max(14, int(width * 0.02))) fontBig.configure(size=max(24, int(width * 0.09))) + def resize_loop(): apply_font_resize() app.after(200, resize_loop) + resize_loop() # start it once # Configure grid to be fully responsive -app.rowconfigure(0, weight=20) # Top - big -app.rowconfigure(1, weight=1) # Bottom - small +app.rowconfigure(0, weight=20) # Top - big +app.rowconfigure(1, weight=1) # Bottom - small app.columnconfigure(0, weight=1, uniform="equal", minsize=100) for i in range(7): - app.columnconfigure(i+1, weight=1, uniform="equal") + app.columnconfigure(i + 1, weight=1, uniform="equal") # Labels # Type and Range -tk.Label(app, textvariable=multimeter_type_var, font=fontMedium, bg="black", fg="lightgrey") \ - .grid(column=0, columnspan=1, row=1, sticky="nsw", padx=0, pady=0) +tk.Label( + app, textvariable=multimeter_type_var, font=fontMedium, bg="black", fg="lightgrey" +).grid(column=0, columnspan=1, row=1, sticky="nsw", padx=0, pady=0) -tk.Label(app, textvariable=multimeter_range_var, font=fontMedium, bg="black", fg="lightgrey") \ - .grid(column=0, columnspan=1, row=0, sticky="nw", padx=0, pady=0) +tk.Label( + app, textvariable=multimeter_range_var, font=fontMedium, bg="black", fg="lightgrey" +).grid(column=0, columnspan=1, row=0, sticky="nw", padx=0, pady=0) -# Data -tk.Label(app, textvariable=multimeter_data_var, font=fontBig, bg="black", fg="#afd787") \ - .grid(column=1, columnspan=6, row=0, rowspan=1, sticky="nsew", padx=0, pady=10) +# Data +tk.Label( + app, textvariable=multimeter_data_var, font=fontBig, bg="black", fg="#afd787" +).grid(column=1, columnspan=6, row=0, rowspan=1, sticky="nsew", padx=0, pady=10) -# Buttons +# Buttons buttons = [ ("RES", set_res_mode), ("RES200", set_res200_mode), @@ -196,9 +228,16 @@ buttons = [ ] for i, (label, cmd) in enumerate(buttons, start=1): - tk.Button(app, text=label, command=cmd, font=fontSmall, bg="black", - fg="#444444", highlightbackground="black", borderwidth=0) \ - .grid(column=i, row=1, sticky="nsew", padx=0, pady=0) + tk.Button( + app, + text=label, + command=cmd, + font=fontSmall, + bg="black", + fg="#444444", + highlightbackground="black", + borderwidth=0, + ).grid(column=i, row=1, sticky="nsew", padx=0, pady=0) # Initial fetch to start the auto-update process