Last active 1718980134

README.md Raw

Element14 Pi-Desktop scripts for LibreELEC

This gist contains the files needed to make Pi-Desktop's power butoon work in LibreELEC. Due to hardware limitations in the Pi-Desktop this button turns off the Raspberry Pi instead of opening the power menu.

Setup

  1. Install the Raspberry Pi Tools add-on
  • System (settings) -> Add-ons -> Install from repozitory -> LibreELEC Add-ons -> Program add-ons -> Raspberry Pi Tools -> Install
  1. Get SSH access
  • System (settings) -> LibreELEC -> Services -> Enable SSH
    • If you don't setup a password the default is libreelec
    • Keep in mind that the default password should be changed or SSH turned off for better security.
  • System (settings) -> LibreELEC -> Connections -> Address: aaa.bbb.c.ddd
    • Get the IP address of the device by looking at the connection used
  1. SSH to the device: ssh root@aaa.bbb.c.ddd
  2. Run the install script: curl -sSL https://opengist.stefka.eu/jiriks74/element14-libreelec/raw/HEAD/install.sh | bash
  3. The light should blink once to indicate that the power button script started successfully
  • It will blink once/twice on every boot to indicate that the Pi comunicates with the power supply

If the service is already running (you ran the script more than once) the ligt will not blink. Reboot the device and watch for a blink during the boot process or restart the service using systemctl restart pidesktop-powerkey.

Manual setup

If you're uncomfortable running scripts from the internet you can fetch the files yourself and save them like this:

/storage/scripts/pidesktop/powerkey.py
/storage/.config/system.d/pidesktop-powerkey.service

Then you need to run

  1. systemctl daemon-reload to load the service file
  2. systemctl enable pidesktop-powerkey to start the service on boot
  3. systemctl start pidekstop-powerkey to start the service now

The light on the case should blink once to indicate that the script initialized successfully.

If the service is already running (you started a running pidesktop-powerkey service) the ligt will not blink. Reboot the device and watch for a blink during the boot process or restart the service using systemctl restart pidesktop-powerkey.

Usage of the Pi-Desktop button

The power key can do 2 things on the Pi Desktop

  • When held for 2 seconds it sends a power off signal to the Pi (only once, more keypresses don't do anything)
  • When held for 5 seconds it cuts off power from the Pi (as if you unplugged if from wall power)
install.sh Raw
1#!/bin/bash
2set -e
3
4echo "Creating directories..."
5mkdir -p /storage/scripts/pidesktop
6
7echo "Downloading files..."
8curl -s "https://opengist.stefka.eu/jiriks74/element14-libreelec/raw/HEAD/powerkey.py" -o /storage/scripts/pidesktop/powerkey.py
9curl -s "https://opengist.stefka.eu/jiriks74/element14-libreelec/raw/HEAD/pidesktop-powerkey.service" -o /storage/.config/system.d/pidesktop-powerkey.service
10echo "Files downloaded successfully."
11
12echo "Loading the systemd service..."
13systemctl daemon-reload
14systemctl enable pidesktop-powerkey
15echo "Service loaded. Starting the service..."
16echo "Watch the case now. The light should blink once/twice to indicate that the the script is working."
17sleep 5
18systemctl start pidesktop-powerkey
19
20echo ""
21echo "Setup finished."
22echo "The power button should work now."
23echo ""
24echo "Disclaimer: Reboot might not be possible anymore. The Pi-Desktop cuts power after 30 seconds of no response from the Pi."
25echo " If the Pi takes more than 30 seconds to reboot the Pi-Desktop will cut it's power and you'll have to turn it on again."
26
pidesktop-powerkey.service Raw
1[Unit]
2Description=pidesktop power button service
3After=local-fs.target
4
5[Service]
6Type=simple
7User=root
8ExecStart=/usr/bin/python /storage/scripts/pidesktop/powerkey.py
9
10[Install]
11WantedBy=multi-user.target
12
powerkey.py Raw
1#!/usr/bin/env python
2#
3# pd-powerkey.py - monitor GPIO to detect power key press from Power MCU (PCU)
4#
5
6import time,os,sys
7os.chdir('/storage/scripts/pidesktop/')
8sys.path.append('/storage/.kodi/addons/virtual.rpi-tools/lib') # Import gpiozero from Raspberry Pi Tools add-on
9from gpiozero import Button,LED
10
11print("pidesktop: power button service initializing")
12
13pwrBtn = Button(13) # PCU to Pi - detect power key pressed
14
15pcu = LED(6) # Pi to PCU - start/stop shutdown timer
16pcu.off() # tell PCU we are alive
17pcu.on() # cause blink by starting shutdown timer
18time.sleep(0.5)
19pcu.off() # clear timer to stop blinking and prefent forced power off
20
21# callback function
22def powerkey_pressed(channels):
23 print("pidesktop: power button press detected, initiating shutdown")
24 os.system("sync")
25 # os.system("shutdown -h now") # Shutdown the OS directly (forced shutdown)
26 os.system("kodi-send --action=ShutDown") # Tell Kodi to execute it's shutdown procedure (graceful)
27 sys.exit()
28
29pwrBtn.when_released = powerkey_pressed # Setup callback for the power off signal
30print("pidesktop: power button monitor enabled")
31
32while True: # Keep the service running
33 time.sleep(10)
34