From f6aa6cd6262beea472600feb739d1c2427dc47d8 Mon Sep 17 00:00:00 2001 From: ZareMate <0.zaremate@gmail.com> Date: Wed, 15 Jan 2025 02:25:36 +0100 Subject: [PATCH] Change rendering method for GUI Add installation scripts for Windows and Linux, and update README for usage instructions --- DISCORD.lnk | Bin 1692 -> 0 bytes README.md | 40 ++++++++++---- install_linux.sh | 15 ++++++ install_windows.bat | 15 ++++++ main.py => main.pyw | 127 ++++++++++++++++++++++++++++---------------- start.bat | 9 ++++ start.ps1 | 2 +- start.sh | 10 ++++ 8 files changed, 161 insertions(+), 57 deletions(-) delete mode 100644 DISCORD.lnk create mode 100644 install_linux.sh create mode 100644 install_windows.bat rename main.py => main.pyw (61%) create mode 100644 start.bat create mode 100644 start.sh diff --git a/DISCORD.lnk b/DISCORD.lnk deleted file mode 100644 index ba7d26d59540dd50b3cce77e4b51f1c909a43ad7..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1692 zcmb_cTS$~a6#mo-yZ@p^ZMQ8lO55#UO|*r;T2mjyHr7g)zi`pN+;DZ<-C7NL?4d9S z>`p}&OQH)&gb!V)rywvkh3G;c^-y$SyAeH9^qqfqf3c#1n&HfxIdkTmZ_YV0RRGd* z(m4V*jS=OSkqImL*p9ld&z3w+yKrPe?415MEw#qrC_^(Xp>K}k({nJ(J-9Mved+Wo zCm&=YUsaDz#UhteGnIyzHE+T9o10N%da&f%ydU?9hq>8e(}XMww)U*O zKOlD-H_#EH=O%h>W(TaOPWlYh4eU{{)nQiw=P*A_hM(MD`mwFQgRQh+2ah3#HhB{! zFH~2)$BLjp4-0g=-lj9kNxQOZ^S(DCbUiMmk-%>?uc3BbLeNAUHWAy2yp06RBsGzy zCr`_4BT3sx$|7Zu=AAwJK;aCQNU{R7RL)X@p%7~l0PRQIR;*x5G?Woh1GX?@I%^`r z{As9QeMlC4`?$IC;gr}8 z%H{bPpq-rmU-~q&EVMs{0?b7MV9(@0*@fM~%I?Zid)Lyojv188-+)Teiz9&#_9sV6 Q4=5Q$pPd 1 and sys.argv[1].startswith("-com="): return sys.argv[1].split("=")[1] - ports = list(list_ports.comports()) + ports = [port for port in list(list_ports.comports()) if port.device != "COM1"] if not ports: - print("No serial ports found. Please connect a device.") + messagebox.showerror("Error", "No serial ports found. Please connect a device.") return None - print("Available Ports:") - for i, port in enumerate(ports): - print(f"{i}: {port.device}") - choice = int(input("Select a port by number: ")) - return ports[choice].device - -x = -1772 -y = 1064 -x2 = -1746 -y2 = 1063 -width = 4 -height = 4 -expected_r = 242 -expected_g = 63 -expected_b = 67 -grid = 4 + + if len(ports) == 1: + return ports[0].device + + port_choices = [port.device for port in ports] + + def on_select(): + selected_port.set(port_listbox.get(port_listbox.curselection())) + port_window.destroy() + + port_window = tk.Tk() + port_window.title("Select Serial Port") + port_window.configure(bg="#1e1e1e") + tk.Label(port_window, text="Available Ports:", bg="#1e1e1e", fg="#ffffff").pack() + port_listbox = tk.Listbox(port_window, bg="#2d2d2d", fg="#ffffff", selectbackground="#0078d7") + for port in port_choices: + port_listbox.insert(tk.END, port) + port_listbox.pack() + selected_port = tk.StringVar() + tk.Button(port_window, text="Select", command=on_select, bg="#0078d7", fg="#ffffff").pack() + port_window.mainloop() + return selected_port.get() def get_discord_status(): with mss.mss() as sct: @@ -61,10 +86,6 @@ def get_discord_status(): mute_image = np.array(mute_screenshot) deafen_image = np.array(deafen_screenshot) - # # Save the screenshots to files - # mss.tools.to_png(mute_screenshot.rgb, mute_screenshot.size, output="mute_screenshot.png") - # mss.tools.to_png(deafen_screenshot.rgb, deafen_screenshot.size, output="deafen_screenshot.png") - # Check the pixel color in a 2x2 region mute_status = any( (mute_image[i, j, 2] == expected_r and @@ -95,32 +116,35 @@ def create_image(): return image def toggle_console(): - whnd = ctypes.windll.kernel32.GetConsoleWindow() - if whnd != 0: - if ctypes.windll.user32.IsWindowVisible(whnd): - ctypes.windll.user32.ShowWindow(whnd, 0) - else: - ctypes.windll.user32.ShowWindow(whnd, 1) + if console_window.state() == "normal": + console_window.withdraw() + else: + console_window.deiconify() + +def log_message(message): + log_text.insert(tk.END, message + "\n") + log_text.see(tk.END) def on_quit(icon, item): icon.stop() if 'ser' in globals() and ser.is_open: ser.close() - print("Serial connection closed.") + log_message("Serial connection closed.") + console_window.withdraw() + console_window.quit() sys.exit(0) def hide_console(): - whnd = ctypes.windll.kernel32.GetConsoleWindow() - if whnd != 0: - ctypes.windll.user32.ShowWindow(whnd, 0) - ctypes.windll.kernel32.CloseHandle(whnd) + log_message("Hiding console window...") + time.sleep(2) + console_window.withdraw() def hide_in_tray(): - hide_console() icon = pystray.Icon("ESP32-discord") icon.icon = create_image() icon.menu = pystray.Menu(pystray.MenuItem('Quit', on_quit), pystray.MenuItem('Toggle Console', toggle_console)) threading.Thread(target=icon.run).start() + hide_console() def main(): global ser @@ -131,20 +155,20 @@ def main(): try: ser = serial.Serial(port, 115200, timeout=1) time.sleep(2) # Wait for the connection to initialize - print("Listening for commands...") + log_message("Listening for commands...") last_mute_status, last_deafen_status = get_discord_status() - print(f"Mute: {last_mute_status}, Deafen: {last_deafen_status}") + log_message(f"Mute: {last_mute_status}, Deafen: {last_deafen_status}") ser.write(f"Mute: {last_mute_status}, Deafen: {last_deafen_status}\n".encode('utf-8')) - if len(sys.argv) > 1 and sys.argv[1].startswith("-com="): - hide_in_tray() + hide_in_tray() + while True: try: if ser.in_waiting > 0: line = ser.readline().decode('utf-8').strip() - print(f"Received: {line}") + log_message(f"Received: {line}") if line == "mute": keyboard.send('ctrl+shift+f16') @@ -154,28 +178,39 @@ def main(): mute_status, deafen_status = get_discord_status() ser.write(f"Mute: {mute_status}, Deafen: {deafen_status}\n".encode('utf-8')) else: - print(f"Unknown command: {line}") + log_message(f"Unknown command: {line}") current_mute_status, current_deafen_status = get_discord_status() if current_mute_status != last_mute_status or current_deafen_status != last_deafen_status: ser.write(f"Mute: {current_mute_status}, Deafen: {current_deafen_status}\n".encode('utf-8')) - print(f"Mute: {current_mute_status}, Deafen: {current_deafen_status}") + log_message(f"Mute: {current_mute_status}, Deafen: {current_deafen_status}") last_mute_status, last_deafen_status = current_mute_status, current_deafen_status time.sleep(0.1) except KeyboardInterrupt: - print("Exiting program.") + log_message("Exiting program.") break except Exception as e: - print(f"Error during execution: {e}") + log_message(f"Error during execution: {e}") break except serial.SerialException as e: - print(f"Could not open serial port: {e}") + messagebox.showerror("Error", f"Could not open serial port: {e}") finally: if 'ser' in globals() and ser.is_open: ser.close() - print("Serial connection closed.") + log_message("Serial connection closed.") if __name__ == "__main__": - main() + console_window = tk.Tk() + console_window.title("ESP32 Discord Controller") + console_window.configure(bg="#1e1e1e") + style = ttk.Style() + style.theme_use('clam') + style.configure("TLabel", background="#1e1e1e", foreground="#ffffff") + style.configure("TButton", background="#0078d7", foreground="#ffffff") + style.configure("TText", background="#2d2d2d", foreground="#ffffff") + log_text = tk.Text(console_window, wrap=tk.WORD, bg="#2d2d2d", fg="#ffffff") + log_text.pack(expand=True, fill=tk.BOTH) + threading.Thread(target=main).start() + console_window.mainloop() diff --git a/start.bat b/start.bat new file mode 100644 index 0000000..120b8c5 --- /dev/null +++ b/start.bat @@ -0,0 +1,9 @@ +@echo off +REM Activate the virtual environment +call .venv\Scripts\activate + +REM Run the Python script +start pythonw main.pyw + +REM Deactivate the virtual environment +deactivate diff --git a/start.ps1 b/start.ps1 index c04a173..714c68d 100644 --- a/start.ps1 +++ b/start.ps1 @@ -2,7 +2,7 @@ & .venv\Scripts\activate # Run the Python script -python main.py -com=COM3 +pythonw main.pyw # Deactivate the virtual environment deactivate \ No newline at end of file diff --git a/start.sh b/start.sh new file mode 100644 index 0000000..8ba8229 --- /dev/null +++ b/start.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +# Activate the virtual environment +source .venv/bin/activate + +# Run the Python script +pythonw main.pyw & + +# Deactivate the virtual environment +deactivate