Skip to content

Commit b7dfb3c

Browse files
committed
Fix bug on windows and optimization processes
1 parent eacb6e4 commit b7dfb3c

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

scripts/led_pulse.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from pyfirmata import Arduino
22
import time
3+
import customtkinter as ctk
34

45

5-
def led(porta: str, opc: int, tempo1: float, tempo2: float = None, num_pulse: int = None):
6-
uno = Arduino(porta)
6+
def led(uno, opc: int, tempo1: float, tempo2: float = None, num_pulse: int = None):
77
match opc:
88
case 1:
99
print('Pulso Unico')

scripts/main.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from tkinter import *
44
import customtkinter as ctk
55
from led_pulse import led
6+
from pyfirmata import Arduino as Arduino_
67

78

89
def Arduino(porta):
@@ -13,6 +14,13 @@ def Arduino(porta):
1314
return None
1415

1516

17+
def first_open():
18+
global uno
19+
if selected_port.get() != ports[0]:
20+
uno = Arduino_(selected_port.get())
21+
open_new_page()
22+
23+
1624
def changeTheme():
1725
global color_bg, color_text, instagram_link, github_link, text_radius_color
1826
if ctk.get_appearance_mode() == "Dark":
@@ -22,7 +30,7 @@ def changeTheme():
2230
text_radius_color = "#EBEBEB"
2331
instagram_link = ctk.CTkLabel(root, text="Instagram", text_color=color_text, cursor="hand2",
2432
fg_color=text_radius_color, bg_color=color_bg, corner_radius=20)
25-
github_link = ctk.CTkLabel(root, text="GitHub", text_color=color_text, cursor="hand2",
33+
github_link = ctk.CTkLabel(root, text="GitHub", text_color=color_text, cursor="hand2",
2634
fg_color=text_radius_color, bg_color=color_bg, corner_radius=20)
2735
instagram_link.place(x=20, y=root.winfo_height() - 84)
2836
github_link.place(x=20, y=root.winfo_height() - 50)
@@ -42,7 +50,8 @@ def changeTheme():
4250

4351

4452
def open_new_page():
45-
if selected_port.get() != ports[0]:
53+
if selected_port.get() != ports[0] and Arduino(selected_port.get()).is_open is True:
54+
Arduino(selected_port.get()).close()
4655
root.geometry(f"500x500+{CENTER}+{CENTER}")
4756
frame(root)
4857
root.title("Seleção de pulso")
@@ -53,7 +62,8 @@ def open_new_page():
5362

5463
root.bind("<Destroy>", lambda event: on_closing(event, Arduino(selected_port.get())))
5564

56-
tabview = ctk.CTkTabview(root, width=400, height=450, corner_radius=20, segmented_button_selected_color="purple",
65+
tabview = ctk.CTkTabview(root, width=400, height=450, corner_radius=20,
66+
segmented_button_selected_color="purple",
5767
segmented_button_selected_hover_color="#8B008B", bg_color=color_bg)
5868
tabview.pack()
5969
tabview.add("SetUp")
@@ -76,7 +86,7 @@ def open_new_page():
7686
main_label1 = ctk.CTkLabel(configures_tab,
7787
text=f"Porta: {selected_port.get()}",
7888
wraplength=400, bg_color=color_bg, compound="left")
79-
main_label1.place(x=0, y=20) # {Arduino(selected_port.get())}
89+
main_label1.place(x=0, y=20)
8090

8191
main_label2 = ctk.CTkLabel(configures_tab,
8292
text=f"Arduino: ",
@@ -133,14 +143,15 @@ def open_new_page():
133143
about_label.pack()
134144
instagram_link = ctk.CTkLabel(root, text="Instagram", text_color=color_text, cursor="hand2",
135145
fg_color=text_radius_color, bg_color=color_bg, corner_radius=20)
136-
github_link = ctk.CTkLabel(root, text="GitHub", text_color=color_text, cursor="hand2", fg_color=text_radius_color,
146+
github_link = ctk.CTkLabel(root, text="GitHub", text_color=color_text, cursor="hand2",
147+
fg_color=text_radius_color,
137148
bg_color=color_bg, corner_radius=20)
138149

139150
instagram_link.bind("<Button-1>", lambda e: callback("https://www.instagram.com/veras_programmer"))
140151
github_link.bind("<Button-1>", lambda e: callback("https://www.github.com/Veras-D"))
141152

142-
instagram_link.place(x=20, y=root.winfo_height()-84)
143-
github_link.place(x=20, y=root.winfo_height()-50)
153+
instagram_link.place(x=20, y=root.winfo_height() - 84)
154+
github_link.place(x=20, y=root.winfo_height() - 50)
144155

145156
else:
146157
port_error = ctk.CTkToplevel(root)
@@ -170,7 +181,7 @@ def pulso_unico():
170181

171182
def led_pulse():
172183
if tempo1_entry.get().strip() != "":
173-
led(porta=selected_port.get(), opc=1, tempo1=float(tempo1_entry.get()))
184+
led(uno=uno, opc=1, tempo1=float(tempo1_entry.get()))
174185
tempo1_entry.delete(0, END)
175186
open_new_page()
176187
else:
@@ -213,7 +224,7 @@ def pulso_periodico():
213224

214225
def led_pulse():
215226
if tempo1_entry.get().strip() != "" and tempo2_entry.get().strip() != "" and num_pulse_entry.get().strip() != "":
216-
led(porta=selected_port.get(), opc=2, tempo1=float(tempo1_entry.get()),
227+
led(uno=uno, opc=2, tempo1=float(tempo1_entry.get()),
217228
tempo2=float(tempo2_entry.get()), num_pulse=int(num_pulse_entry.get()))
218229
tempo1_entry.delete(0, END)
219230
tempo2_entry.delete(0, END)
@@ -253,7 +264,6 @@ def led_pulse():
253264
ctk.set_appearance_mode("dark")
254265
frame(root)
255266

256-
257267
selected_port = StringVar()
258268
selected_port.set("Selecione a porta")
259269

@@ -263,12 +273,10 @@ def led_pulse():
263273
dropdown.set(ports[0])
264274
dropdown.pack(pady=5)
265275

266-
267-
button = ctk.CTkButton(root, text="Continue", command=open_new_page, fg_color="purple", hover_color="#8B008B")
276+
button = ctk.CTkButton(root, text="Continue", command=first_open, fg_color="purple", hover_color="#8B008B")
268277
button.pack(pady=25)
269278

270279
porta = selected_port.get()
271280
root.bind("<Destroy>", lambda event: on_closing(event, Arduino(selected_port.get())))
272281

273-
274282
root.mainloop()

0 commit comments

Comments
 (0)