Repository navigation
Expand file tree
/
Copy pathVolumeChangerArduino.py
More file actions
118 lines (104 loc) · 5.1 KB
/
Copy pathVolumeChangerArduino.py
File metadata and controls
118 lines (104 loc) · 5.1 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import serial
import time
import math
class VolumeChangerArduino():
def __init__(self, port="COM6", baudrate=115200, sleeptime=0.001):
self.arduino = serial.Serial(port=port, baudrate=baudrate, timeout=1)
self.sleeptime = sleeptime
self.serialData = ""
self.volume_level = 0
self.older_volume = 0
self.popt = []
self.funcCreator()
self.volumeSetup()
@staticmethod
def volumeFormatter(x, a, b, c):
return a * x ** 2 + b * x + c
#Converts volume percentage for pycaw
def funcCreator(self):
import numpy as np
from scipy.optimize import curve_fit
self.xdata = [0, 2, 3, 7, 13, 23, 27, 35, 45, 59, 77, 82, 88, 94, 100]
ydata = [76, 60, 52, 40, 31, 22, 20, 16, 12, 8, 4, 3, 2, 1, 0]
x = np.array(self.xdata, dtype=float) # transform your data in a numpy array of floats
y = np.array(ydata, dtype=float) # so the curve_fit can work
for i in range(0, len(self.xdata) - 2, 2):
self.popt.append("")
self.popt[int(i / 2)], _ = curve_fit(VolumeChangerArduino.volumeFormatter, [x[i], x[i + 1], x[i + 2]],
[y[i], y[i + 1], y[i + 2]])
#Setups volume tools
def volumeSetup(self):
from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(
IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
self.volume = cast(interface, POINTER(IAudioEndpointVolume))
self.volume.GetMute()
self.volume.GetMasterVolumeLevel()
self.volume.GetVolumeRange()
def serialGetData(self):
self.serialData = ""
while self.arduino.in_waiting > 0:
self.serialData += self.arduino.read().decode("ascii")
return self.serialData
def volumeChanger(self):
while True:
self.serialGetData()
if self.serialData != "":
if "\n" in self.serialData:
self.volume_level = int(self.serialData.rstrip().split("\r\n")[-1])
elif type(self.serialData) == str:
self.volume_level = int(self.serialData)
time.sleep(self.sleeptime * 10)
if self.older_volume != self.volume_level:
if self.volume_level == 100:
self.volume.SetMasterVolumeLevel(0, None)
else:
for i in range(0, len(self.xdata)-2, 2):
if self.volume_level <= self.xdata[i+2] and self.volume_level >= self.xdata[i]:
self.volume.SetMasterVolumeLevel(-1*(VolumeChangerArduino.volumeFormatter(self.volume_level, *self.popt[int(i/2)])), None)
self.older_volume = self.volume_level
self.serialData = ""
time.sleep(self.sleeptime)
def widgetOperations(self, widget_latency, position, taskbar_thick, size, transparency, margin):
import tkinter
def volumeWatcher():
label.config(text=self.volume_level)
label.after(widget_latency, volumeWatcher)
widget = tkinter.Tk()
self.widget = widget
ws, hs = widget.winfo_screenwidth(), widget.winfo_screenheight()
if size <= 1:
fontsize = math.floor(hs*size)
else:
fontsize = math.floor(size)
label = tkinter.Label(widget, font=("digital numbers", fontsize), background="#1B1E26", foreground="#FFE222")
label.pack(anchor="center")
label.master.overrideredirect(True)
positions_dict = {"b": "-", "t": "+", "l": "+", "r": "-"}
if position[2] == position[1]:
label.master.geometry('{0}{1}{2}{3}'.format(positions_dict[position[1]], int(math.floor(ws * taskbar_thick + margin)), positions_dict[position[0]], int(math.floor(margin))))
elif position[2] == position[0]:
label.master.geometry('{0}{1}{2}{3}'.format(positions_dict[position[1]], int(math.floor(margin)), positions_dict[position[0]], int(math.floor(hs * taskbar_thick + margin))))
else:
label.master.geometry('{0}{1}{2}{3}'.format(positions_dict[position[1]], int(math.floor(margin)), positions_dict[position[0]], int(math.floor(margin))))
# label.master.wm_attributes("-transparentcolor","#FFE221")
label.master.wm_attributes("-alpha",transparency)
label.master.wm_attributes("-topmost", True)
label.master.lift()
volumeWatcher()
widget.mainloop()
def useWidget(self, widget_latency=10, position="brb", taskbar_thick=0.03, size=0.08, transparency=1, margin=0):
import threading
self.widgetThread = threading.Thread(target=self.widgetOperations, args=(widget_latency,position,taskbar_thick,size,transparency,margin,))
self.widgetThread.start()
def closeWidget(self):
self.widget.quit()
def run(self):
self.volumeChanger()
if __name__ == "__main__":
x = VolumeChangerArduino()
x.useWidget()
x.run()