Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
linusg authored Oct 2, 2016
1 parent b9b68f4 commit 9e40118
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
74 changes: 74 additions & 0 deletions rpi_backlight.py
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
32 changes: 32 additions & 0 deletions setup.py
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"],
)

0 comments on commit 9e40118

Please sign in to comment.