Implement Discord mute/deafen status detection and control via serial communication
This commit is contained in:
		
							parent
							
								
									110b0daebb
								
							
						
					
					
						commit
						2fdfa7d3df
					
				
							
								
								
									
										18
									
								
								colors.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								colors.py
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,18 @@ | |||||||
|  | import keyboard | ||||||
|  | import time | ||||||
|  | import serial.tools.list_ports as list_ports | ||||||
|  | import pygetwindow as gw | ||||||
|  | import pyautogui | ||||||
|  | 
 | ||||||
|  | while True: | ||||||
|  |     if keyboard.is_pressed('m'): | ||||||
|  |         x, y = pyautogui.position() | ||||||
|  |         time.sleep(1) | ||||||
|  |         color = pyautogui.screenshot().getpixel((x, y)) | ||||||
|  |         print(f"Coordinates: ({x}, {y}), Color: {color}") | ||||||
|  |     if keyboard.is_pressed('d'): | ||||||
|  |         x2, y2 = pyautogui.position() | ||||||
|  |         time.sleep(1) | ||||||
|  |         color2 = pyautogui.screenshot().getpixel((x2, y2)) | ||||||
|  |         print(f"Coordinates: ({x2}, {y2}), Color: {color2}") | ||||||
|  |     time.sleep(0.1) | ||||||
							
								
								
									
										
											BIN
										
									
								
								deafen_screenshot.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								deafen_screenshot.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| After Width: | Height: | Size: 69 B | 
							
								
								
									
										55
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										55
									
								
								main.py
									
									
									
									
									
								
							| @ -2,6 +2,11 @@ import serial | |||||||
| import time | import time | ||||||
| import keyboard | import keyboard | ||||||
| import serial.tools.list_ports as list_ports | import serial.tools.list_ports as list_ports | ||||||
|  | import pygetwindow as gw | ||||||
|  | import pyautogui | ||||||
|  | import mss | ||||||
|  | import numpy as np | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| def get_serial_port(): | def get_serial_port(): | ||||||
|     ports = list(list_ports.comports()) |     ports = list(list_ports.comports()) | ||||||
| @ -14,6 +19,43 @@ def get_serial_port(): | |||||||
|     choice = int(input("Select a port by number: ")) |     choice = int(input("Select a port by number: ")) | ||||||
|     return ports[choice].device |     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(): | def main(): | ||||||
|     port = get_serial_port() |     port = get_serial_port() | ||||||
|     if not port: |     if not port: | ||||||
| @ -24,6 +66,10 @@ def main(): | |||||||
|         time.sleep(2)  # Wait for the connection to initialize |         time.sleep(2)  # Wait for the connection to initialize | ||||||
|         print("Listening for commands...") |         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: |         while True: | ||||||
|             try: |             try: | ||||||
|                 if ser.in_waiting > 0: |                 if ser.in_waiting > 0: | ||||||
| @ -34,9 +80,18 @@ def main(): | |||||||
|                         keyboard.send('ctrl+shift+f16') |                         keyboard.send('ctrl+shift+f16') | ||||||
|                     elif line == "deafen": |                     elif line == "deafen": | ||||||
|                         keyboard.send('ctrl+shift+f15') |                         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: |                     else: | ||||||
|                         print(f"Unknown command: {line}") |                         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) |                 time.sleep(0.1) | ||||||
|             except KeyboardInterrupt: |             except KeyboardInterrupt: | ||||||
|                 print("Exiting program.") |                 print("Exiting program.") | ||||||
|  | |||||||
| @ -1,7 +1,12 @@ | |||||||
|  | #define MUTE_PIN 18 | ||||||
|  | #define DEAFEN_PIN 19 | ||||||
|  | 
 | ||||||
| void setup() | void setup() | ||||||
| { | { | ||||||
|     pinMode(16, INPUT_PULLUP);   // D16 for "mute" button
 |     pinMode(16, INPUT_PULLUP);   // D16 for "mute" button
 | ||||||
|     pinMode(17, INPUT_PULLUP); // D17 for "deafen" button
 |     pinMode(17, INPUT_PULLUP); // D17 for "deafen" button
 | ||||||
|  |     pinMode(MUTE_PIN, OUTPUT); | ||||||
|  |     pinMode(DEAFEN_PIN, OUTPUT); | ||||||
|     Serial.begin(115200); |     Serial.begin(115200); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -18,4 +23,29 @@ void loop() | |||||||
|         Serial.println("deafen"); |         Serial.println("deafen"); | ||||||
|         delay(200); // Debounce delay
 |         delay(200); // Debounce delay
 | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     if (Serial.available() > 0) | ||||||
|  |     { | ||||||
|  |         String input = Serial.readStringUntil('\n'); | ||||||
|  |         if (input == "Mute: True, Deafen: False") | ||||||
|  |         { | ||||||
|  |             digitalWrite(MUTE_PIN, HIGH); | ||||||
|  |             digitalWrite(DEAFEN_PIN, LOW); | ||||||
|  |         } | ||||||
|  |         else if (input == "Mute: True, Deafen: True") | ||||||
|  |         { | ||||||
|  |             digitalWrite(MUTE_PIN, HIGH); | ||||||
|  |             digitalWrite(DEAFEN_PIN, HIGH); | ||||||
|  |         } | ||||||
|  |         else if (input == "Mute: False, Deafen: False") | ||||||
|  |         { | ||||||
|  |             digitalWrite(MUTE_PIN, LOW); | ||||||
|  |             digitalWrite(DEAFEN_PIN, LOW); | ||||||
|  |         } | ||||||
|  |         else if (input == "Mute: False, Deafen: True") | ||||||
|  |         { | ||||||
|  |             digitalWrite(MUTE_PIN, LOW); | ||||||
|  |             digitalWrite(DEAFEN_PIN, HIGH); | ||||||
|  |         } | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | |||||||
							
								
								
									
										
											BIN
										
									
								
								mute_screenshot.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								mute_screenshot.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| After Width: | Height: | Size: 69 B | 
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user