4 4
Schwemmlandebene

tutorial Setting onboard LEDs with rc.local or systemd

Recommended Posts

Hallo,
 
this Mini-Howto describes how to change the behavior of the onboard LEDs during booting. There are many ways to do that. Two ways are shown here, a SysV style using /etc/rc.local and a systemd style using a configuration file in /etc/tmpfiles.d/. The systemd file is earlier interpreted than rc.local.
 
It is tested on Banana Pi M1*/M2/R1 with Debian jessie next (5.0x, 4.4.x).
* partly
 
Which LEDs could be accessed?
root@bananapim2:~# ls -1 /sys/class/leds/
bpi-m2:blue:usr
bpi-m2:green:usr
bpi-m2:red:usr

* The Banana Pi M1 lists the blue LED but it couldn't be set via /etc/tmpfiles.d/ or /etc/rc.local.

 
Which behaviors are possible?
root@bananapim2:~# cat /sys/class/leds/bpi-m2\:green\:usr/trigger 
none rc-feedback kbd-scrollock kbd-numlock kbd-capslock kbd-kanalock kbd-shiftlock kbd-altgrlock kbd-ctrllock kbd-altlock kbd-shiftllock kbd-shiftrlock kbd-ctrlllock kbd-ctrlrlock usb-gadget usb-host [mmc0] heartbeat cpu0 cpu1 cpu2 cpu3 default-on mmc1 rfkill0 
 
Edit /etc/rc.local to set the behavior of the onboard LEDs
root@bananapim2:~# cat /etc/rc.local
#!/bin/sh -e
echo "default-on" > /sys/class/leds/bpi-m2\:red\:usr/trigger
echo "mmc0" > /sys/class/leds/bpi-m2\:green\:usr/trigger
echo "heartbeat" > /sys/class/leds/bpi-m2\:blue\:usr/trigger

exit 0

or  edit /etc/tmpfiles.d/leds.conf.

root@bananapim2:~# cat /etc/tmpfiles.d/leds.conf 
w /sys/class/leds/bpi-m2:red:usr/trigger - - - - default-on
w /sys/class/leds/bpi-m2:green:usr/trigger - - - - mmc0
w /sys/class/leds/bpi-m2:blue:usr/trigger - - - - heartbeat

/etc/rc.local can be executed on the console, /etc/tmpfiles.d/leds.conf needs a reboot.

 
A third way may be to create systemd rules. But it doesn't work for me.
root@bananapim2:~# udevadm info -a -p /sys/class/leds/bpi-m2\:red\:usr
[snip]
  looking at device '/devices/platform/leds/leds/bpi-m2:red:usr':
    KERNEL=="bpi-m2:red:usr"
    SUBSYSTEM=="leds"
    DRIVER==""
    ATTR{brightness}=="255"
    ATTR{max_brightness}=="255"
    ATTR{trigger}=="none rc-feedback kbd-scrollock kbd-numlock kbd-capslock kbd-kanalock kbd-shiftlock kbd-altgrlock kbd-ctrllock kbd-altlock kbd-shiftllock kbd-shiftrlock kbd-ctrlllock kbd-ctrlrlock usb-gadget usb-host mmc0 heartbeat cpu0 cpu1 cpu2 cpu3 [default-on] mmc1 rfkill0 "
[snip]
 
root@bananapim2:~#nano /etc/udev/rules.d/10-leds.rules
 
For setting the LEDs via dts files, have a look at this discussion:
 
Regards,
Steve

Share this post


Link to post
Share on other sites

I'd like to add another method which I found "cleaner" and "easier".

Debian provides an easy way to set sysfs values after reboot via the /etc/sysfs.conf file and the /etc/sysfs.d/ directory.

(This is basically the same as the relation between the /proc/sys and sysctl.conf and sysctl.d.)

 

My use-case:

I wanted to set the red light on my OpiPC to the heartbeat effect.

This can be set manually with:

# echo heartbeat > /sys/class/leds/orangepi\:red\:status/trigger

The sysfs files format is really simple:

# the path omits the /sys/
path/to/the/settings = value

So all I had to do was:

# echo "class/leds/orangepi\:red\:status/trigger = heartbeat" > /etc/sysfs.d/red_led.conf

One can modify the sysfs.conf itself, but it's more pluggable/manageable via .conf files under the sysfs.d directory.

