Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import sys, tty, termios, math, decimal
- current_backlight_file = '/sys/class/backlight/intel_backlight/brightness'
- max_backlight_file = '/sys/class/backlight/intel_backlight/max_brightness'
- logical_steps = 20
- def get3ch():
- fd = sys.stdin.fileno()
- old_settings = termios.tcgetattr(fd)
- try:
- tty.setraw(fd)
- ch = sys.stdin.read(3)
- finally:
- termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
- return ch
- def get():
- while(1):
- k=get3ch()
- if k!='':break
- if k=='\x1b[A':
- return "up"
- elif k=='\x1b[B':
- return "down"
- elif k=='\x1b[C':
- return "right"
- elif k=='\x1b[D':
- return "left"
- else:
- return "not an arrow key!"
- def file_read_int(fname):
- with open(fname) as f:
- data = f.read()
- return int(data)
- def file_write_int(fname, n):
- with open(fname, 'w') as f:
- f.write(format(n))
- def get_hw_current_backlight():
- max_hw_bl = file_read_int(max_backlight_file)
- curr_hw_bl = file_read_int(current_backlight_file)
- current_logical_brightness = hw_to_lg(max_hw_bl, curr_hw_bl)
- if current_logical_brightness == 0: current_logical_brightness = 1
- return current_logical_brightness
- def set_logical_backlight_level(n):
- if n < 1 or n > logical_steps:
- return
- max_hw_bl = file_read_int(max_backlight_file)
- hw_bl = lg_to_hw(max_hw_bl, n)
- if hw_bl >= 1 and hw_bl <= max_backlight_file:
- print("New brightness: {}".format(n))
- file_write_int(current_backlight_file, hw_bl)
- def hw_to_lg(max_hw_bl, val):
- base = math.pow(max_hw_bl, (1.0/logical_steps))
- return int(round(math.log(val, base)))
- def lg_to_hw(max_hw_bl, val):
- base = math.pow(max_hw_bl, (1.0/logical_steps))
- return int(round(math.pow(base, val)))
- while True:
- key = get()
- curr_bl = get_hw_current_backlight()
- if key == "up" or key == "right":
- set_logical_backlight_level(curr_bl + 1)
- elif key == "down" or key == "left":
- set_logical_backlight_level(curr_bl - 1)
- else:
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement