-
-
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
7 changed files
with
137 additions
and
65 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
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
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
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
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
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 |
---|---|---|
@@ -1,16 +1,20 @@ | ||
""" | ||
*A Python module for controlling power and brightness of the official Raspberry Pi 7" touch display.* | ||
A Python module for controlling power and brightness | ||
of the official Raspberry Pi 7" touch display. | ||
:Author: Linus Groh ([email protected]) | ||
Ships with a CLI, GUI and Python API. | ||
:Author: Linus Groh | ||
:License: MIT license | ||
""" | ||
from __future__ import print_function | ||
import time | ||
import os | ||
import sys | ||
import argparse | ||
|
||
__author__ = "Linus Groh" | ||
__version__ = "1.5.0" | ||
__version__ = "1.6.0" | ||
PATH = "/sys/class/backlight/rpi_backlight/" | ||
|
||
|
||
|
@@ -26,6 +30,7 @@ def _get_value(name): | |
except PermissionError: | ||
_perm_denied() | ||
|
||
|
||
def _set_value(name, value): | ||
with open(os.path.join(PATH, name), "w") as f: | ||
f.write(str(value)) | ||
|
@@ -46,21 +51,27 @@ def get_power(): | |
return not _get_value("bl_power") | ||
|
||
|
||
def set_brightness(value, smooth=True): | ||
def set_brightness(value, smooth=False, duration=1): | ||
"""Set the display brightness.""" | ||
max_value = get_max_brightness() | ||
if type(value) != int: | ||
raise ValueError("integer required, got '{}'".format(type(value))) | ||
if not isinstance(value, int): | ||
raise ValueError( | ||
"integer required, got '{}'".format(type(value))) | ||
if not 10 < value <= max_value: | ||
raise ValueError("value must be between 11 and {}, got {}".format(max_value, value)) | ||
|
||
raise ValueError( | ||
"value must be between 11 and {}, got {}".format(max_value, value)) | ||
|
||
if smooth: | ||
if not isinstance(duration, (int, float)): | ||
raise ValueError( | ||
"integer or float required, got '{}'".format(type(duration))) | ||
actual = get_actual_brightness() | ||
diff = abs(value-actual) | ||
while actual != value: | ||
actual = actual - 1 if actual > value else actual + 1 | ||
|
||
_set_value("brightness", actual) | ||
time.sleep(0.01) | ||
time.sleep(duration/diff) | ||
else: | ||
_set_value("brightness", value) | ||
|
||
|
@@ -75,21 +86,50 @@ def set_power(on): | |
|
||
def cli(): | ||
"""Start the command line interface.""" | ||
global input | ||
if sys.version.startswith("2"): | ||
input = raw_input | ||
|
||
while True: | ||
value = input("Enter value of brightness (between 11 and 255): ") | ||
try: | ||
value = int(value) | ||
if 10 < value < 256: | ||
break | ||
else: | ||
continue | ||
except ValueError: | ||
continue | ||
set_brightness(value) | ||
parser = argparse.ArgumentParser( | ||
description="Control power and brightness of the " | ||
"official Raspberry Pi 7\" touch display.") | ||
parser.add_argument("-b", "--brightness", metavar='VALUE', | ||
type=int, choices=range(11, 256), | ||
help="set the display brightness to VALUE (11-255)") | ||
parser.add_argument("-d", "--duration", type=int, default=1, | ||
help="fading duration in seconds") | ||
parser.add_argument("-s", "--smooth", action='store_true', | ||
help="fade the display brightness, see -d/--duration") | ||
parser.add_argument("--on", action='store_true', | ||
help="set the display powered on") | ||
parser.add_argument("--off", action='store_true', | ||
help="set the display powered off") | ||
parser.add_argument("--max-brightness", action='store_true', | ||
help="get the maximum display brightness") | ||
parser.add_argument("--actual-brightness", action='store_true', | ||
help="get the actual display brightness") | ||
parser.add_argument("--power", action='store_true', | ||
help="get the current power state") | ||
args = parser.parse_args() | ||
|
||
if all(arg in (False, None) for arg in ( | ||
args.off, args.on, args.brightness, args.max_brightness, | ||
args.actual_brightness, args.power)): | ||
parser.print_help() | ||
|
||
if args.off is True: | ||
set_power(False) | ||
|
||
if args.on is True: | ||
set_power(True) | ||
|
||
if args.brightness: | ||
set_brightness(args.brightness, args.smooth, args.duration) | ||
|
||
if args.max_brightness: | ||
print(get_max_brightness()) | ||
|
||
if args.actual_brightness: | ||
print(get_actual_brightness()) | ||
|
||
if args.power: | ||
print(get_power()) | ||
|
||
|
||
def gui(): | ||
|
@@ -106,11 +146,11 @@ def gui(): | |
|
||
ad1 = Gtk.Adjustment(value=get_actual_brightness(), lower=11, upper=255) | ||
scale = Gtk.Scale(orientation=Gtk.Orientation.HORIZONTAL, adjustment=ad1) | ||
def on_scale_changed(scale, _): | ||
value = int(scale.get_value()) | ||
|
||
def on_scale_changed(s, _): | ||
value = int(s.get_value()) | ||
set_brightness(value) | ||
|
||
scale.connect("button-release-event", on_scale_changed) | ||
scale.connect("key_release_event", on_scale_changed) | ||
scale.connect("scroll-event", on_scale_changed) | ||
|
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 |
---|---|---|
|
@@ -8,22 +8,25 @@ | |
setup(name="rpi_backlight", | ||
py_modules=["rpi_backlight"], | ||
version=__version__, | ||
description="A Python module for controlling power and brightness of the official Raspberry Pi 7\" touch display.", | ||
description="A Python module for controlling power and brightness " | ||
"of the official Raspberry Pi 7\" touch display.", | ||
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"], | ||
keywords=["raspberry pi", "display", "touchscreen", | ||
"brightness", "backlight"], | ||
classifiers=[ | ||
"Development Status :: 3 - Alpha", | ||
"Development Status :: 5 - Production/Stable", | ||
"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", | ||
"Topic :: System :: Hardware", | ||
"Topic :: Multimedia", | ||
"Topic :: Utilities", | ||
|
@@ -35,4 +38,4 @@ | |
"rpi-backlight-gui = rpi_backlight:gui", | ||
], | ||
}, | ||
) | ||
) |