Share this post


Link to post
Share on other sites

Any idea how to register a trigger provider?

 

I'd be interested registering some code to strobe the led in a kind of morse-code.  Specifically I wanted to strobe the 32bit IPv4 address for fun.  Basically 1-blink for a zero-bit, 2-blink for a one-bit, with 1-beat rest between each bit and a 5-beat rest between each message/word.

# 'b' = blink
# '_' = no-blink
# '-' = rest
# 1-bit = 'b-b'
# 0-bit = 'b-_'
# stop = '_-_-_-_-_'
#
# IPv4 Addr: 10.10.10.10
# Binary: 0000 1010 0000 1010 0000 1010 0000 1010
# Msg: b-_-b-_-b-_-b-_-b-b-b-_-b-b-b-_-b-_-b-_-b-_-b-_-b-b-b-_-b-b-b-_-b-_-b-_-b-_-b-_-b-b-b-_-b-b-b-_-b-_-b-_-b-_-b-_-b-b-b-_-b-b-b-_-_-_-_-_-_

I'd image the 'blink-clock' to be at ether 2hz or 4hz.  This could obviously be fitted for reading out any register or combination of GPOs.  Some other encoding scheme may work.  Morse code is a good base 36 code and slightly more compact, but harder to decode for bit bangers.

 

Q: Real question is how does one register their script/binary as a 'trigger-provider'

A: Like this apparently.  kmod-ledtrig-morse.

 

Update: Tacked onto this reply as not to bloat the tutorial with chatter.

Share this post


Link to post
Share on other sites

Nice...your boards talking morse! Is it avaiable for other devices as an Onion?

Yes...i've got one, but powering it on already a challenge...

Would be nice for my Orange Pi units.

Share this post


Link to post
Share on other sites

Ok...no morse, but I made a small script to test if my AppleTalk on my Orange Pi ( called MacIPpi ) is still alive.

It will blink for 3 second the red led if it is alive and will be default-on if something goes wrong.

 

#!/bin/bash

nbplkup | grep 'MacIPpi' &> /dev/null 2>&1
if [ $? == 0 ]; then
echo "heartbeat" > /sys/class/leds/orangepi\:red\:status/trigger
sleep 3
echo "none" > /sys/class/leds/orangepi\:red\:status/trigger
else
echo "default-on" > /sys/class/leds/orangepi\:red\:status/trigger
fi

 

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

*  *    * * *   root    /bin/appletalk-test >/dev/null 2>&1

 

Share this post


Link to post
Share on other sites

To illustrate my LED functionality I made a small YouTube movie to show how I use the red led to tell if my AppleTalk is working.

Have a look:

 

 

 

Part of my MacIPpi page:

 

http://www.macip.net/?page_id=113

 

This is the complete script:

 

#!/bin/bash

# flashes red led to display a working AppleTalk enviroment.
# when its sees MacIPpi in the nbplkup it is o.k.

echo "default-on" > /sys/class/leds/orangepi\:red\:status/trigger
sleep 0.6
echo "none" > /sys/class/leds/orangepi\:red\:status/trigger


nbplkup | grep 'MacIPpi' &> /dev/null
#echo $?
if [ $? == 0 ]; then
#   echo "matched"
# 6 seconds
echo "heartbeat" > /sys/class/leds/orangepi\:red\:status/trigger
sleep 4
echo "none" > /sys/class/leds/orangepi\:red\:status/trigger
sleep 10
# 19 seconds
echo "heartbeat" > /sys/class/leds/orangepi\:red\:status/trigger
sleep 4
echo "none" > /sys/class/leds/orangepi\:red\:status/trigger
sleep 10
# 33 seconds
echo "heartbeat" > /sys/class/leds/orangepi\:red\:status/trigger
sleep 4
echo "none" > /sys/class/leds/orangepi\:red\:status/trigger
sleep 11
# 47 seconds
echo "heartbeat" > /sys/class/leds/orangepi\:red\:status/trigger
sleep 4
echo "none" > /sys/class/leds/orangepi\:red\:status/trigger

else
echo "default-on" > /sys/class/leds/orangepi\:red\:status/trigger
#wall 'AppleTalk Down!'
fi

 

Share this post


Link to post
Share on other sites
4 4