Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nespi4 Ubuntu install' scripts #131

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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:
Expand Down
14 changes: 14 additions & 0 deletions RetroFlag_Ubuntu22.README
Original file line number Diff line number Diff line change
@@ -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


Binary file added RetroFlag_Ubuntu22.dtbo
Binary file not shown.
17 changes: 17 additions & 0 deletions RetroFlag_Ubuntu22.dts
Original file line number Diff line number Diff line change
@@ -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
};
};
};
29 changes: 29 additions & 0 deletions ubuntu22_SafeShutdown.py
Original file line number Diff line number Diff line change
@@ -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()
86 changes: 86 additions & 0 deletions ubuntu22_install.sh
Original file line number Diff line number Diff line change
@@ -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
#-----------------------------------------------------------
83 changes: 83 additions & 0 deletions ubuntu_SafeShutdown.py
Original file line number Diff line number Diff line change
@@ -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()
Loading