Geen omschrijving
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.

brightness.py 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env python
  2. import lux
  3. import ddc
  4. import time
  5. filter_fact = 0.9
  6. c_in = 0.6, -60.0, # in_a, in_b
  7. calibration = {
  8. "HPN:HP 27xq:CNK1072BJY": [
  9. 1.0, 30.0, # out_a, out_b
  10. ],
  11. "MSI:MSI G27CQ4:": [
  12. 1.0, 20.0, # out_a, out_b
  13. ],
  14. }
  15. def cal(v, c):
  16. # out = out_b + out_a * in_a * max(0, in_b + in)
  17. return c[1] + c[0] * c_in[0] * max(0, c_in[1] + v)
  18. def filter_lux(old, new):
  19. return (old * filter_fact) + (new * (1.0 - filter_fact))
  20. def lux_to_disp(name, val):
  21. if name in calibration:
  22. val = cal(int(val), calibration[name])
  23. else:
  24. raise ValueError("no calibration for \"{}\"".format(name))
  25. val = int(val)
  26. return min(max(val, 0), 100)
  27. if __name__ == "__main__":
  28. usb = lux.usb_init()
  29. lux.check_connection(usb)
  30. disps = ddc.ddc_detect()
  31. brightness = lux.read_brightness(usb)
  32. print("Brightness:", brightness)
  33. for d in disps:
  34. d["prev"] = ddc.ddc_get(d["id"])
  35. print("Display \"{}\" at {}".format(d["name"], d["prev"]))
  36. print()
  37. print("{}: Starting main loop".format(time.ctime()))
  38. print()
  39. while True:
  40. brightness = filter_lux(brightness, lux.read_brightness(usb))
  41. for d in disps:
  42. val = lux_to_disp(d["name"], brightness)
  43. if val != d["prev"]:
  44. d["prev"] = val
  45. print("{}: Setting \"{}\" to {}".format(time.ctime(), d["name"], val))
  46. ddc.ddc_set(d["id"], val)
  47. time.sleep(1.0)