Commit 7761f0ec authored by kenkeiras's avatar kenkeiras

Brightness controller script

parent eb566c6a
# Temporary files
*#
*~
*.pyc
# Generated files
*.c
*.o
brightness
# Makefile for creating our standalone Cython program
PYTHON := python
PYVERSION := $(shell $(PYTHON) -c "import sys; print(sys.version[:3])")
SUDO := sudo
INCDIR := $(shell $(PYTHON) -c "from distutils import sysconfig; print(sysconfig.get_python_inc())")
PLATINCDIR := $(shell $(PYTHON) -c "from distutils import sysconfig; print(sysconfig.get_python_inc(plat_specific=True))")
LIBDIR1 := $(shell $(PYTHON) -c "from distutils import sysconfig; print(sysconfig.get_config_var('LIBDIR'))")
LIBDIR2 := $(shell $(PYTHON) -c "from distutils import sysconfig; print(sysconfig.get_config_var('LIBPL'))")
PYLIB := $(shell $(PYTHON) -c "from distutils import sysconfig; print(sysconfig.get_config_var('LIBRARY')[3:-2])")
CC := $(shell $(PYTHON) -c "import distutils.sysconfig; print(distutils.sysconfig.get_config_var('CC'))")
LINKCC := $(shell $(PYTHON) -c "import distutils.sysconfig; print(distutils.sysconfig.get_config_var('LINKCC'))")
LINKFORSHARED := $(shell $(PYTHON) -c "import distutils.sysconfig; print(distutils.sysconfig.get_config_var('LINKFORSHARED'))")
LIBS := $(shell $(PYTHON) -c "import distutils.sysconfig; print(distutils.sysconfig.get_config_var('LIBS'))")
SYSLIBS := $(shell $(PYTHON) -c "import distutils.sysconfig; print(distutils.sysconfig.get_config_var('SYSLIBS'))")
brightness: brightness.o
$(LINKCC) -o $@ $^ -L$(LIBDIR1) -L$(LIBDIR2) -l$(PYLIB) $(LIBS) $(SYSLIBS) $(LINKFORSHARED)
$(SUDO) chown root $@
$(SUDO) chmod +s $@
brightness.o: brightness.c
$(CC) -c $^ -I$(INCDIR) -I$(PLATINCDIR)
CYTHON := cython3
brightness.c: brightness.py
$(CYTHON) --embed brightness.py
all: brightness
clean:
rm -f *.c *.o brightness
#!/usr/bin/env python
# coding: utf-8
import sys
import os
def set_brightness(backlight, value):
return open('/sys/class/backlight/{backlight}/brightness'
.format(backlight=backlight), 'wt').write(str(value))
def get_brightness(backlight):
return int(open('/sys/class/backlight/{backlight}/brightness'
.format(backlight=backlight), 'rt').read())
def get_max_brightness(backlight):
return int(open('/sys/class/backlight/{backlight}/max_brightness'
.format(backlight=backlight), 'rt').read())
def less(backlight):
brightness = get_brightness(backlight)
max_brightness = get_max_brightness(backlight)
set_brightness(backlight, max(0, brightness - max(1, max_brightness / 10)))
def more(backlight):
brightness = get_brightness(backlight)
max_brightness = get_max_brightness(backlight)
set_brightness(backlight, max(0, brightness + max(1, max_brightness / 10)))
def get_device_list():
return os.listdir('/sys/class/backlight/')
def show_help():
print('''brightness.py (less|more) device
Devices:
{}'''.format('\n '.join(get_device_list())))
if __name__ == '__main__':
if len(sys.argv) == 3 and sys.argv[1] in ('less', 'more'):
if sys.argv[2] not in get_device_list():
print('Device {} not found'.format(sys.argv[2]))
elif sys.argv[1] == 'less':
less(sys.argv[2])
elif sys.argv[1] == 'more':
more(sys.argv[2])
else:
show_help()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment