finish all

This commit is contained in:
SelfConfusion 2024-07-09 15:57:47 +08:00
parent 684de2bc00
commit d51d2e994e
3 changed files with 305 additions and 287 deletions

2
.gitignore vendored
View File

@ -1,2 +1,2 @@
.venv/ .venv/
src/__pycache__/ src/__pycache__/

View File

@ -1,2 +1,4 @@
pip==24.1.1 customtkinter==5.2.2
setuptools==65.5.0 darkdetect==0.8.0
packaging==24.1
pyserial==3.5

View File

@ -1,284 +1,300 @@
import customtkinter as ctk import customtkinter as ctk
import serial import serial
import serial.tools import serial.tools
import serial.tools.list_ports as ls_ports import serial.tools.list_ports as ls_ports
import serial.tools.list_ports import serial.tools.list_ports
from typing import Optional, Any from typing import Optional, Any
import functools import functools
class App(ctk.CTk): class App(ctk.CTk):
__window_width = 1280 __window_width = 1280
__window_height = 1350 __window_height = 1350
__column_weight = [1, 9, 3] __column_weight = [1, 9, 3]
serial_status = False serial_status = False
serial_baudrate = 115200 serial_baudrate = 115200
gain_values = [0.0 for i in range(5)] gain_values = [0.0 for i in range(5)]
gain_default = [1.3, 0.7, 1.1, 2.2, 0.5] gain_default = [1.3, 0.7, 1.1, 2.2, 0.5]
gain_enable = [True for i in range(5)] gain_enable = [True for i in range(5)]
def __init__(self) -> None: def __init__(self) -> None:
super().__init__() super().__init__()
self.__windowInit(self.__window_width, self.__window_height) self.__windowInit(self.__window_width, self.__window_height)
self.__data_frame() self.__data_frame()
def __windowInit(self, width: int, height: int) -> None: def __windowInit(self, width: int, height: int) -> None:
self.title("ISP上位机") self.title("ISP上位机")
# appearance and font # appearance and font
ctk.set_appearance_mode("dark") ctk.set_appearance_mode("dark")
ctk.set_default_color_theme("dark-blue") ctk.set_default_color_theme("dark-blue")
ctk.set_widget_scaling(2.0) ctk.set_window_scaling(0.8)
self.font = ctk.CTkFont(family="", size=17) # ctk.set_widget_scaling(2.0)
self.font = ctk.CTkFont(family="", size=17)
# get the screen dimension
screen_width = self.winfo_screenwidth() # get the screen dimension
screen_height = self.winfo_screenheight() screen_width = self.winfo_screenwidth()
# find the center point screen_height = self.winfo_screenheight()
center_x = int(screen_width / 2 - width / 2) # find the center point
center_y = int(screen_height / 2 - height / 2) center_x = int(screen_width / 2 - width / 2)
# set the position of the self to the center of the screen center_y = int(screen_height / 2 - height / 2)
self.geometry(f"{width}x{height}+{center_x}+{center_y}") # set the position of the self to the center of the screen
self.geometry(f"{width}x{height}+{center_x}+{center_y}")
# create gain_frame
top_frame = ctk.CTkFrame(self) # create gain_frame
top_frame.pack(side="top", fill=ctk.X, padx=10) top_frame = ctk.CTkFrame(self)
top_frame.columnconfigure(0, weight=self.__column_weight[0]) top_frame.pack(side="top", fill=ctk.X, padx=10)
top_frame.columnconfigure(1, weight=self.__column_weight[1]) top_frame.columnconfigure(0, weight=self.__column_weight[0])
top_frame.columnconfigure(2, weight=self.__column_weight[2]) top_frame.columnconfigure(1, weight=self.__column_weight[1])
top_frame.columnconfigure(2, weight=self.__column_weight[2])
# Serial Port
# create label # Serial Port
self.serial_label = ctk.CTkLabel(top_frame, text="串口:", font=self.font) # create label
self.serial_label.grid(column=0, row=0, sticky=ctk.EW, pady=10, padx=10) self.serial_label = ctk.CTkLabel(top_frame, text="串口:", font=self.font)
# create combobox self.serial_label.grid(column=0, row=0, sticky=ctk.EW, pady=10, padx=10)
self.serial_combobox = ctk.CTkComboBox( # create combobox
top_frame, self.serial_combobox = ctk.CTkComboBox(
state="readonly", top_frame,
command=self.selectSerial, state="readonly",
font=self.font, command=self.selectSerial,
dropdown_font=self.font, font=self.font,
) dropdown_font=self.font,
self.serial_combobox.bind("<Button>", self.updateSerial) )
self.updateSerial() self.serial_combobox.bind("<Button>", self.updateSerial)
self.serial_combobox.grid(column=1, row=0, sticky=ctk.EW, pady=10, padx=20) self.updateSerial()
# create button self.serial_combobox.grid(column=1, row=0, sticky=ctk.EW, pady=10, padx=20)
self.serial_button = ctk.CTkButton( # create button
top_frame, self.serial_button = ctk.CTkButton(
command=self.connectSerial, top_frame,
font=self.font, command=self.connectSerial,
) font=self.font,
self.serial_button_text = ctk.StringVar(self.serial_button, "连接") )
self.serial_button.configure(textvariable=self.serial_button_text) self.serial_button_text = ctk.StringVar(self.serial_button, "连接")
self.serial_button.grid(column=2, row=0, sticky=ctk.EW, pady=10, padx=10) self.serial_button.configure(textvariable=self.serial_button_text)
self.serial_button.grid(column=2, row=0, sticky=ctk.EW, pady=10, padx=10)
# Serial Baudrate
# create label # Serial Baudrate
self.baud_label = ctk.CTkLabel(top_frame, text="波特率:", font=self.font) # create label
self.baud_label.grid(column=0, row=1, sticky=ctk.EW, pady=10, padx=10) self.baud_label = ctk.CTkLabel(top_frame, text="波特率:", font=self.font)
# create combobox self.baud_label.grid(column=0, row=1, sticky=ctk.EW, pady=10, padx=10)
self.baud_combobox = ctk.CTkComboBox( # create combobox
top_frame, self.baud_combobox = ctk.CTkComboBox(
values=[str(9600), str(19200), str(38400), str(57600), str(115200)], top_frame,
command=self.setBaudrate, values=[str(9600), str(19200), str(38400), str(57600), str(115200)],
font=self.font, command=self.setBaudrate,
dropdown_font=self.font, font=self.font,
) dropdown_font=self.font,
self.baud_combobox.grid(column=1, row=1, sticky=ctk.EW, pady=10, padx=20) )
# create Set Button self.baud_combobox.grid(column=1, row=1, sticky=ctk.EW, pady=10, padx=20)
self.send_button = ctk.CTkButton( # create Set Button
top_frame, text="设置", font=self.font, command=self.setISP self.send_button = ctk.CTkButton(
) top_frame, text="设置", font=self.font, command=self.setISP
self.send_button.grid(column=2, row=1, sticky=ctk.EW, pady=10, padx=10) )
return None self.send_button.grid(column=2, row=1, sticky=ctk.EW, pady=10, padx=10)
return None
def __data_frame(self) -> None:
def setGain(value, i:int, stringVar: ctk.StringVar)-> None: def __data_frame(self) -> None:
self.gain_values[i] = value def setGain(value, i: int, stringVar: ctk.StringVar) -> None:
stringVar.set(f'{value:.2f}') self.gain_values[i] = value
return None stringVar.set(f"{value:.2f}")
return None
def switchSlider(i: int, slider: ctk.CTkSlider) -> None:
if slider.cget("state") == "normal": def switchSlider(i: int, slider: ctk.CTkSlider) -> None:
self.gain_values[i] = self.gain_default[i] if slider.cget("state") == "normal":
slider.configure(button_color="grey") self.gain_values[i] = self.gain_default[i]
slider.configure(state="disabled") slider.configure(button_color="grey")
else: slider.configure(state="disabled")
self.gain_values[i] = slider.get() else:
slider.configure(button_color="#1f538d") self.gain_values[i] = slider.get()
slider.configure(state="normal") slider.configure(button_color="#1f538d")
return None slider.configure(state="normal")
return None
def switchCorrection(i: int) -> None:
if self.gain_enable[i]: def switchCorrection(i: int) -> None:
self.gain_enable[i] = False if self.gain_enable[i]:
else: self.gain_enable[i] = False
self.gain_enable[i] = True else:
return None self.gain_enable[i] = True
return None
text = [
"红色校正系数", text = [
"绿色校正系数", "红色校正系数",
"蓝色校正系数", "绿色校正系数",
"伽马校正系数", "蓝色校正系数",
"饱和度调整系数", "伽马校正系数",
] "饱和度调整系数",
gain_min = [-2.0, -2.0, -2.0, 0.0, -1.0] ]
gain_max = [2.0, 2.0, 2.0, 5.0, 1.0] gain_min = [-2.0, -2.0, -2.0, 0.0, -1.0]
fg_clolor = [ gain_max = [2.0, 2.0, 2.0, 5.0, 1.0]
"#4d140b", fg_clolor = [
"#005f50", "#4d140b",
"#456789", "#005f50",
"#596062", "#456789",
"#596062", "#596062",
] "#596062",
]
data_frame = ctk.CTkFrame(self)
data_frame.pack(side="top", fill=ctk.X, pady=10) data_frame = ctk.CTkFrame(self)
data_frame.columnconfigure(0, weight=10) data_frame.pack(side="top", fill=ctk.X, pady=10)
data_frame.columnconfigure(1, weight=3) data_frame.columnconfigure(0, weight=10)
data_frame.columnconfigure(1, weight=3)
gain_frame = []
gain_str = [] gain_frame = []
gain_label = [] gain_str = []
gain_slider = [] gain_label = []
gain_button = [] gain_slider = []
gain_checkbox = [] gain_button = []
for i in range(5): gain_checkbox = []
# gain_frame for i in range(5):
gain_frame.append(ctk.CTkFrame(data_frame, fg_color=fg_clolor[i])) # gain_frame
gain_frame[i].grid(column=0, sticky=ctk.EW, pady=10, padx=20, ipady=10) gain_frame.append(ctk.CTkFrame(data_frame, fg_color=fg_clolor[i]))
gain_frame[i].grid(column=0, sticky=ctk.EW, pady=10, padx=20, ipady=10)
# label
if i < 3: # label
ctk.CTkLabel(gain_frame[i], text=text[i], font=self.font).pack( if i < 3:
side="top", ipady=10 ctk.CTkLabel(gain_frame[i], text=text[i], font=self.font).pack(
) side="top", ipady=10
else: )
gain_checkbox.append( else:
ctk.CTkCheckBox( gain_checkbox.append(
gain_frame[i], ctk.CTkCheckBox(
text=text[i], gain_frame[i],
font=self.font, text=text[i],
command=functools.partial(switchCorrection, i), font=self.font,
) command=functools.partial(switchCorrection, i),
) )
gain_checkbox[i - 3].select() )
gain_checkbox[i - 3].pack(side="top", ipady=10) gain_checkbox[i - 3].select()
gain_checkbox[i - 3].pack(side="top", ipady=10)
# slider
gain_slider.append( # slider
ctk.CTkSlider(gain_frame[i], from_=gain_min[i], to=gain_max[i]) gain_slider.append(
) ctk.CTkSlider(gain_frame[i], from_=gain_min[i], to=gain_max[i])
gain_slider[i].set(self.gain_default[i]) )
gain_slider[i].pack(side="top", fill=ctk.X, padx=10) gain_slider[i].set(self.gain_default[i])
gain_str.append( gain_slider[i].pack(side="top", fill=ctk.X, padx=10)
ctk.StringVar(gain_slider[i], f"{self.gain_default[i]:.2f}") gain_str.append(
) ctk.StringVar(gain_slider[i], f"{self.gain_default[i]:.2f}")
gain_slider[i].configure(command=functools.partial(setGain, i=i, stringVar=gain_str[i])) )
gain_slider[i].configure(
# gain value command=functools.partial(setGain, i=i, stringVar=gain_str[i])
ctk.CTkLabel(gain_frame[i], text=str(gain_min[i]), font=self.font).pack( )
side="left", padx=10
) # gain value
ctk.CTkLabel(gain_frame[i], text=str(gain_max[i]), font=self.font).pack( ctk.CTkLabel(gain_frame[i], text=str(gain_min[i]), font=self.font).pack(
side="right", padx=10 side="left", padx=10
) )
gain_label.append( ctk.CTkLabel(gain_frame[i], text=str(gain_max[i]), font=self.font).pack(
ctk.CTkLabel( side="right", padx=10
gain_frame[i], )
textvariable=gain_str[i], gain_label.append(
font=self.font, ctk.CTkLabel(
) gain_frame[i],
) textvariable=gain_str[i],
gain_label[i].pack(side="top") font=self.font,
)
# button )
gain_button.append( gain_label[i].pack(side="top")
ctk.CTkButton(
data_frame, # button
text="使用推荐配置", gain_button.append(
command=functools.partial(switchSlider, i=i, slider=gain_slider[i]), ctk.CTkButton(
font=self.font, data_frame,
) text="使用推荐配置",
) command=functools.partial(switchSlider, i=i, slider=gain_slider[i]),
gain_button[i].grid(column=1, row=i, sticky=ctk.NSEW, pady=10, padx=20) font=self.font,
)
return None )
gain_button[i].grid(column=1, row=i, sticky=ctk.NSEW, pady=10, padx=20)
def updateSerial(self, event: Optional[Any] = None):
print("Update Serial Ports") return None
port_list = []
for info in ls_ports.comports(): def updateSerial(self, event: Optional[Any] = None):
port, desc, hwid = info print("Update Serial Ports")
port_list.append(port + " " + desc) port_list = []
self.serial_combobox.configure(values=port_list) for info in ls_ports.comports():
port, desc, hwid = info
def selectSerial(self, port_info: Optional[Any] = None): port_list.append(port + " " + desc)
print("Select Serial port") self.serial_combobox.configure(values=port_list)
try:
self.select_port = port_info.split()[0] def selectSerial(self, port_info: Optional[Any] = None):
except Exception: print("Select Serial port")
print("Get None") try:
finally: self.select_port = port_info.split()[0]
print(self.select_port) except Exception:
print("Get None")
def connectSerial(self, info: Optional[Any] = None): finally:
try: print(self.select_port)
print(self.select_port)
except AttributeError: def connectSerial(self, info: Optional[Any] = None):
print("Please Select Serial Port") try:
else: print(self.select_port)
if not self.serial_status: except AttributeError:
try: print("Please Select Serial Port")
self.serial = serial.Serial(self.select_port, self.serial_baudrate) else:
except AttributeError: if not self.serial_status:
print("Failed to Connect Serial") try:
else: self.serial = serial.Serial(self.select_port, self.serial_baudrate)
self.serial_status = True self.serial.open()
self.serial_button_text.set("断开连接") except AttributeError:
print("Select Serial Port:") print("Failed to Connect Serial")
print(self.select_port) else:
else: self.serial_status = True
try: self.serial_button_text.set("断开连接")
self.serial.close() print("Select Serial Port:")
except Exception: print(self.select_port)
print("Failed to Close Serial") else:
else: try:
self.serial_status = False self.serial.close()
self.serial_button_text.set("连接") except Exception:
print("Succeed to Close Serial") print("Failed to Close Serial")
else:
def setBaudrate(self, baudrate: Optional[Any] = None): self.serial_status = False
self.serial_baudrate = int(baudrate) self.serial_button_text.set("连接")
if self.serial_status: print("Succeed to Close Serial")
try:
self.serial.baudrate = int(baudrate) def setBaudrate(self, baudrate: Optional[Any] = None):
except Exception: self.serial_baudrate = int(baudrate)
print("Failed to Set Serial Baudrate") if self.serial_status:
else: try:
print("Succeed to Set Serial Baudrate") self.serial.close()
else: self.serial = serial.Serial(self.select_port, self.serial_baudrate)
print("Please Connect to Serial") self.serial.open()
except Exception:
def setISP(self, event: Optional[Any] = None): print("Failed to Set Serial Baudrate")
if self.serial_status: else:
self.serial.open() print("Succeed to Set Serial Baudrate")
for i in range(5): else:
if self.gain_enable[i]: print("Please Connect to Serial")
match i:
case 1: def setISP(self, event: Optional[Any] = None):
self.serial.write(b'r') def switchSign(i: int) -> str:
match i:
self.serial.close() case 0:
else: return 'r'
print("Please Connect to Serial") case 1:
return 'g'
def run(self): case 2:
self.mainloop() return 'b'
case 3:
return 'a'
if __name__ == "__main__": case 4:
app = App() return 's'
app.run() case _:
return 'error'
if self.serial_status:
for i in range(5):
if self.gain_enable[i]:
self.serial.write(f"{switchSign(i)}{bin(int(self.gain_values[i]))}{bin(int(self.gain_values[i] % 1 * 256))}")
else:
print("Please Connect to Serial")
def run(self):
self.mainloop()
if __name__ == "__main__":
app = App()
app.run()