暫無描述
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.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. if len(disps) <= 0:
  32. raise ValueError("no displays found")
  33. for d in disps:
  34. # select i2c bus if available, id otherwise
  35. if "bus" in d:
  36. d["_id"] = d["bus"]
  37. else:
  38. d["_id"] = d["id"]
  39. # get initial value
  40. d["prev"] = ddc.ddc_get(d["_id"])
  41. print("Display \"{}\" ({}) at {}".format(d["name"], d["_id"], d["prev"]))
  42. brightness = lux.read_brightness(usb)
  43. print("Brightness:", brightness)
  44. print()
  45. print("{}: Starting main loop".format(time.ctime()))
  46. print()
  47. while True:
  48. brightness = filter_lux(brightness, lux.read_brightness(usb))
  49. for d in disps:
  50. val = lux_to_disp(d["name"], brightness)
  51. if val != d["prev"]:
  52. d["prev"] = val
  53. print("{}: Setting \"{}\" to {}".format(time.ctime(), d["name"], val))
  54. ddc.ddc_set(d["_id"], val)
  55. time.sleep(1.0)