From 9e40118e1b177afab574aff4dbe83b1d6d66e3a5 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sun, 2 Oct 2016 20:15:13 +0200 Subject: [PATCH] Add files via upload --- rpi_backlight.py | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ setup.py | 32 +++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 rpi_backlight.py create mode 100644 setup.py diff --git a/rpi_backlight.py b/rpi_backlight.py new file mode 100644 index 0000000..6323f0d --- /dev/null +++ b/rpi_backlight.py @@ -0,0 +1,74 @@ +import time +import os +import sys + +__author__ = "Linus Groh" +__version__ = "1.0.0" +path = "/sys/class/backlight/rpi_backlight/" + + +def _perm_denied(): + print("This program must be run as root!") + sys.exit() + + +def get_actual_brightness(): + global path + with open(os.path.join(path, "actual_brightness"), "r") as f: + return int(f.read()) + + +def get_max_brightness(): + global path + with open(os.path.join(path, "max_brightness"), "r") as f: + return int(f.read()) + + +def set_brightness(value, smooth=True): + global path + if not 10 < value <= get_max_brightness() or type(value) != int: + raise ValueError("value must be between 11 and {}, got {}".format(max_value, value)) + + def run(value): + try: + with open(os.path.join(path, "brightness"), "w") as f: + f.write(str(value)) + except PermissionError: + _perm_denied() + + if smooth: + v = get_actual_brightness() + while v != value: + v = v - 1 if v > value else v + 1 + + run(v) + time.sleep(0.01) + else: + run(value) + + +def set_power(on): + try: + with open(os.path.join(path, "bl_power"), "w") as f: + if on: + f.write("0") + else: + f.write("1") + except PermissionError: + _perm_denied() + + +if __name__ == "__main__": + if sys.version.startswith("2"): + input = raw_input + + success = False + while not success: + v = input("Enter value of brightness (between 11 and 255): ") + try: + v = int(v) + except ValueError: + continue + if 10 < v < 256: + set_brightness(v) + success = True diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..68ebb36 --- /dev/null +++ b/setup.py @@ -0,0 +1,32 @@ +from setuptools import setup +from rpi_backlight import __version__ + + +with open("README.rst", "r") as f: + long_description = f.read() + +setup(name="rpi_backlight", + py_modules=["rpi_backlight"], + version=__version__, + description="Python library containing some functions to control the 7\" touch display from the Raspberry Pi foundation.", + long_description=long_description, + author="Linus Groh", + license="MIT", + author_email="mail@linusgroh.de", + url="https://github.com/linusg/rpi-backlight", + download_url="https://pypi.python.org/pypi/rpi_backlight", + keywords=["raspberry pi", "display", "touchscreen", "brightness"], + classifiers=[ + "Development Status :: 3 - Alpha", + "Intended Audience :: Developers", + "License :: OSI Approved :: MIT License", + "Operating System :: POSIX :: Linux", + "Programming Language :: Python", + "Programming Language :: Python :: 2", + "Programming Language :: Python :: 3", + "Topic :: System :: Hardware", + "Topic :: Multimedia", + "Topic :: Utilities", + "Topic :: Software Development :: Libraries", + "Topic :: Software Development :: Libraries :: Python Modules"], + )