-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
47 lines (41 loc) · 1.75 KB
/
Copy pathmain.py
File metadata and controls
47 lines (41 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import serial,os,math,time
os.system("cls")
#initialize the processes and volume for each slider using dictionaries
program = {"A": "skype.exe", "B": "chrome.exe", "C": "vlc.exe", "D": "steam.exe"}
setVolume = {"A": 0, "B": 0, "C": 0, "D": 0}
#cycle through all available COM ports to poll for a connected device
#repeats itself if no arduino is found on the COM ports
print "Connecting to arduino..."
ports = ['COM%s' % (i + 1) for i in range(256)]
ardFound = False
while not ardFound:
for port in ports:
#print "Testing %s" %(port)
try:
ser = serial.Serial(port, 9600, timeout=4)
except (OSError, serial.SerialException):
pass
else:
#connected device found, test if the device is an arduino sending a ready state
#if so, break out of the for and while loops
serInput = ser.readline()
#read only the first 13 characters, as a weird character is being output at the end
serInput = serInput[0:13]
if serInput != "ARDUINO READY":
ser.close()
else:
print "Connected to arduino on port %s." %(port)
ardFound = True
break
#return ready state to the arduino to begin slider configuration
ser.write("OS READY")
#main loop
while 1:
#read the serial output and the slider for which the output is given
serialreading = ser.readline()
serialVolume = float(serialreading[1:])/1024.0
slider = serialreading[0]
#set new volume if the slider has changed, constantly adjusting is more resource-heavy
if serialVolume != setVolume[slider]:
os.system("nircmd setappvolume %s %f" %(program[slider], serialVolume))
setVolume[slider] = serialVolume