Last active 1718980134

jiriks74's Avatar jiriks74 revised this gist 1718965854. Go to revision

4 files changed, 93 insertions

README.md(file created)

@@ -0,0 +1,38 @@
1 + # Element14 Pi-Desktop scripts for LibreELEC
2 +
3 + This gist contains the files needed to make Pi-Desktop's power butoon work in LibreELEC.
4 + Due to hardware limitations in the Pi-Desktop this button turns off the Raspberry Pi instead of opening the power menu.
5 +
6 + ## Setup
7 +
8 + 1. Install the Raspberry Pi Tools add-on
9 + - `System (settings)` -> `Add-ons` -> `Install from repozitory` -> `LibreELEC Add-ons` -> `Program add-ons` -> `Raspberry Pi Tools` -> `Install`
10 + 2. Get SSH access
11 + - `System (settings)` -> `LibreELEC` -> `Services` -> `Enable SSH`
12 + - If you don't setup a password the default is `libreelec`
13 + - Keep in mind that the default password should be changed or SSH turned off for better security.
14 + - `System (settings)` -> `LibreELEC` -> `Connections` -> `Address: aaa.bbb.c.ddd`
15 + - Get the IP address of the device by looking at the connection used
16 + 3. SSH to the device: `ssh root@aaa.bbb.c.ddd`
17 + 4. Run the install script: `curl -sSL https://opengist.stefka.eu/jiriks74/element14-libreelec/raw/HEAD/install.sh | bash`
18 + 5. The light should blink once to indicate that the power button script started successfully
19 + - It will blink once on every boot to indicate that the Pi comunicates with the power supply
20 +
21 + ## Manual setup
22 +
23 + If you're uncomfortable running a scripts from the internet you can fetch the files yourself and save them like this:
24 +
25 + ```bash
26 + /storage/scripts/pidesktop/powerkey.py
27 + /storage/.config/system.d/pidesktop-powerkey.service
28 + ```
29 +
30 + Then you need to run `systemctl enable pidesktop-powerkey` to start the scripts on boot and `systemctl start pidekstop-powerkey` to start it now.
31 +
32 + The light on the case should blink once to indicate that the script initialized successfully.
33 +
34 + ## Usage
35 +
36 + The power key can do 2 things on the Pi Desktop
37 + - When held for 2 seconds it sends a power off signal to the Pi (only once, more keypresses don't do anything)
38 + - When held for 5 seconds it cuts off power from the Pi (as if you unplugged if from wall power)

install.sh(file created)

@@ -0,0 +1,11 @@
1 + #!/bin/bash
2 + set -e
3 +
4 + mkdir -p /storage/scripts/pidesktop
5 +
6 + curl "https://opengist.stefka.eu/jiriks74/element14-libreelec/raw/HEAD/powerkey.py" -o /storage/scripts/pidesktop/powerkey.py
7 + curl "https://opengist.stefka.eu/jiriks74/element14-libreelec/raw/HEAD/pidesktop-powerkey.service" -o /storage/.config/system.d/pidesktop-powerkey.service
8 +
9 + systemctl daemon-reload
10 + systemctl enable pidesktop-powerkey
11 + systemctl start pidesktop-powerkey

pidesktop-powerkey.service(file created)

@@ -0,0 +1,11 @@
1 + [Unit]
2 + Description=pidesktop power button service
3 + After=local-fs.target
4 +
5 + [Service]
6 + Type=simple
7 + User=root
8 + ExecStart=/usr/bin/python3.11 /storage/scripts/pidesktop/powerkey.py
9 +
10 + [Install]
11 + WantedBy=multi-user.target

powerkey.py(file created)

@@ -0,0 +1,33 @@
1 + #!/usr/bin/env python
2 + #
3 + # pd-powerkey.py - monitor GPIO to detect power key press from Power MCU (PCU)
4 + #
5 +
6 + import time,os,sys
7 + os.chdir('/storage/scripts/pidesktop/')
8 + sys.path.append('/storage/.kodi/addons/virtual.rpi-tools/lib') # Import gpiozero from Raspberry Pi Tools add-on
9 + from gpiozero import Button,LED
10 +
11 + print("pidesktop: power button service initializing")
12 +
13 + pwrBtn = Button(13) # PCU to Pi - detect power key pressed
14 +
15 + pcu = LED(6) # Pi to PCU - start/stop shutdown timer
16 + pcu.off() # tell PCU we are alive
17 + pcu.on() # cause blink by starting shutdown timer
18 + time.sleep(0.5)
19 + pcu.off() # clear timer to stop blinking and prefent forced power off
20 +
21 + # callback function
22 + def 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 +
29 + pwrBtn.when_released = powerkey_pressed # Setup callback for the power off signal
30 + print("pidesktop: power button monitor enabled")
31 +
32 + while True: # Keep the service running
33 + time.sleep(10)
Newer Older