ESP32-discord/main.py

57 lines
1.7 KiB
Python

import serial
import time
import keyboard
import serial.tools.list_ports as list_ports
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
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...")
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')
else:
print(f"Unknown command: {line}")
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()