I have been able to install the Pimoroni OnOff SHIM on my RPi running LibreELEC. The OnOff SHIM enables you to completely power off - and power on again - a Raspberry Pi with a single pushbutton. As mentioned elsewhere in this forum, it is not possible to install it using Pimoroni's install script, as this requires use of apt-get to install packages, which is not available under LibreELEC; also, it would be necessary to change paths throughout the script.
I came across this thread on the Retropie forum, where the author had stripped this down to two simple shell scripts. Inspired by this, I have created the following scripts for LibreELEC.
The first is a python script, which checks for the button being pressed, and if held for > 1 second (as with the Pimoroni script), invokes the halt (or poweroff) os command. This requires the Raspberry Pi Tools add-on, and should be called from autostart.sh. (I saved it in /storage/.kodi/userdata.)
#!/usr/bin/env python
from time import sleep
import sys, os
sys.path.append("/storage/.kodi/addons/virtual.rpi-tools/lib")
import RPi.GPIO as GPIO
pin = 17
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def button_action(pin):
button_press_timer = 0
while True:
if (GPIO.input(pin) == False) : # while button is still pressed down
button_press_timer += 1 # keep counting until button is released
else: # button is released, figure out for how long
if (button_press_timer > 1) : # pressed for > 1 second
os.system("halt")
button_press_timer = 0
sleep(1)
GPIO.add_event_detect(pin, GPIO.FALLING, callback=button_action, bouncetime=20)
try:
while True:
sleep(0.5)
except KeyboardInterrupt:
GPIO.cleanup() # clean up GPIO on CTRL+C exit
Display More
The second script, which is called as part of the shutdown process, will flash the SHIM's onboard LED 3 times, and then drive GPIO 4 low, cutting the power to the RPi (as with the Pimoroni script):
#!/bin/sh
#
if [ "$1" != "reboot" ]; then
poweroff_pin="4"
led_pin="17"
trigger_pin="17"
echo $trigger_pin > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio$led_pin/direction
for iteration in 1 2 3; do
echo 0 > /sys/class/gpio/gpio$led_pin/value
sleep 0.2
echo 1 > /sys/class/gpio/gpio$led_pin/value
sleep 0.2
done
echo $poweroff_pin > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio$poweroff_pin/direction
echo 0 > /sys/class/gpio/gpio$poweroff_pin/value
fi
Display More
This needs to be saved as /storage/.config/shutdown.sh
Now, pressing and holding the button - or calling 'Power off system' from the Power options menu, will completely power off the RPi.
A momentary press of the button will power it up again.