112 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			112 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| 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
 | |
| 
 | |
| 
 | |
| def get_serial_port():
 | |
|     ports = list(list_ports.comports())
 | |
|     if not ports:
 | |
|         print("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 = 1059
 | |
| x2 = -1745
 | |
| y2 = 1064
 | |
| width = 1
 | |
| height = 1
 | |
| expected_r = 242
 | |
| expected_g = 63
 | |
| expected_b = 67
 | |
| 
 | |
| def get_discord_status():
 | |
|     with mss.mss() as sct:
 | |
|         # Define the region where the mute and deafen icons are located
 | |
|         mute_region = {"top": y, "left": x, "width": width, "height": height}
 | |
|         deafen_region = {"top": y2, "left": x2, "width": width, "height": height}
 | |
|         
 | |
|         # Capture the screen region
 | |
|         mute_screenshot = sct.grab(mute_region)
 | |
|         deafen_screenshot = sct.grab(deafen_region)
 | |
|         
 | |
|         # Convert the screenshots to numpy arrays
 | |
|         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 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)
 | |
|         return mute_status, deafen_status
 | |
| 
 | |
| def main():
 | |
|     port = get_serial_port()
 | |
|     if not port:
 | |
|         return
 | |
|     
 | |
|     try:
 | |
|         ser = serial.Serial(port, 115200, timeout=1)
 | |
|         time.sleep(2)  # Wait for the connection to initialize
 | |
|         print("Listening for commands...")
 | |
|         
 | |
|         last_mute_status, last_deafen_status = get_discord_status()
 | |
|         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'))
 | |
|         
 | |
|         while True:
 | |
|             try:
 | |
|                 if ser.in_waiting > 0:
 | |
|                     line = ser.readline().decode('utf-8').strip()
 | |
|                     print(f"Received: {line}")
 | |
|                     
 | |
|                     if line == "mute":
 | |
|                         keyboard.send('ctrl+shift+f16')
 | |
|                     elif line == "deafen":
 | |
|                         keyboard.send('ctrl+shift+f15')
 | |
|                     elif line == "status":
 | |
|                         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}")
 | |
|                     
 | |
|                 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}")
 | |
|                     last_mute_status, last_deafen_status = current_mute_status, current_deafen_status
 | |
|                 
 | |
|                 time.sleep(0.1)
 | |
|             except KeyboardInterrupt:
 | |
|                 print("Exiting program.")
 | |
|                 break
 | |
|             except Exception as e:
 | |
|                 print(f"Error during execution: {e}")
 | |
|                 break
 | |
|     
 | |
|     except serial.SerialException as e:
 | |
|         print(f"Could not open serial port: {e}")
 | |
|     finally:
 | |
|         if 'ser' in locals() and ser.is_open:
 | |
|             ser.close()
 | |
|             print("Serial connection closed.")
 | |
| 
 | |
| if __name__ == "__main__":
 | |
|     main()
 |