Nessuna descrizione
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ddc.py 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env python
  2. import subprocess
  3. def ddc_detect():
  4. r = subprocess.run(["ddcutil", "detect", "-t"], capture_output=True)
  5. if r.returncode != 0:
  6. raise ValueError("ddcutil returned {} \"{}\"".format(r.returncode))
  7. out = []
  8. displays = r.stdout.decode("utf-8").split("\n\n")
  9. for disp in displays:
  10. data = disp.split("\n")
  11. field = {}
  12. if len(data) < 4:
  13. continue
  14. for d in data:
  15. v = d.split()
  16. if v[0] == "Display":
  17. field["id"] = v[1]
  18. elif (v[0] == "I2C") and (v[1] == "bus:"):
  19. field["bus"] = v[2]
  20. elif v[0] == "Monitor:":
  21. field["name"] = ' '.join(v[1:])
  22. # if id is not there it's an "Ivalid display"
  23. if "id" in field:
  24. out.append(field)
  25. return out
  26. def ddc_get(dev):
  27. if dev.startswith("/dev/i2c-"):
  28. cmd = ["ddcutil", "--skip-ddc-checks", "--bus", dev.replace("/dev/i2c-", ""), "-t", "getvcp", "10"]
  29. else:
  30. cmd = ["ddcutil", "-d", str(dev), "-t", "getvcp", "10"]
  31. r = subprocess.run(cmd, capture_output=True)
  32. if r.returncode != 0:
  33. raise ValueError("ddcutil returned {} \"{}\"".format(r.returncode, r.stderr.decode("utf-8")))
  34. s = r.stdout.decode("utf-8").split()
  35. if (s[0] != "VCP") or (s[1] != "10") or (s[2] != "C") or (s[4] != "100"):
  36. raise ValueError("unexpected identifier \"{}\"".format(r.stdout.decode("utf-8")))
  37. return int(s[3])
  38. def ddc_set(dev, val):
  39. val = int(val)
  40. if (val < 0) or (val > 100):
  41. raise ValueError("out of range")
  42. if dev.startswith("/dev/i2c-"):
  43. cmd = ["ddcutil", "--noverify", "--bus", dev.replace("/dev/i2c-", ""), "-t", "setvcp", "10", str(val)]
  44. else:
  45. cmd = ["ddcutil", "--noverify", "-d", str(dev), "-t", "setvcp", "10", str(val)]
  46. r = subprocess.run(cmd, capture_output=True)
  47. if r.returncode != 0:
  48. raise ValueError("ddcutil returned {} \"{}\"".format(r.returncode, r.stderr.decode("utf-8")))
  49. if __name__ == "__main__":
  50. devs = ddc_detect()
  51. for dev in devs:
  52. print("Display:", dev["id"], dev["name"])
  53. b = ddc_get(dev["id"])
  54. print("Brightness:", b)
  55. new = 50
  56. if b == 50:
  57. new = 60
  58. print("Setting to {}...".format(new))
  59. ddc_set(dev["id"], new)
  60. b = ddc_get(dev["id"])
  61. print("Brightness:", b)
  62. print()