Skip to content

Instantly share code, notes, and snippets.

@YakBarber
Last active April 19, 2019 16:46
Show Gist options
  • Save YakBarber/41f71f066896b0310cc5c0de0ec77d57 to your computer and use it in GitHub Desktop.
Save YakBarber/41f71f066896b0310cc5c0de0ec77d57 to your computer and use it in GitHub Desktop.
Small script to control brightness on laptop with Intel chipset running Ubuntu. Fn brightness keys and xbacklight stopped working, but this worked. Pass an int between 0 and 100 representing the percent brightness you want. Call without args to get current percentage.
#!/usr/bin/env python3
import os
import sys
import subprocess
from pathlib import Path
if os.geteuid()!=0:
args = ['sudo'] + sys.argv
os.execlp('sudo',*args)
bpath = Path("/sys/class/backlight/intel_backlight/")
bmax = int((bpath/"max_brightness").read_text())
bcur = int((bpath/"brightness").read_text())
if len(sys.argv)<2:
print(round(bcur/bmax*100))
sys.exit(0)
else:
perc = int(sys.argv[1])
val = str(int(perc/100*bmax))
cmd = "echo " + val + " > " + str(bpath/"brightness")
print(subprocess.getoutput(cmd))
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment