Skip to content

Instantly share code, notes, and snippets.

@pfeerick
Created June 22, 2018 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pfeerick/f8a8c5dd71e5a6c700b174c1ef448b0c to your computer and use it in GitHub Desktop.
Save pfeerick/f8a8c5dd71e5a6c700b174c1ef448b0c to your computer and use it in GitHub Desktop.
rock64 power led heartbeat indicator
#!/bin/bash
#if a gpio group exists, doing this at startup allows any group member to control leds
#sudo chgrp gpio /sys/class/leds/{diy,work}-led/brightness
#sudo chmod g+w /sys/class/leds/{diy,work}-led/brightness
LED_GPIO="/sys/class/leds/power-led"
LED_ON=255
LED_OFF=0
RequireRoot() {
if [ "$(id -u)" != "0" ]; then
echo "This function requires root privleges - run as root or through sudo. Exiting" >&2
exit 1
fi
} # RequireRoot
ParseOptions() {
while getopts 'hHsSiI' c ; do
case ${c} in
s|S)
RequireRoot
SetupGPIOs
exit 0
;;
i|I)
RequireRoot
InstallToRcLocal
exit 0
;;
h|H)
DisplayUsage
exit 0
;;
esac
done
} # ParseOptions
Cleanup()
{
exit 0
} # Cleanup
SetupGPIOs()
{
[ $SUDO_USER ] && user=$SUDO_USER || user=`whoami`
echo "Ensuring 'gpio' group exists..."
getent group gpio >/dev/null 2>&1 || groupadd gpio
echo "Ensuring '$user' is in gpio group..."
usermod -aG gpio $user
echo "Setting necessary group permissions..."
chgrp gpio /sys/class/leds/{standby,power}-led/brightness
chmod g+w /sys/class/leds/{standby,power}-led/brightness
} # SetupGPIOs
InstallToRcLocal() {
local SCRIPT=`realpath $0`
cat >/etc/rc.local << EOL
#!/bin/bash
$SCRIPT -s
$SCRIPT &
exit 0
EOL
chmod +x /etc/rc.local
echo "Reboot to see the changes!"
} # InstallToRcLocal
DisplayUsage()
{
echo "TBC"
}
Main()
{
ParseOptions "$@"
#exit nicely if Ctrl-C pressed, SIGTERM, etc sent
trap Cleanup SIGHUP SIGINT SIGTERM
#make sure led is off, and delay for asthetics
echo ${LED_OFF} > "${LED_GPIO}/brightness" #off
sleep 0.5s
#main loop
while true
do
echo ${LED_ON} > "${LED_GPIO}/brightness" #on
sleep 0.1s
echo ${LED_OFF} > "${LED_GPIO}/brightness" #off
sleep 0.1s
echo ${LED_ON} > "${LED_GPIO}/brightness" #on
sleep 0.1s
echo ${LED_OFF} > "${LED_GPIO}/brightness" #off
sleep 9.7s
done
exit 0
} # Main
Main $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment