-
Notifications
You must be signed in to change notification settings - Fork 0
/
change-backlight.py
executable file
·52 lines (35 loc) · 1.43 KB
/
change-backlight.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python3
from math import log10
import sys
import subprocess
def get_backlight():
return float(subprocess.check_output(["xbacklight", "-get"]))
def set_backlight(backlight):
subprocess.call(["xbacklight", "-set", str(backlight)])
def backlight_to_step(backlight, backlight_min, backlight_max, steps):
x_min = log10(backlight_min)
x_max = log10(backlight_max)
return round(log10(backlight) / (x_max - x_min) * steps)
def step_to_backlight(step, backlight_min, backlight_max, steps):
x_min = log10(backlight_min)
x_max = log10(backlight_max)
x = step / steps * (x_max - x_min)
backlight = round(max(min(10 ** x, backlight_max), backlight_min))
return backlight
if __name__ == "__main__":
backlight_min = 2
backlight_max = 100
steps = 20
if len(sys.argv) < 2 or sys.argv[1] not in ["-inc", "-dec"]:
print("usage:\n\t{0} -inc / -dec".format(sys.argv[0]))
sys.exit(0)
current_backlight = get_backlight()
current_step = backlight_to_step(current_backlight, backlight_min, backlight_max, steps)
action = sys.argv[1]
if action == "-inc":
new_step = current_step + 1
elif action == "-dec":
new_step = current_step - 1
new_backlight = step_to_backlight(new_step, backlight_min, backlight_max, steps)
print("Current backlight: {0}\nChanging to: {1}".format(current_backlight, new_backlight))
set_backlight(new_backlight)