From 062c6252a2e8b997e3d41eecfa9a68d508d0ba3a Mon Sep 17 00:00:00 2001 From: ZareMate <0.zaremate@gmail.com> Date: Mon, 13 Jan 2025 20:23:39 +0100 Subject: [PATCH] Add system tray functionality and improve Discord status detection logic --- .gitignore | 1 + DISCORD.lnk | Bin 1630 -> 1692 bytes README.md | 60 ++++++++++++++++++++++++++++++ deafen_screenshot.png | Bin 69 -> 99 bytes main.py | 83 ++++++++++++++++++++++++++++++++++-------- mute_screenshot.png | Bin 69 -> 110 bytes start.ps1 | 2 +- 7 files changed, 130 insertions(+), 16 deletions(-) create mode 100644 README.md diff --git a/.gitignore b/.gitignore index e69de29..45367a3 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1 @@ +./*.png \ No newline at end of file diff --git a/DISCORD.lnk b/DISCORD.lnk index 5e39ac405588f83e530ca5d3178aefc99aaead4b..478522520df2db20a59c5615ca49e5f0b549af2c 100644 GIT binary patch delta 98 zcmcb|GlzG=e5T7&CoT-*cr{s%fq_AUe{v$D$YfzgH>NLBCPy=-v1d#HiK|XdU=*Ev zm(hml)8xt0Os^QVCLdvP6pmpiW+-7uWGDiXdJF|Xp5bH<=ETXe%sQJ_GH+!9020C- AWdHyG delta 97 zcmbQkdyi+re5P}u6BmYYTn^M@U|jpk;_M4#~an;EQjG~k8 yGTJcR2%Rj=^h$!8L60GZp_rkBA(5d7Na`^Z0C|QClXo-gOpazw*!-M%D-!^N+Z(R{ diff --git a/README.md b/README.md new file mode 100644 index 0000000..7c007b7 --- /dev/null +++ b/README.md @@ -0,0 +1,60 @@ +# ESP32 Discord Controller + +This project allows you to control Discord mute and deafen status using an ESP32 device. The script captures the screen to detect the status and communicates with the ESP32 via a serial connection. + +## Installation + +### Prerequisites + +- Python 3.x +- pip (Python package installer) +- An ESP32 device +- A USB cable to connect the ESP32 to your computer + +### Python Packages + +Install the required Python packages using pip: + +```sh +pip install pyserial keyboard pygetwindow pyautogui mss numpy pystray pillow +``` + +### Clone the Repository + +Clone this repository to your local machine: + +```sh +git clone https://github.com/yourusername/ESP32-discord.git +cd ESP32-discord +``` + +## Usage + +### Running the Script + +You can run the script with the following command: + +```sh +python main.py +``` + +### Specifying the COM Port + +You can specify the COM port directly when running the script: + +```sh +python main.py -com=COM3 +``` + +### Hiding the Console Window + +If the COM port is specified, the script will hide the console window and run in the system tray. You can quit the application from the tray icon. + +## Notes + +- Ensure that your ESP32 device is connected to your computer via USB. +- The script captures a small region of the screen to detect the mute and deafen status. Adjust the coordinates and dimensions in the script if necessary. + +## License + +This project is licensed under the MIT License. \ No newline at end of file diff --git a/deafen_screenshot.png b/deafen_screenshot.png index 93af050daae1bbdf1ae33dc773941fd0f87da3a3..8f5d425c254346cd126dd37c27129e6ca36b208f 100644 GIT binary patch literal 99 zcmeAS@N?(olHy`uVBq!ia0vp^EFjFm1SHiab7}%9El(H6kcv6U38|@*r%ikNX6BoB xFHalPpIo(eHy@K=2Xi8$U;rm`jKm2>29--<3_pBYJ^*zwc)I$ztaD0e0sy$y9De`+ literal 69 zcmeAS@N?(olHy`uVBq!ia0vp^j3CUx1SBVv2j2ryJf1F&Ar*6yZ|r)lz`zp0WccvG R%;`XJ22WQ%mvv4FO#sz)5Y_+y diff --git a/main.py b/main.py index 68eeab7..5dc5481 100644 --- a/main.py +++ b/main.py @@ -2,13 +2,21 @@ import serial import time import keyboard import serial.tools.list_ports as list_ports -import pygetwindow as gw -import pyautogui import mss import numpy as np +import sys +import pystray +from PIL import Image, ImageDraw +import threading +import ctypes +# Set the title for the PowerShell window +ctypes.windll.kernel32.SetConsoleTitleW("ESP32 Discord Controller") def get_serial_port(): + if len(sys.argv) > 1 and sys.argv[1].startswith("-com="): + return sys.argv[1].split("=")[1] + ports = list(list_ports.comports()) if not ports: print("No serial ports found. Please connect a device.") @@ -20,14 +28,15 @@ def get_serial_port(): return ports[choice].device x = -1772 -y = 1059 -x2 = -1745 -y2 = 1064 -width = 1 -height = 1 +y = 1064 +x2 = -1746 +y2 = 1063 +width = 4 +height = 4 expected_r = 242 expected_g = 63 expected_b = 67 +grid = 4 def get_discord_status(): with mss.mss() as sct: @@ -47,16 +56,57 @@ def get_discord_status(): 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 at (0, 0) - mute_status = (mute_image[0, 0, 2] == expected_r and - mute_image[0, 0, 1] == expected_g and - mute_image[0, 0, 0] == expected_b) - deafen_status = (deafen_image[0, 0, 2] == expected_r and - deafen_image[0, 0, 1] == expected_g and - deafen_image[0, 0, 0] == expected_b) + # Check the pixel color in a 2x2 region + mute_status = any( + (mute_image[i, j, 2] == expected_r and + mute_image[i, j, 1] == expected_g and + mute_image[i, j, 0] == expected_b) + for i in range(grid) for j in range(grid) + ) + deafen_status = any( + (deafen_image[i, j, 2] == expected_r and + deafen_image[i, j, 1] == expected_g and + deafen_image[i, j, 0] == expected_b) + for i in range(grid) for j in range(grid) + ) return mute_status, deafen_status +def create_image(): + # Generate an image for the system tray icon + width = 64 + height = 64 + image = Image.new('RGB', (width, height), (255, 255, 255)) + dc = ImageDraw.Draw(image) + dc.rectangle( + [(width // 2, 0), (width, height // 2)], + fill="black") + dc.rectangle( + [(0, height // 2), (width // 2, height)], + fill="black") + return image + +def on_quit(icon, item): + icon.stop() + if 'ser' in globals() and ser.is_open: + ser.close() + print("Serial connection closed.") + 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) + +def hide_in_tray(): + hide_console() + icon = pystray.Icon("ESP32-discord") + icon.icon = create_image() + icon.menu = pystray.Menu(pystray.MenuItem('Quit', on_quit)) + threading.Thread(target=icon.run).start() + def main(): + global ser port = get_serial_port() if not port: return @@ -70,6 +120,9 @@ def main(): print(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() + while True: try: if ser.in_waiting > 0: @@ -103,7 +156,7 @@ def main(): except serial.SerialException as e: print(f"Could not open serial port: {e}") finally: - if 'ser' in locals() and ser.is_open: + if 'ser' in globals() and ser.is_open: ser.close() print("Serial connection closed.") diff --git a/mute_screenshot.png b/mute_screenshot.png index 27b738ef8f25315aa971defde31c02aba6f3a8da..0f7d9d8e95470b569c8f58bb22ac2c7ebd786cca 100644 GIT binary patch literal 110 zcmeAS@N?(olHy`uVBq!ia0vp^EFjFm1SHiab7}%9Q%@Jikcv6UGnTGAeCkx1io%w& z2U-|UFa>SbeY