Bez popisu
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 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env python
  2. import lux
  3. import ddc
  4. import time
  5. import influx
  6. filter_fact = 0.99
  7. c_in = 0.6, -30.0, # in_a, in_b
  8. calibration = {
  9. "HPN:HP 27xq:CNK1072BJY": [
  10. 1.0, 10.0, # out_a, out_b
  11. ],
  12. "MSI:MSI G27CQ4:": [
  13. 1.0, 0.0, # out_a, out_b
  14. ],
  15. }
  16. def cal(v, c):
  17. # out = out_b + out_a * in_a * max(0, in_b + in)
  18. return c[1] + c[0] * c_in[0] * max(0, c_in[1] + v)
  19. def filter_lux(old, new):
  20. return (old * filter_fact) + (new * (1.0 - filter_fact))
  21. def lux_to_disp(name, val):
  22. if name in calibration:
  23. val = cal(int(val), calibration[name])
  24. else:
  25. raise ValueError("no calibration for \"{}\"".format(name))
  26. val = int(val)
  27. return min(max(val, 0), 100)
  28. if __name__ == "__main__":
  29. usb = lux.usb_init()
  30. lux.check_connection(usb)
  31. disps = ddc.ddc_detect()
  32. if len(disps) <= 0:
  33. raise ValueError("no displays found")
  34. for d in disps:
  35. # select i2c bus if available, id otherwise
  36. if "bus" in d:
  37. d["_id"] = d["bus"]
  38. else:
  39. d["_id"] = d["id"]
  40. # get initial value
  41. d["prev"] = ddc.ddc_get(d["_id"])
  42. print("Display \"{}\" ({}) at {}".format(d["name"], d["_id"], d["prev"]))
  43. brightness = lux.read_brightness(usb)
  44. print("Brightness:", brightness)
  45. last_brightness = brightness
  46. print()
  47. print("{}: Starting main loop".format(time.ctime()))
  48. print()
  49. time_brightness = time.time()
  50. time_displays = time.time()
  51. while True:
  52. # read brightness at approx. 1Hz with low-pass filtering
  53. time.sleep(1.0)
  54. brightness = filter_lux(brightness, lux.read_brightness(usb))
  55. # print brightness changes at most every 5s
  56. if (time.time() - time_brightness) > 5.0:
  57. time_brightness = time.time()
  58. if int(brightness) != last_brightness:
  59. last_brightness = int(brightness)
  60. print("{}: Brightness: {}".format(time.ctime(), last_brightness))
  61. try:
  62. influx.write("brightness,location=pc-back", "lux", brightness)
  63. for d in disps:
  64. name = '_'.join(d["name"].split())
  65. influx.write("brightness,location=" + name, "backlight", d["prev"])
  66. except:
  67. pass
  68. # set displays at most every 10s
  69. if (time.time() - time_displays) > 10.0:
  70. time_displays = time.time()
  71. for d in disps:
  72. val = lux_to_disp(d["name"], brightness)
  73. if val != d["prev"]:
  74. try:
  75. print("{}: Setting \"{}\" to {}".format(time.ctime(), d["name"], val))
  76. ddc.ddc_set(d["_id"], val)
  77. d["prev"] = val
  78. except Exception as e:
  79. print(e)