Skip to content

Commit

Permalink
Release 1.8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
linusg committed Sep 17, 2017
1 parent e96e7d6 commit dbc9e04
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changes
=======

1.8.0
-----

- Fix permission error inconsistency across Python versions
- Update link to PyPI

1.7.1
-----

Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ rpi-backlight
:alt: Issues

.. image:: https://img.shields.io/pypi/v/rpi_backlight.svg
:target: https://pypi.python.org/pypi/rpi_backlight
:target: https://pypi.org/project/rpi_backlight/
:alt: Version

.. image:: https://img.shields.io/github/license/mashape/apistatus.svg
Expand Down
28 changes: 18 additions & 10 deletions rpi_backlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,32 @@
import argparse

__author__ = "Linus Groh"
__version__ = "1.7.1"
__version__ = "1.8.0"
PATH = "/sys/class/backlight/rpi_backlight/"


def _perm_denied():
print("This program must be run as root!")
print("A permission error occured. You must either run this program as root or change the")
print("permissions for the backlight access as described on the GitHub page.")
sys.exit()


def _get_value(name):
try:
with open(os.path.join(PATH, name), "r") as f:
return f.read()
except PermissionError:
_perm_denied()
except (OSError, IOError) as err:
if err.errno == 13:
_perm_denied()


def _set_value(name, value):
with open(os.path.join(PATH, name), "w") as f:
f.write(str(value))
try:
with open(os.path.join(PATH, name), "w") as f:
f.write(str(value))
except (OSError, IOError) as err:
if err.errno == 13:
_perm_denied()


def get_actual_brightness():
Expand All @@ -42,6 +48,7 @@ def get_actual_brightness():
:return: Actual brightness value.
:rtype: int
"""

return int(_get_value("actual_brightness"))


Expand All @@ -51,6 +58,7 @@ def get_max_brightness():
:return: Maximum possible brightness value.
:rtype: int
"""

return int(_get_value("max_brightness"))


Expand All @@ -60,6 +68,7 @@ def get_power():
:return: Whether the diplay is powered on or not.
:rtype: bool
"""

return not int(_get_value("bl_power"))


Expand All @@ -70,6 +79,7 @@ def set_brightness(value, smooth=False, duration=1):
:param smooth: Boolean if the brightness should be faded or not
:param duration: Fading duration in seconds
"""

max_value = get_max_brightness()
if not isinstance(value, int):
raise ValueError(
Expand Down Expand Up @@ -98,10 +108,8 @@ def set_power(on):
:param on: Boolean whether the display should be powered on or not
"""
try:
_set_value("bl_power", int(not on))
except PermissionError:
_perm_denied()

_set_value("bl_power", int(not on))


def cli():
Expand Down

0 comments on commit dbc9e04

Please sign in to comment.