Added data.json and comments to python

This commit is contained in:
Arne van Iterson 2020-05-31 13:45:07 +02:00
parent c075fcba4c
commit 88736c4ac5
3 changed files with 64 additions and 18 deletions

View File

@ -1,3 +1,8 @@
{ {
"device-id": "1A86:7523" "device-id": "1A86:7523",
"system-sounds": "Systeem",
"custom-names" : {
"firefox.exe": "Firefox",
"Discord.exe": "Discord"
}
} }

View File

@ -405,7 +405,6 @@ void loop() // Loop code
// Check time // Check time
dt = millis() - time; dt = millis() - time;
time = millis(); time = millis();
Serial.println(interval);
// Check for serial data or commands // Check for serial data or commands
checkSerial(); checkSerial();

View File

@ -3,9 +3,10 @@ from pprint import pprint
import serial import serial
import serial.tools.list_ports import serial.tools.list_ports
import time import time
import json
def main(): def main():
print ("Starting Arduino Mixer...\n") print ("\nArduino Mixer by Arne van Iterson\n")
# Get audio sessions # Get audio sessions
sessions = AudioUtilities.GetAllSessions() sessions = AudioUtilities.GetAllSessions()
@ -13,7 +14,20 @@ def main():
for session in sessions: for session in sessions:
print (" " + str(session.DisplayName) + ": " + str(session.Process and session.Process.name())) 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 # Init serial connection
board = serial.Serial()
board.baudrate = 115200
ports = serial.tools.list_ports.comports() ports = serial.tools.list_ports.comports()
if len(ports) == 0: if len(ports) == 0:
print("\nNo Serial ports Available") print("\nNo Serial ports Available")
@ -22,12 +36,19 @@ def main():
print ("\nSerial ports Available:") print ("\nSerial ports Available:")
for port, desc, hwid in sorted(ports): for port, desc, hwid in sorted(ports):
print(" {}: {} [{}]".format(port, desc, hwid)) print(" {}: {} [{}]".format(port, desc, hwid))
# Check if hwid matches id found in data.json and select if so
board = serial.Serial() if jsonData["device-id"] in hwid:
board.baudrate = 115200 board.port = port
board.port = input("\nSelect COM 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() board.open()
# Reset arduino and wait for it to finish startup
print ("Waiting for arduino...") print ("Waiting for arduino...")
time.sleep(5) time.sleep(5)
board.write("<HELLO>".encode("utf-8")) board.write("<HELLO>".encode("utf-8"))
@ -41,7 +62,16 @@ def main():
startMarker = '60' # < startMarker = '60' # <
endMarker = '62' # > 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(): for c in board.read():
if c == 62: if c == 62:
recvInProgress = False recvInProgress = False
@ -65,28 +95,40 @@ def main():
elif command == "SWITCH": elif command == "SWITCH":
currentProgram = int(data) currentProgram = int(data)
program = sessions[currentProgram] program = sessions[currentProgram]
displayName = None
volume = program.SimpleAudioVolume.GetMasterVolume() 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": if program.DisplayName == "@%SystemRoot%\System32\AudioSrv.Dll,-202":
name = "Systeem" displayName = jsonData['system-sounds']
elif str(program.Process and program.Process.name()) == "firefox.exe": elif program.Process and program.Process.name():
name = "Firefox" # Check custom names from data.json
elif str(program.Process and program.Process.name()) == "Discord.exe": for process, name in jsonData['custom-names'].items():
name = "Discord" if program.Process and program.Process.name() == process:
elif program.DisplayName != "": displayName = str(name)
name = str(program.DisplayName) # If no name is found check for a DisplayName
else: if displayName is None:
name = str(program.Process and program.Process.name()) 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(("<NAME," + name + ">").encode()) # Write data to arduino
board.write(("<NAME," + displayName + ">").encode())
board.write(("<GETVOL," + str(round(volume * 100)) + ">").encode()) board.write(("<GETVOL," + str(round(volume * 100)) + ">").encode())
elif command == "SETVOL": 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 = sessions[currentProgram].SimpleAudioVolume
audio.SetMasterVolume(float(data) / 100, None) audio.SetMasterVolume(float(data) / 100, None)
volume = audio.GetMasterVolume() volume = audio.GetMasterVolume()
# Send confirmation to arduino
board.write(("<GETVOL," + str(round(volume * 100)) + ">").encode()) board.write(("<GETVOL," + str(round(volume * 100)) + ">").encode())
line = [] line = []