From 0d10fab4730f7dfc25ffe7cbb4f380a1af55761e Mon Sep 17 00:00:00 2001 From: Devin Flake Date: Mon, 11 Jan 2021 22:50:40 -0700 Subject: [PATCH] adding scripts for Ubuntu on Raspberry Pi --- Ubuntu_SafeShutdown.py | 71 ++++++++++++++++++++++++++++++++++++++++++ Ubuntu_install.sh | 64 +++++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 Ubuntu_SafeShutdown.py create mode 100644 Ubuntu_install.sh diff --git a/Ubuntu_SafeShutdown.py b/Ubuntu_SafeShutdown.py new file mode 100644 index 0000000..b6c8bc9 --- /dev/null +++ b/Ubuntu_SafeShutdown.py @@ -0,0 +1,71 @@ +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) + print("Powering off in 5 seconds") + os.system("sleep 5s") + os.system("poweroff") + +#blinks the LED to signal button being pushed +def ledBlink(): + 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) + +#resets the pi +def reset(): + while True: + #self.assertEqual(GPIO.input(resetPin), GPIO.LOW) + GPIO.wait_for_edge(resetPin, GPIO.FALLING) + print("Reseting in 5 seconds") + os.system("sleep 5s") + os.system("shutdown -r now") + + +if __name__ == "__main__": + #initialize GPIO settings + init() + #create a multiprocessing.Process instance for each function to enable parallelism + powerProcess = Process(target = poweroff) + powerProcess.start() + ledProcess = Process(target = ledBlink) + ledProcess.start() + resetProcess = Process(target = reset) + resetProcess.start() + + powerProcess.join() + ledProcess.join() + resetProcess.join() + + GPIO.cleanup() + diff --git a/Ubuntu_install.sh b/Ubuntu_install.sh new file mode 100644 index 0000000..5c9a84a --- /dev/null +++ b/Ubuntu_install.sh @@ -0,0 +1,64 @@ +#!/bin/bash + +SourcePath=https://raw.githubusercontent.com/RetroFlag/retroflag-picase/master + +#Check if root-------------------------------------- +if [[ $EUID -ne 0 ]]; then + echo "Please execute script as root." + exit 1 +fi +#----------------------------------------------------------- + +echo "Installing Python GPIO library" +sudo apt update +sudo apt install python3-rpi.gpio + +#RetroFlag pw io ;2:in ;3:in ;4:in ;14:out 1---------------------------------------- +File=/boot/firmware/config.txt +wget -O "/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 "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----------------------------- +sudo mkdir "/opt/RetroFlag" +script=/opt/RetroFlag/Ubuntu_SafeShutdown.py +wget -O $script "$SourcePath/Ubuntu_SafeShutdown.py" + +#Enable Python script to run on start up------------ +RC=/etc/rc.local + +if grep -q "/usr/bin/python3 $script &" "$RC"; + then + echo "File $RC already configured. Doing nothing." + else + echo "#!/bin/bash +/usr/bin/python3 /opt/RetroFlag/Ubuntu_SafeShutdown.py & +exit 0" >> "$RC" + sudo chown root /etc/rc.local + sudo chmod 755 /etc/rc.local + echo "File /etc/rc.local configured." +fi +#----------------------------------------------------------- + +#Reboot to apply changes---------------------------- +echo "RetroFlag Pi Case installation done. Will now reboot after 5 seconds." +sleep 5 +sudo reboot +#----------------------------------------------------------- +