diff --git a/README.md b/README.md index ef5863a..72dfb78 100644 --- a/README.md +++ b/README.md @@ -8,15 +8,15 @@ * [Example for RecalBox](#example-for-recalbox) * [Example for batocera](#example-for-batocera) --------------------- +-------------------- # GPiCase2 (GPi Case 2 only) The RetroFlag GPiCase 2 CM4 safe shutdown script will automatically switch between the LCD display and HDMI output when using the dock. ### Click the link Jump to install GPiCase2 script:[GPiCase2 Script](https://github.com/RetroFlag/GPiCase2-Script). - --------------------- + +-------------------- # GPi Case (GPi Case only) @@ -41,18 +41,18 @@ wget -O - "https://raw.githubusercontent.com/RetroFlag/retroflag-picase/master/i wget -O - "https://raw.githubusercontent.com/RetroFlag/retroflag-picase/master/recalbox_install_gpi.sh" | bash - --------------------- + +-------------------- -# Pi Case -## (nespi+, superpi, megapi,nespi4 case) +# Pi Case +## (nespi+, superpi, megapi, nespi4 case) RetroFlag Pi-Case Safe Shutdown ### Turn switch "SAFE SHUTDOWN" to ON. -### Example for RetroPie: +### RetroPie: 1. Make sure internet connected. 2. Make sure keyboard connected. 3. Press F4 enter terminal. @@ -61,8 +61,7 @@ RetroFlag Pi-Case Safe Shutdown wget -O - "https://raw.githubusercontent.com/RetroFlag/retroflag-picase/master/install.sh" | sudo bash - -### Example for RecalBox: +### RecalBox: 1. Make sure internet connected. 2. Make sure keyboard connected. 3. Press F4 first. And then press ALT-F2 enter terminal. @@ -72,8 +71,7 @@ wget -O - "https://raw.githubusercontent.com/RetroFlag/retroflag-picase/master/i wget -O - "https://raw.githubusercontent.com/RetroFlag/retroflag-picase/master/recalbox_install.sh" | bash - -### Example for batocera: +### Batocera: 1. Make sure internet connected. 2. Make sure keyboard connected. 3. Enter terminal. How to enter terminal: https://wiki.batocera.org/access_the_batocera_via_ssh @@ -82,6 +80,19 @@ wget -O - "https://raw.githubusercontent.com/RetroFlag/retroflag-picase/master/r wget -O - "https://raw.githubusercontent.com/RetroFlag/retroflag-picase/master/batocera_install.sh" | bash +### Ubuntu +1. Make sure internet connected. +2. Make sure keyboard connected. +3. Enter terminal. +4. In the terminal, type the one-line command below(Case sensitive): + +Ubuntu 20.10 to 21.10: +wget -O - "https://raw.githubusercontent.com/rfocosi/retroflag-picase/master/ubuntu_install.sh" | sudo bash + +Ubuntu 22.04+: +wget -O - "https://raw.githubusercontent.com/rfocosi/retroflag-picase/master/ubuntu22_install.sh" | sudo bash + +-------------------- ### Example for lakkatv: diff --git a/RetroFlag_Ubuntu22.README b/RetroFlag_Ubuntu22.README new file mode 100644 index 0000000..f4e3f46 --- /dev/null +++ b/RetroFlag_Ubuntu22.README @@ -0,0 +1,14 @@ +Firmware overlay for Ubuntu 22+ +=============================== + +This overlay will turn on GPIO 4 at startup. + +To compile, execute the command: +dtc -I dts -O dtb -o RetroFlag_Ubuntu22.dtbo RetroFlag_Ubuntu22.dts + +After: +- Copy RetroFlag_Ubuntu22.dtbo to /boot/firmware/overlays +- Add this line to the end of /boot/firmware/config.txt: +dtoverlay=RetroFlag_Ubuntu22.dtbo + + diff --git a/RetroFlag_Ubuntu22.dtbo b/RetroFlag_Ubuntu22.dtbo new file mode 100644 index 0000000..8ea6287 Binary files /dev/null and b/RetroFlag_Ubuntu22.dtbo differ diff --git a/RetroFlag_Ubuntu22.dts b/RetroFlag_Ubuntu22.dts new file mode 100644 index 0000000..2b1572a --- /dev/null +++ b/RetroFlag_Ubuntu22.dts @@ -0,0 +1,17 @@ +/dts-v1/; + +/ { + videocore { + pins_4b { // Pi4 Model B + pin_config { + pin@default { + polarity = "active_high"; + termination = "pull_down"; + startup_state = "inactive"; + function = "input"; + }; // pin + pin@p4 { function = "output"; termination = "pull_down"; polarity = "active_low"; startup_state = "active"; }; // ON + }; // pin_config + }; + }; +}; diff --git a/ubuntu22_SafeShutdown.py b/ubuntu22_SafeShutdown.py new file mode 100644 index 0000000..ca49937 --- /dev/null +++ b/ubuntu22_SafeShutdown.py @@ -0,0 +1,29 @@ +from gpiozero import Button, LED +import os +from signal import pause +import subprocess + +#initialize pins +powerBtnPin = 3 +resetBtnPin = 2 +ledPin = 14 + +led = LED(ledPin) +led.on() + +#functions that handle button events +def poweroff(): + led.blink(.2,.2) + os.system("shutdown -h now") + +def reboot(): + led.blink(.2,.2) + os.system("reboot") + +powerBtn = Button(powerBtnPin) +rebootBtn = Button(resetBtnPin) + +powerBtn.when_pressed = poweroff +rebootBtn.when_pressed = reboot + +pause() diff --git a/ubuntu22_install.sh b/ubuntu22_install.sh new file mode 100644 index 0000000..b5d8df5 --- /dev/null +++ b/ubuntu22_install.sh @@ -0,0 +1,86 @@ +#!/bin/bash + +#DEV_ROOT= + +CURL_EXEC=$( which curl ) +PYTHON_EXEC=$( which python | which python3 ) +WGET_EXEC=$( which wget ) + +[ -z $SourcePath ] && SourcePath=https://raw.githubusercontent.com/rfocosi/retroflag-picase/master + +#Check if root-------------------------------------- +if [[ $EUID -ne 0 ]]; then + echo + echo "Please execute script as root." + exit 1 +fi +#--------------------------------------------------- + +if [[ -z "$WGET_EXEC" ]]; then + echo + echo "Requires 'wget'. Please install and try again." + exit 1 +fi + +if [[ -z "$CURL_EXEC" ]]; then + echo + echo "Requires 'curl'. Please install and try again." + exit 1 +fi + +if [[ -z "$PYTHON_EXEC" || ! "$( $PYTHON_EXEC -V )" = "Python 3"* ]]; then + echo + echo "Requires 'python3'. Please install and try again." + exit 1 +fi + +if [[ -z "$( python3 -c "import sys; import gpiozero; print('Found') if 'gpiozero' in sys.modules else print('')" )" ]]; then + echo + echo "Library 'python3-gpiozero' not found. Please install and try again." + exit 1 +fi + +#RetroFlag pw io ;2:in ;3:in ;4:in ;14:out 1---------------------------------------- +[[ ! -z "$DEV_ROOT" ]] && mkdir -p $DEV_ROOT/boot/firmware/overlays +File=$DEV_ROOT/boot/firmware/config.txt +$WGET_EXEC -q -O "$DEV_ROOT/boot/firmware/overlays/RetroFlag_Ubuntu22.dtbo" "$SourcePath/RetroFlag_Ubuntu22.dtbo" +if grep -q "RetroFlag_" "$File"; + then + sed -i '/RetroFlag_/c dtoverlay=RetroFlag_Ubuntu22.dtbo' $File + echo "PW IO fix." + else + echo "dtoverlay=RetroFlag_Ubuntu22.dtbo" >> $File + echo "dtoverlay=gpio-poweroff,gpiopin=4,active_low=1,input=1" >> $File + echo "PW IO enabled." +fi +if grep -q "enable_uart" "$File"; + then + sed -i '/enable_uart/c enable_uart=1' $File + echo "UART fix." + else + echo "enable_uart=1" >> $File + echo "UART enabled." +fi + +#----------------------------------------------------------- + +#Download Python script----------------------------- +mkdir -p "$DEV_ROOT/opt/RetroFlag" +script=$DEV_ROOT/opt/RetroFlag/SafeShutdown.py +$WGET_EXEC -q -O $script "$SourcePath/ubuntu22_SafeShutdown.py" + +#Enable Python script to run on start up------------ +[[ ! -z "$DEV_ROOT" ]] && mkdir -p $DEV_ROOT/lib/systemd/system/ +service=$DEV_ROOT/lib/systemd/system/safe-shutdown.service +curl -s "$SourcePath/ubuntu_safe-shutdown.service" | awk '{gsub(/PYTHON_EXEC/,"'$PYTHON_EXEC'")}1' > $service + +systemctl enable safe-shutdown +echo "Added service '$service' to system startup." + +#----------------------------------------------------------- + +#Reboot to apply changes---------------------------- +echo "RetroFlag Pi Case installation done. Will now reboot after 3 seconds." +sleep 3 +sudo reboot +#----------------------------------------------------------- diff --git a/ubuntu_SafeShutdown.py b/ubuntu_SafeShutdown.py new file mode 100644 index 0000000..905f45b --- /dev/null +++ b/ubuntu_SafeShutdown.py @@ -0,0 +1,83 @@ +import RPi.GPIO as GPIO +import os +import time +from multiprocessing import Process + +#initialize pins +powerPin = 3 #pin 5 +ledPin = 14 #TXD +resetPin = 2 #pin 13 +powerenPin = 4 #pin 5 + +#initialize GPIO settings +def init(): + GPIO.setwarnings(False) + GPIO.setmode(GPIO.BCM) + GPIO.setup(powerPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) + GPIO.setup(resetPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) + GPIO.setup(ledPin, GPIO.OUT) + GPIO.output(ledPin, GPIO.HIGH) + GPIO.setup(powerenPin, GPIO.OUT) + GPIO.output(powerenPin, GPIO.HIGH) + +#waits for user to hold button up to 1 second before issuing poweroff command +def poweroff(): + while True: + #self.assertEqual(GPIO.input(powerPin), GPIO.LOW) + GPIO.wait_for_edge(powerPin, GPIO.FALLING) + os.system("sleep 1s") + os.system("poweroff") + +#blinks the LED to signal button being pushed +def powerLedBlink(): + while True: + GPIO.output(ledPin, GPIO.HIGH) + #self.assertEqual(GPIO.input(powerPin), GPIO.LOW) + GPIO.wait_for_edge(powerPin, GPIO.FALLING) + start = time.time() + while GPIO.input(powerPin) == GPIO.LOW: + GPIO.output(ledPin, GPIO.LOW) + time.sleep(0.2) + GPIO.output(ledPin, GPIO.HIGH) + time.sleep(0.2) + +def resetLedBlink(): + while True: + GPIO.output(ledPin, GPIO.HIGH) + #self.assertEqual(GPIO.input(powerPin), GPIO.LOW) + GPIO.wait_for_edge(resetPin, GPIO.FALLING) + start = time.time() + while GPIO.input(resetPin) == GPIO.LOW: + GPIO.output(ledPin, GPIO.LOW) + time.sleep(0.2) + GPIO.output(ledPin, GPIO.HIGH) + time.sleep(0.2) + +#resets the pi +def reset(): + while True: + #self.assertEqual(GPIO.input(resetPin), GPIO.LOW) + GPIO.wait_for_edge(resetPin, GPIO.FALLING) + os.system("sleep 1s") + os.system("reboot") + + +if __name__ == "__main__": + #initialize GPIO settings + init() + #create a multiprocessing.Process instance for each function to enable parallelism + powerProcess = Process(target = poweroff) + powerProcess.start() + resetProcess = Process(target = reset) + resetProcess.start() + powerLedProcess = Process(target = powerLedBlink) + powerLedProcess.start() + resetLedProcess = Process(target = resetLedBlink) + resetLedProcess.start() + + powerProcess.join() + resetProcess.join() + powerLedProcess.join() + resetLedProcess.join() + + GPIO.cleanup() diff --git a/ubuntu_install.sh b/ubuntu_install.sh new file mode 100644 index 0000000..86906d9 --- /dev/null +++ b/ubuntu_install.sh @@ -0,0 +1,86 @@ +#!/bin/bash + +#DEV_ROOT= + +CURL_EXEC=$( which curl ) +PYTHON_EXEC=$( which python | which python3 ) +WGET_EXEC=$( which wget ) + +[ -z $SourcePath ] && SourcePath=https://raw.githubusercontent.com/rfocosi/retroflag-picase/master + +#Check if root-------------------------------------- +if [[ $EUID -ne 0 ]]; then + echo + echo "Please execute script as root." + exit 1 +fi +#--------------------------------------------------- + +if [[ -z "$WGET_EXEC" ]]; then + echo + echo "Requires 'wget'. Please install and try again." + exit 1 +fi + +if [[ -z "$CURL_EXEC" ]]; then + echo + echo "Requires 'curl'. Please install and try again." + exit 1 +fi + +if [[ -z "$PYTHON_EXEC" || ! "$( $PYTHON_EXEC -V )" = "Python 3"* ]]; then + echo + echo "Requires 'python3'. Please install and try again." + exit 1 +fi + +if [[ -z "$( python3 -c "import RPi.GPIO as GPIO; print(GPIO.VERSION)" )" ]]; then + echo + echo "Library 'python3-rpi.gpio' not found. Please install and try again." + exit 1 +fi + +#RetroFlag pw io ;2:in ;3:in ;4:in ;14:out 1---------------------------------------- +[[ ! -z "$DEV_ROOT" ]] && mkdir -p $DEV_ROOT/boot/firmware/overlays +File=$DEV_ROOT/boot/firmware/config.txt +$WGET_EXEC -q -O "$DEV_ROOT/boot/firmware/overlays/RetroFlag_pw_io.dtbo" "$SourcePath/RetroFlag_pw_io.dtbo" +if grep -q "RetroFlag_pw_io" "$File"; + then + sed -i '/RetroFlag_pw_io/c dtoverlay=RetroFlag_pw_io.dtbo' $File + echo "PW IO fix." + else + echo "dtoverlay=RetroFlag_pw_io.dtbo" >> $File + echo "dtoverlay=gpio-poweroff,gpiopin=4,active_low=1,input=1" >> $File + echo "PW IO enabled." +fi +if grep -q "enable_uart" "$File"; + then + sed -i '/enable_uart/c enable_uart=1' $File + echo "UART fix." + else + echo "enable_uart=1" >> $File + echo "UART enabled." +fi + +#----------------------------------------------------------- + +#Download Python script----------------------------- +mkdir -p "$DEV_ROOT/opt/RetroFlag" +script=$DEV_ROOT/opt/RetroFlag/SafeShutdown.py +$WGET_EXEC -q -O $script "$SourcePath/ubuntu_SafeShutdown.py" + +#Enable Python script to run on start up------------ +[[ ! -z "$DEV_ROOT" ]] && mkdir -p $DEV_ROOT/lib/systemd/system/ +service=$DEV_ROOT/lib/systemd/system/safe-shutdown.service +curl -s "$SourcePath/ubuntu_safe-shutdown.service" | awk '{gsub(/PYTHON_EXEC/,"'$PYTHON_EXEC'")}1' > $service + +systemctl enable safe-shutdown +echo "Added service '$service' to system startup." + +#----------------------------------------------------------- + +#Reboot to apply changes---------------------------- +echo "RetroFlag Pi Case installation done. Will now reboot after 3 seconds." +sleep 3 +sudo reboot +#----------------------------------------------------------- diff --git a/ubuntu_safe-shutdown.service b/ubuntu_safe-shutdown.service new file mode 100644 index 0000000..010296a --- /dev/null +++ b/ubuntu_safe-shutdown.service @@ -0,0 +1,11 @@ +[Unit] +Description=Nespi4 Case Safe Shutdown + +[Service] +Type=idle +ExecStart=PYTHON_EXEC /opt/RetroFlag/SafeShutdown.py +StandardOutput=null + +[Install] +WantedBy=multi-user.target +Alias=safe-shutdown.service