diff --git a/res/data.json b/res/data.json index 3e2b588..e03d66d 100644 --- a/res/data.json +++ b/res/data.json @@ -1,3 +1,8 @@ { - "device-id": "1A86:7523" + "device-id": "1A86:7523", + "system-sounds": "Systeem", + "custom-names" : { + "firefox.exe": "Firefox", + "Discord.exe": "Discord" + } } \ No newline at end of file diff --git a/src/arduino/arduino-mixer.ino b/src/arduino/arduino-mixer.ino index 4e59b30..1d12b29 100644 --- a/src/arduino/arduino-mixer.ino +++ b/src/arduino/arduino-mixer.ino @@ -405,7 +405,6 @@ void loop() // Loop code // Check time dt = millis() - time; time = millis(); - Serial.println(interval); // Check for serial data or commands checkSerial(); diff --git a/src/python/start.py b/src/python/start.py index d82c864..cf03332 100644 --- a/src/python/start.py +++ b/src/python/start.py @@ -3,9 +3,10 @@ from pprint import pprint import serial import serial.tools.list_ports import time +import json def main(): - print ("Starting Arduino Mixer...\n") + print ("\nArduino Mixer by Arne van Iterson\n") # Get audio sessions sessions = AudioUtilities.GetAllSessions() @@ -13,7 +14,20 @@ def main(): for session in sessions: print (" " + str(session.DisplayName) + ": " + str(session.Process and session.Process.name())) + # Get data from data.json + with open("./res/data.json") as file: + jsonData = json.load(file) + print("\nArduino Device ID found:\n " + str(jsonData['device-id'])) + + print("\nCustom names found:") + print(" System sounds -> " + str(jsonData['system-sounds'])) + for process, name in jsonData['custom-names'].items(): + print(" " + str(process) + " -> " + str(name)) + # Init serial connection + board = serial.Serial() + board.baudrate = 115200 + ports = serial.tools.list_ports.comports() if len(ports) == 0: print("\nNo Serial ports Available") @@ -22,12 +36,19 @@ def main(): print ("\nSerial ports Available:") for port, desc, hwid in sorted(ports): print(" {}: {} [{}]".format(port, desc, hwid)) - - board = serial.Serial() - board.baudrate = 115200 - board.port = input("\nSelect COM port: ") + # Check if hwid matches id found in data.json and select if so + if jsonData["device-id"] in hwid: + board.port = port + + # Ask for port if a match is not found + if not (board.port): + board.port = "COM" + input("\nSelect COM port number: ") + + # Connect to serial port + print("Connecting to " + str(board.port)) board.open() + # Reset arduino and wait for it to finish startup print ("Waiting for arduino...") time.sleep(5) board.write("".encode("utf-8")) @@ -41,7 +62,16 @@ def main(): startMarker = '60' # < endMarker = '62' # > - while True: + timeout = 30 + t = time.time() + + while timeout != 0: + # Keep track of time + # TODO: Add timeouts to detect a disconnect + dt = time.time() - t + t = time.time() + + # Check for command markers for c in board.read(): if c == 62: recvInProgress = False @@ -65,28 +95,40 @@ def main(): elif command == "SWITCH": currentProgram = int(data) program = sessions[currentProgram] + displayName = None volume = program.SimpleAudioVolume.GetMasterVolume() + # Always rename system sounds because the name is too long to be handled by the Arduino if program.DisplayName == "@%SystemRoot%\System32\AudioSrv.Dll,-202": - name = "Systeem" - elif str(program.Process and program.Process.name()) == "firefox.exe": - name = "Firefox" - elif str(program.Process and program.Process.name()) == "Discord.exe": - name = "Discord" - elif program.DisplayName != "": - name = str(program.DisplayName) - else: - name = str(program.Process and program.Process.name()) + displayName = jsonData['system-sounds'] + elif program.Process and program.Process.name(): + # Check custom names from data.json + for process, name in jsonData['custom-names'].items(): + if program.Process and program.Process.name() == process: + displayName = str(name) + # If no name is found check for a DisplayName + if displayName is None: + if program.DisplayName != "": + displayName = str(program.DisplayName) + else: + # If no DisplayName is found, use the process name + # TODO: Remove '.exe' + displayName = str(program.Process and program.Process.name()) - board.write(("").encode()) + # Write data to arduino + board.write(("").encode()) board.write(("").encode()) elif command == "SETVOL": + # Change volume of selected program + # TODO: Send volume to arduino if volume is changed some other way audio = sessions[currentProgram].SimpleAudioVolume audio.SetMasterVolume(float(data) / 100, None) volume = audio.GetMasterVolume() + + # Send confirmation to arduino board.write(("").encode()) line = []