Skip to content

Instantly share code, notes, and snippets.

@FZX
Last active November 16, 2016 19:02
Show Gist options
  • Save FZX/499df015bb2f19c8c3054f09381b6124 to your computer and use it in GitHub Desktop.
Save FZX/499df015bb2f19c8c3054f09381b6124 to your computer and use it in GitHub Desktop.
Ubuntu / Linux Mint Brightness control. I could not find real fix for problem with keys changing brightness level on my laptop. So wrote little Python 3 script. Add alias to this file in .bashrc
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2016 FZX <[email protected]>
#
# Distributed under terms of the GPL v2.0 license.
"""
Controlling brightness for nv_backlight.
On my laptop does not work brightness control
with keys. So this little script will help me
to do it manually from terminal.
Make alias to this file in .bashrc
alias br='sudo python3 brightness.py'
"""
import os
import sys
def current_level():
with open("/sys/class/backlight/nv_backlight/brightness", "r") as f:
level = f.read()
return level
def update_level(value):
with open("/sys/class/backlight/nv_backlight/brightness", "w") as f:
f.write(str(value))
print("Brightness level set to {}".format(value))
def get_new_value():
error_message = "Please use only this format 'br 30'! Max level is 100!"
try:
new_value = int(sys.argv[1])
if (len(sys.argv) > 2) or (new_value > 100):
print(error_message)
else:
return abs(new_value)
except:
print(error_message)
def notify(value):
os.popen("sudo -u mint notify-send -i /usr/share/icons/Mint-X/devices/scalable/computer-symbolic.svg 'Brightness' 'Level set to {}'".
format(value))
def main():
if len(sys.argv) < 2:
print("Current brightness level is:", current_level())
elif sys.argv[1] == "+":
if current_level().strip() != "100":
new_level = int(current_level()) + 10
update_level(new_level)
notify(new_level)
elif sys.argv[1] == "-":
if current_level().strip() > "10":
new_level = int(current_level()) - 10
update_level(new_level)
notify(new_level)
else:
level = get_new_value()
if level:
update_level(level)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment