-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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="[email protected]", | ||
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"], | ||
) |