No Description
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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. # Display X
  15. num = data[0].split()
  16. if num[0] != "Display":
  17. #raise ValueError("unexpected identifier (\"{}\" != \"Display\")".format(num[0]))
  18. continue
  19. field["id"] = num[1]
  20. # Monitor: name ...
  21. name = data[3].split()
  22. if name[0] != "Monitor:":
  23. #raise ValueError("unexpected identifier (\"{}\" != \"Monitor:\")".format(name[0]))
  24. continue
  25. field["name"] = ' '.join(name[1:])
  26. out.append(field)
  27. return out
  28. def ddc_get(dev):
  29. r = subprocess.run(["ddcutil", "-d", str(dev), "-t", "getvcp", "10"], capture_output=True)
  30. if r.returncode != 0:
  31. raise ValueError("ddcutil returned {} \"{}\"".format(r.returncode, r.stderr.decode("utf-8")))
  32. s = r.stdout.decode("utf-8").split()
  33. if (s[0] != "VCP") or (s[1] != "10") or (s[2] != "C") or (s[4] != "100"):
  34. raise ValueError("unexpected identifier \"{}\"".format(r.stdout.decode("utf-8")))
  35. return int(s[3])
  36. def ddc_set(dev, val):
  37. val = int(val)
  38. if (val < 0) or (val > 100):
  39. raise ValueError("out of range")
  40. r = subprocess.run(["ddcutil", "-d", str(dev), "-t", "setvcp", "10", str(val)], capture_output=True)
  41. if r.returncode != 0:
  42. raise ValueError("ddcutil returned {} \"{}\"".format(r.returncode, r.stderr.decode("utf-8")))
  43. if __name__ == "__main__":
  44. devs = ddc_detect()
  45. for dev in devs:
  46. print("Display:", dev["id"], dev["name"])
  47. b = ddc_get(dev["id"])
  48. print("Brightness:", b)
  49. new = 50
  50. if b == 50:
  51. new = 60
  52. print("Setting to {}...".format(new))
  53. ddc_set(dev["id"], new)
  54. b = ddc_get(dev["id"])
  55. print("Brightness:", b)
  56. print()