Browse Source

use i2c bus number instead of display number for ddcutil

Thomas B 3 months ago
parent
commit
b4888f009f
2 changed files with 39 additions and 22 deletions
  1. 15
    5
      client/brightness.py
  2. 24
    17
      client/ddc.py

+ 15
- 5
client/brightness.py View File

@@ -37,12 +37,22 @@ if __name__ == "__main__":
37 37
     lux.check_connection(usb)
38 38
 
39 39
     disps = ddc.ddc_detect()
40
-    brightness = lux.read_brightness(usb)
41
-    print("Brightness:", brightness)
40
+    if len(disps) <= 0:
41
+        raise ValueError("no displays found")
42 42
 
43 43
     for d in disps:
44
-        d["prev"] = ddc.ddc_get(d["id"])
45
-        print("Display \"{}\" at {}".format(d["name"], d["prev"]))
44
+        # select i2c bus if available, id otherwise
45
+        if "bus" in d:
46
+            d["_id"] = d["bus"]
47
+        else:
48
+            d["_id"] = d["id"]
49
+
50
+        # get initial value
51
+        d["prev"] = ddc.ddc_get(d["_id"])
52
+        print("Display \"{}\" ({}) at {}".format(d["name"], d["_id"], d["prev"]))
53
+
54
+    brightness = lux.read_brightness(usb)
55
+    print("Brightness:", brightness)
46 56
 
47 57
     print()
48 58
     print("{}: Starting main loop".format(time.ctime()))
@@ -56,6 +66,6 @@ if __name__ == "__main__":
56 66
             if val != d["prev"]:
57 67
                 d["prev"] = val
58 68
                 print("{}: Setting \"{}\" to {}".format(time.ctime(), d["name"], val))
59
-                ddc.ddc_set(d["id"], val)
69
+                ddc.ddc_set(d["_id"], val)
60 70
 
61 71
         time.sleep(1.0)

+ 24
- 17
client/ddc.py View File

@@ -17,26 +17,28 @@ def ddc_detect():
17 17
         if len(data) < 4:
18 18
             continue
19 19
 
20
-        # Display X
21
-        num = data[0].split()
22
-        if num[0] != "Display":
23
-            #raise ValueError("unexpected identifier (\"{}\" != \"Display\")".format(num[0]))
24
-            continue
25
-        field["id"] = num[1]
26
-
27
-        # Monitor: name ...
28
-        name = data[3].split()
29
-        if name[0] != "Monitor:":
30
-            #raise ValueError("unexpected identifier (\"{}\" != \"Monitor:\")".format(name[0]))
31
-            continue
32
-        field["name"] = ' '.join(name[1:])
33
-
34
-        out.append(field)
20
+        for d in data:
21
+            v = d.split()
22
+            if v[0] == "Display":
23
+                field["id"] = v[1]
24
+            elif (v[0] == "I2C") and (v[1] == "bus:"):
25
+                field["bus"] = v[2]
26
+            elif v[0] == "Monitor:":
27
+                field["name"] = ' '.join(v[1:])
28
+
29
+        # if id is not there it's an "Ivalid display"
30
+        if "id" in field:
31
+            out.append(field)
35 32
 
36 33
     return out
37 34
 
38 35
 def ddc_get(dev):
39
-    r = subprocess.run(["ddcutil", "-d", str(dev), "-t", "getvcp", "10"], capture_output=True)
36
+    if dev.startswith("/dev/i2c-"):
37
+        cmd = ["ddcutil", "--skip-ddc-checks", "--bus", dev.replace("/dev/i2c-", ""), "-t", "getvcp", "10"]
38
+    else:
39
+        cmd = ["ddcutil", "-d", str(dev), "-t", "getvcp", "10"]
40
+
41
+    r = subprocess.run(cmd, capture_output=True)
40 42
     if r.returncode != 0:
41 43
         raise ValueError("ddcutil returned {} \"{}\"".format(r.returncode, r.stderr.decode("utf-8")))
42 44
 
@@ -51,7 +53,12 @@ def ddc_set(dev, val):
51 53
     if (val < 0) or (val > 100):
52 54
         raise ValueError("out of range")
53 55
 
54
-    r = subprocess.run(["ddcutil", "-d", str(dev), "-t", "setvcp", "10", str(val)], capture_output=True)
56
+    if dev.startswith("/dev/i2c-"):
57
+        cmd = ["ddcutil", "--noverify", "--bus", dev.replace("/dev/i2c-", ""), "-t", "setvcp", "10", str(val)]
58
+    else:
59
+        cmd = ["ddcutil", "--noverify", "-d", str(dev), "-t", "setvcp", "10", str(val)]
60
+
61
+    r = subprocess.run(cmd, capture_output=True)
55 62
     if r.returncode != 0:
56 63
         raise ValueError("ddcutil returned {} \"{}\"".format(r.returncode, r.stderr.decode("utf-8")))
57 64
 

Loading…
Cancel
Save