Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onboard LEDs support #75

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions OPi/GPIO.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,45 @@ def my_callback_two(channel):
This lists all the files associated with pwmchip0. if you successfully created an object in step 3 you will see a pwm object (for example pwm0).
The number listed after pwm is the pin number used in PWM_pin.

LEDs:
-----
This code was written on an OrangePi PC+ so the following examples are based on that, however any single board
computer running linux that has a onboard leds be able to use this.

To list all LEDs on the system:

.. code:: bash
ls -l /sys/class/leds/

This will show all of the LEDs on the system for example orangepi:green:pwr.

1. To turn LED ON:

.. code:: python

GPIO.setled(GPIO.RED, GPIO.HIGH)
# or
GPIO.setled(GPIO.RED, 1)
# or
GPIO.setled(GPIO.RED, True)

2. To turn LED OFF:

.. code:: python

GPIO.setled(GPIO.RED, GPIO.LOW)
# or
GPIO.setled(GPIO.RED, 0)
# or
GPIO.setled(GPIO.RED, False)

3. To change several LEDs states at the same time:

.. code:: python

leds_list = [GPIO.RED, GPIO.GREEN] # also works with tuples
GPIO.led(leds_list, GPIO.HIGH) # sets both LEDs ON
GPIO.led(leds_list, (GPIO.HIGH, GPIO.LOW)) # sets first LED ON and second LED OFF

Methods
-------
Expand All @@ -360,6 +399,7 @@ def my_callback_two(channel):
from OPi.constants import NONE, RISING, FALLING, BOTH # noqa: F401
from OPi.constants import BCM, BOARD, SUNXI, CUSTOM
from OPi.constants import PUD_UP, PUD_DOWN, PUD_OFF # noqa: F401
from OPi.constants import RED, GREEN # LEDs
from OPi.pin_mappings import get_gpio_pin, set_custom_pin_mappings
from OPi import event, sysfs

Expand Down Expand Up @@ -525,6 +565,28 @@ def output(channel, state):
pin = get_gpio_pin(_mode, channel)
return sysfs.output(pin, state)

def setled(led, state):
"""
Set the state of a onboard LEDs.

:param led: :py:attr:`GPIO.RED` or :py:attr:`GPIO.GREEN`.
:param state: can be :py:attr:`0` / :py:attr:`GPIO.LOW` / :py:attr:`False`
or :py:attr:`1` / :py:attr:`GPIO.HIGH` / :py:attr:`True`.

**Several LEDs:**
You can change state of both LEDs the same call. For example:

.. code:: python

leds_list = [GPIO.RED, GPIO.GREEN] # also works with tuples
GPIO.led(leds_list, GPIO.HIGH) # sets both LEDs ON
GPIO.led(leds_list, (GPIO.HIGH, GPIO.LOW)) # sets first LED ON and second LED OFF
"""
if isinstance(led, list):
for l in led:
setled(l, state)
else:
return sysfs.setled(led, state)

def wait_for_edge(channel, trigger, timeout=-1):
"""
Expand Down
4 changes: 4 additions & 0 deletions OPi/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ def __setattr__(self, name, value):
GPIO.FALLING = 2
GPIO.BOTH = 3

# LEDs
GPIO.RED = 'orangepi:red:status'
GPIO.GREEN = 'orangepi:green:pwr'

sys.modules[__name__] = GPIO
15 changes: 14 additions & 1 deletion OPi/sysfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from contextlib import contextmanager
from OPi.constants import HIGH, LOW, IN, OUT, \
NONE, RISING, FALLING, BOTH
NONE, RISING, FALLING, BOTH, RED, GREEN

import os
import time
Expand Down Expand Up @@ -84,6 +84,19 @@ def edge(pin, trigger):
with open(path, "w") as fp:
fp.write(opts[trigger])

@contextmanager
def brightness_descriptor(pin, mode="r"):
path = "/sys/class/leds/{0}/brightness".format(pin)
await_permissions(path)
with open(os.path.abspath(path), mode) as fp:
yield fp

def setled(led, value):
assert led in [RED, GREEN]
str_value = "1" if value else "0"
with brightness_descriptor(led, "w") as fp:
fp.write(str_value)

# Hardware PWM functionality:
# resources: https://developer.toradex.com/knowledge-base/pwm-linux & https://www.faschingbauer.me/trainings/material/soup/hardware/pwm/topic.html

Expand Down
10 changes: 10 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,13 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Installation
-------
::

sudo apt-get update
sudo apt-get install python3-dev python3-setuptools git
git clone https://github.com/alexgrach/OPI.GPIO
cd OPI.GPIO
sudo python setup.py install