Skip to content

Commit

Permalink
Release 1.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
linusg committed Apr 29, 2017
1 parent c428789 commit 4f969f5
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 65 deletions.
9 changes: 9 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
Changes
=======

1.6.0
-----

- Added ``duration`` parameter to ``set_brightness``
- ``smooth`` now defaults to ``False``
- Huge improvements on CLI
- Fixed renamed function in examples
- Minor code and readme improvements

1.5.0
-----

Expand Down
30 changes: 13 additions & 17 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ Features
- Command line interface
- Graphical user interface


Requirements
------------

Expand Down Expand Up @@ -71,7 +70,6 @@ Insert the line::

SUBSYSTEM=="backlight",RUN+="/bin/chmod 666 /sys/class/backlight/%k/brightness /sys/class/backlight/%k/bl_power"


Usage
-----

Expand All @@ -81,12 +79,11 @@ API
Example in a Python shell::

>>> import rpi_backlight as bl
>>> bl.set_brightness(20)
>>> bl.set_brightness(255)
>>> bl.set_brightness(20, smooth=False)
>>> bl.set_brightness(20, smooth=True, duration=3)
>>> bl.get_max_brightness()
255
>>> bl.get_current_brightness()
>>> bl.get_actual_brightness()
20
>>> bl.get_power()
True
Expand All @@ -97,12 +94,17 @@ Example in a Python shell::
CLI
***

Open a terminal and run ``rpi-backlight`` as root.
Open a terminal and run ``rpi-backlight`` as root::

**NOTE: The command line interface could change in the future, providing more otions instead of being just an user input application!**

.. image:: https://raw.githubusercontent.com/linusg/rpi-backlight/master/docs/cli.png
:alt: Command Line Interface
$ rpi-backlight -b 255
$ rpi-backlight -b 20 -s -d 3
$ rpi-backlight --max-brightness
255
$ rpi-backlight --actual-brightness
20
$ rpi-backlight --power
True
$ rpi-backlight --off

GUI
***
Expand All @@ -115,13 +117,7 @@ Open a terminal and run ``rpi-backlight-gui`` as root.
Todo
----

- Allow to set the brightness fading duration in ``set_brightness(value)``
- (*Create a really simple GUI in* ``pygobject`` *to change the display brightness, maybe just a scale/slider*)

- Most of it done, but not tested across several Python versions and distros
- Ensure it runs on the Pi under both Python 2 and 3

I would be happy if you can help shortening this todo-list!
Yay, this list is currently empty! Feel free to add ideas here.

External Links
--------------
Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@

# General information about the project.
project = 'rpi-backlight'
copyright = '2016, Linus Groh'
copyright = '2017, Linus Groh'
author = 'Linus Groh'

# The version info for the project you're documenting, acts as replacement for
Expand Down Expand Up @@ -139,7 +139,7 @@
# The name for this set of Sphinx documents.
# "<project> v<release> documentation" by default.
#
# html_title = 'rpi-backlight v1.4.0'
# html_title = 'rpi-backlight v1.6.0'

# A shorter title for the navigation bar. Default is the same as html_title.
#
Expand Down
7 changes: 4 additions & 3 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ Installation process
As an alternative you can get the source code from GitHub_ and install it
using the setup script::

sudo python setup.py install
git clone https://github.com/linusg/rpi-backlight.git
cd rpi-backlight
sudo python3 setup.py install

**Note:** You may need to edit the backlight rules file in order to run the code::

sudo nano /etc/udev/rules.d/backlight-permissions.rules
Expand Down
45 changes: 34 additions & 11 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ Application programming interface

#. Now you can adjust the display power and brightness::

>>> bl.set_brightness(20)
>>> bl.set_brightness(255)
>>> bl.set_brightness(20, smooth=False)
>>> bl.set_brightness(20, smooth=True, duration=3)
>>> bl.get_max_brightness()
255
>>> bl.get_current_brightness()
>>> bl.get_actual_brightness()
20
>>> bl.get_power()
True
Expand All @@ -26,20 +25,44 @@ Application programming interface
Also see the :ref:`rpi-backlight API` for reference.

.. note::
In order to run any of the ``set_`` functions, you have to run your
code as root, e.g. with ``sudo``.
In order to run any of the ``set_`` functions, you have to edit the
backlight permissions as described in the installation process or run
your code as root, e.g. with ``sudo``.

Command line interface
----------------------

Open a terminal and run ``rpi-backlight`` as root, e.g.::

sudo rpi-backlight

**NOTE: The command line interface could change in the future, providing more otions instead of being just an user input application!**

.. image:: https://raw.githubusercontent.com/linusg/rpi-backlight/master/docs/cli.png
:alt: Command Line Interface
$ rpi-backlight -b 255
$ rpi-backlight -b 20 -s -d 3
$ rpi-backlight --max-brightness
255
$ rpi-backlight --actual-brightness
20
$ rpi-backlight --power
True
$ rpi-backlight --off

Parameters::

usage: rpi-backlight [-h] [-b VALUE] [-d DURATION] [-s] [--on] [--off]
[--max-brightness] [--actual-brightness] [--power]

Control power and brightness of the official Raspberry Pi 7" touch display.

optional arguments:
-h, --help show this help message and exit
-b VALUE, --brightness VALUE
set the display brightness to VALUE (11-255)
-d DURATION, --duration DURATION
fading duration in seconds
-s, --smooth fade the display brightness, see -d/--duration
--on set the display powered on
--off set the display powered off
--max-brightness get the maximum display brightness
--actual-brightness get the actual display brightness
--power get the current power state

Graphical user interface
------------------------
Expand Down
96 changes: 68 additions & 28 deletions rpi_backlight.py
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/"


Expand All @@ -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))
Expand All @@ -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)

Expand All @@ -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():
Expand All @@ -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)
Expand Down
11 changes: 7 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -35,4 +38,4 @@
"rpi-backlight-gui = rpi_backlight:gui",
],
},
)
)

0 comments on commit 4f969f5

Please sign in to comment.