-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdisplay_ddcutil.py
86 lines (73 loc) · 2.74 KB
/
display_ddcutil.py
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
"""Wrapper module for ddcutil"""
import subprocess
from base_classes import Display
class DisplayDDCUtil(Display):
"""Handle displays through ddcutil"""
def __init__(self, name=None, path=None, bus=None):
super().__init__(name, path)
self.bus = bus
self.brightness = None
@classmethod
def detect(cls, parameters=None):
"""Find all displays connected to the system"""
try:
output = cls.command("detect")
except subprocess.CalledProcessError:
print('ddcutil detect failed')
return
current_valid = False
current_bus, current_name = (None, None)
for line in output.splitlines():
if len(line) == 0:
pass
elif not line[0].isspace():
if line.startswith('Display'):
current_valid = True
else:
current_valid = False
current_bus, current_name = (None, None)
elif current_valid:
line = line.strip()
if line.startswith('I2C bus'):
current_bus = line.split("/dev/i2c-")[1].strip()
elif line.startswith('Monitor:'):
current_name = line.split("Monitor:")[1].strip()
if current_name is not None and current_bus is not None:
yield cls(name=current_name, path="/dev/i2c-" + str(current_bus), bus=current_bus)
current_bus, current_name = (None, None)
@classmethod
def command(cls, cmd):
"""Run a command through ddcutil"""
command = ['ddcutil', "--brief"]
try:
cmd = cmd.split()
except AttributeError:
pass
command += cmd
output = subprocess.run(
command,
stdout=subprocess.PIPE,
check=True
).stdout.decode('utf-8').strip()
return output
def read_brightness(self):
"""Get brightness from the display"""
try:
data = self.command(['--bus', str(self.bus), 'getvcp', '0x10'])
self.brightness = int(data.split()[3])
except subprocess.CalledProcessError:
self.brightness = None
print('ddcutil getvcp failed')
def get_brightness(self):
"""Return last brightness value"""
if not self.enabled:
return None
if self.brightness is None:
self.read_brightness()
return self.brightness
def _set_brightness(self, brightness):
try:
self.command(['--bus', str(self.bus), 'setvcp', '0x10', str(brightness)])
self.brightness = brightness
except subprocess.CalledProcessError:
print('ddcutil setvcp failed')