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

adding scripts for Ubuntu on Raspberry Pi #108

Open
wants to merge 1 commit 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
71 changes: 71 additions & 0 deletions Ubuntu_SafeShutdown.py
Original file line number Diff line number Diff line change
@@ -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()

64 changes: 64 additions & 0 deletions Ubuntu_install.sh
Original file line number Diff line number Diff line change
@@ -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
#-----------------------------------------------------------