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
dt = millis() - time;
time = millis();
Serial.println(interval);
// Check for serial data or commands
checkSerial();

View File

@ -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))
# Check if hwid matches id found in data.json and select if so
if jsonData["device-id"] in hwid:
board.port = port
board = serial.Serial()
board.baudrate = 115200
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()
# Reset arduino and wait for it to finish startup
print ("Waiting for arduino...")
time.sleep(5)
board.write("<HELLO>".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)
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:
name = str(program.Process and program.Process.name())
# 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())
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(("<GETVOL," + str(round(volume * 100)) + ">").encode())
line = []