Skip to content

Instantly share code, notes, and snippets.

@mashurex
Last active February 20, 2019 17:47

Revisions

  1. mashurex revised this gist Feb 20, 2019. 1 changed file with 89 additions and 43 deletions.
    132 changes: 89 additions & 43 deletions gbz-install.sh
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    #!/bin/bash
    set -e
    set -u

    echo "Updating packages..."
    sudo apt-get update

    echo "Installing required packages..."
    sudo apt-get -y install git build-essential python-dev cmake

    if [ ! -f /usr/local/bin/retrogame ]; then
    @@ -34,34 +34,66 @@ EOF

    sudo chown root:root retrogame.cfg
    sudo chmod 555 retrogame.cfg
    sudo mv retrogame.cfg /boot/retrogame.cfg
    # FIXME: This isn't working right, controller still needs configuring on 1st boot
    # cat > /home/pi/.emulationstation/es_input.cfg <<EOF
    #<?xml version="1.0"?>
    #<inputList>
    # <inputAction type="onfinish">
    # <command>/opt/retropie/supplementary/emulationstation/scripts/inputconfiguration.sh</command>
    # </inputAction>
    # <inputConfig type="keyboard" deviceName="Keyboard" deviceGUID="-1">
    # <input name="a" type="key" id="97" value="1" />
    # <input name="b" type="key" id="98" value="1" />
    # <input name="down" type="key" id="1073741905" value="1" />
    # <input name="hotkeyenable" type="key" id="32" value="1" />
    # <input name="left" type="key" id="1073741904" value="1" />
    # <input name="leftshoulder" type="key" id="108" value="1" />
    # <input name="right" type="key" id="1073741903" value="1" />
    # <input name="rightshoulder" type="key" id="114" value="1" />
    # <input name="select" type="key" id="32" value="1" />
    # <input name="start" type="key" id="13" value="1" />
    # <input name="up" type="key" id="1073741906" value="1" />
    # <input name="x" type="key" id="120" value="1" />
    # <input name="y" type="key" id="121" value="1" />
    # </inputConfig>
    #</inputList>
    #EOF
    sudo cp retrogame.cfg /boot/retrogame.cfg
    fi

    if [ ! -f /etc/udev/rules.d/10-retrogame.rules ]; then
    echo "Installing Retrogame SDL Config..."
    sudo cat > /etc/udev/rules.d/10-retrogame.rules <<EOF
    SUBSYSTEM=="input", ATTRS{name}=="retrogame", ENV{ID_INPUT_KEYBOARD}="1"
    EOF

    fi

    if [ ! -f /home/pi/.emulationstation/es_temporaryinput.cfg ]; then
    cat > /home/pi/.emulationstation/es_input.cfg <<EOF
    <?xml version="1.0"?>
    <inputList>
    <inputAction type="onfinish">
    <command>/opt/retropie/supplementary/emulationstation/scripts/inputconfiguration.sh</command>
    </inputAction>
    <inputConfig type="keyboard" deviceName="Keyboard" deviceGUID="-1">
    <input name="pageup" type="key" id="108" value="1"/>
    <input name="start" type="key" id="13" value="1"/>
    <input name="down" type="key" id="1073741905" value="1"/>
    <input name="pagedown" type="key" id="114" value="1"/>
    <input name="right" type="key" id="1073741903" value="1"/>
    <input name="select" type="key" id="32" value="1"/>
    <input name="left" type="key" id="1073741904" value="1"/>
    <input name="up" type="key" id="1073741906" value="1"/>
    <input name="a" type="key" id="97" value="1"/>
    <input name="b" type="key" id="98" value="1"/>
    <input name="x" type="key" id="120" value="1"/>
    <input name="y" type="key" id="121" value="1"/>
    </inputConfig>
    </inputList>
    EOF

    cat > /home/pi/.emulationstation/es_temporaryinput.cfg <<EOF
    <?xml version="1.0"?>
    <inputList>
    <inputConfig type="keyboard" deviceName="Keyboard" deviceGUID="-1">
    <input name="a" type="key" id="97" value="1" />
    <input name="b" type="key" id="98" value="1" />
    <input name="down" type="key" id="1073741905" value="1" />
    <input name="hotkeyenable" type="key" id="32" value="1" />
    <input name="left" type="key" id="1073741904" value="1" />
    <input name="leftshoulder" type="key" id="108" value="1" />
    <input name="right" type="key" id="1073741903" value="1" />
    <input name="rightshoulder" type="key" id="114" value="1" />
    <input name="select" type="key" id="32" value="1" />
    <input name="start" type="key" id="13" value="1" />
    <input name="up" type="key" id="1073741906" value="1" />
    <input name="x" type="key" id="120" value="1" />
    <input name="y" type="key" id="121" value="1" />
    </inputConfig>
    </inputList>
    EOF

    sudo chown pi:pi /home/pi/.emulationstation/es_*.cfg
    sudo chmod 777 /home/pi/.emulationstation/es_*.cfg
    fi

    if [ ! -f /home/pi/.ads1x15 ]; then
    cd /home/pi
    if [ ! -d Adafruit_Python_ADS1x15 ]; then
    @@ -71,12 +103,22 @@ if [ ! -f /home/pi/.ads1x15 ]; then

    cd Adafruit_Python_ADS1x15
    if [ ! -f setuptools-3.5.1.zip ]; then
    echo "Downloading setuptools-3.5.1.zip..."
    wget https://pypi.python.org/packages/source/s/setuptools/setuptools-3.5.1.zip
    if [ -f /boot/setuptools-3.5.1.zip ]; then
    sudo cp /boot/setuptools-*.zip .
    else
    echo "Downloading setuptools-3.5.1.zip..."
    wget https://pypi.python.org/packages/source/s/setuptools/setuptools-3.5.1.zip
    fi
    fi
    echo "Installing Adafruit ADS1x15 Python library..."
    sudo python setup.py install
    touch /home/pi/.ads1x15

    if [ -f /boot/setuptools-3.5.1.zip ]; then
    echo "Removing setuptools from /boot..."
    sudo rm -f /boot/setuptools-3.5.1.zip
    fi
    rm -rf Adafruit_Python_ADS1x15
    fi

    if [ ! -f /home/pi/.minty ]; then
    @@ -96,6 +138,7 @@ EOF
    fi
    sudo ./MintyInstall.sh
    touch /home/pi/.minty
    rm -f MintyInstall.sh
    fi

    if [ ! -f /home/pi/.fbcp ]; then
    @@ -113,16 +156,16 @@ if [ ! -f /home/pi/.fbcp ]; then
    make
    sudo cp fbcp /usr/bin
    sudo chmod 777 /usr/bin/fbcp
    cd /home/pi
    rm -rf rpi-fbcp
    cd /home/pi
    sudo sed -i '/\"exit 0\"/!s/exit 0/\/usr\/bin\/fbcp \&\nexit 0/g' /etc/rc.local
    touch .fbcp
    rm -rf rpi-fbcp
    fi

    if [ ! -f /home/pi/safe.py ]; then
    cd /home/pi
    echo "Installing safe shutdown script..."
    cat > safe.py <<EOF
    cd /home/pi
    echo "Installing safe shutdown script..."
    cat > safe.py <<EOF
    #!/usr/bin/python
    import RPi.GPIO as GPIO
    import os, time
    @@ -132,34 +175,37 @@ GPIO.setup(17, GPIO.OUT) #ON control
    GPIO.output(17, GPIO.HIGH)
    while True:
    if (GPIO.input(27)):
    time.sleep(0.25)
    time.sleep(1.00)
    else:
    print ("Shutting down...")
    os.system("sudo shutdown -h now")
    break
    EOF

    sudo sed -i '/\"exit 0\"/!s/exit 0/\/home\/pi\/safe.py \&\nexit 0/g' /etc/rc.local
    sudo chmod 777 /home/pi/safe.py
    sudo sed -i '/\"exit 0\"/!s/exit 0/\/home\/pi\/safe.py \&\nexit 0/g' /etc/rc.local
    fi

    if [ ! -f /home/pi/.fbtft ]; then
    sudo raspi-config nonint do_spi 0
    sudo raspi-config nonint do_overscan 1
    sudo modprobe fbtft_device custom name=fb_ili9341 gpios=reset:25,dc:24 speed=80000000 fps=60 bgr=1 rotate=90
    dmesg | tail
    sudo cat > /etc/modprobe.d/fbtft.conf <<EOF
    options fbtft_device custom name=fb_ili9341 gpios=reset:25,dc:24 speed=80000000 fps=60 bgr=1 rotate=90 custom=1
    EOF

    sudo cat >> /etc/modules <<EOF
    spi-bcm2835
    fbtft_device
    EOF

    touch /home/pi/.fbtft
    sudo cat > /etc/modprobe.d/fbtft.conf <<EOF
    options fbtft_device custom name=fb_ili9341 gpios=reset:25,dc:24 speed=80000000 fps=60 bgr=1 rotate=90 custom=1
    EOF

    sudo modprobe fbtft_device custom name=fb_ili9341 gpios=reset:25,dc:24 speed=80000000 fps=60 bgr=1 rotate=90
    con2fbmap 1 1

    fi

    sudo chown -R pi:pi /home/pi

    echo "Done."
    echo
    echo "Rebooting..."
  2. mashurex revised this gist Feb 19, 2019. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions config.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    disable_overscan=1
    dtparam=i2c_arm=on
    dtparam=spi=on
    dtparam=audio=on
    gpu_mem_256=128
    gpu_mem_512=256
    gpu_mem_1024=256
    dtoverlay=pwm,pin=18,func=2
  3. mashurex revised this gist Feb 19, 2019. 1 changed file with 26 additions and 25 deletions.
    51 changes: 26 additions & 25 deletions gbz-install.sh
    Original file line number Diff line number Diff line change
    @@ -35,29 +35,30 @@ EOF
    sudo chown root:root retrogame.cfg
    sudo chmod 555 retrogame.cfg
    sudo mv retrogame.cfg /boot/retrogame.cfg
    cat > /home/pi/.emulationstation/es_input.cfg <<EOF
    <?xml version="1.0"?>
    <inputList>
    <inputAction type="onfinish">
    <command>/opt/retropie/supplementary/emulationstation/scripts/inputconfiguration.sh</command>
    </inputAction>
    <inputConfig type="keyboard" deviceName="Keyboard" deviceGUID="-1">
    <input name="a" type="key" id="97" value="1" />
    <input name="b" type="key" id="98" value="1" />
    <input name="down" type="key" id="1073741905" value="1" />
    <input name="hotkeyenable" type="key" id="32" value="1" />
    <input name="left" type="key" id="1073741904" value="1" />
    <input name="leftshoulder" type="key" id="108" value="1" />
    <input name="right" type="key" id="1073741903" value="1" />
    <input name="rightshoulder" type="key" id="114" value="1" />
    <input name="select" type="key" id="32" value="1" />
    <input name="start" type="key" id="13" value="1" />
    <input name="up" type="key" id="1073741906" value="1" />
    <input name="x" type="key" id="120" value="1" />
    <input name="y" type="key" id="121" value="1" />
    </inputConfig>
    </inputList>
    EOF
    # FIXME: This isn't working right, controller still needs configuring on 1st boot
    # cat > /home/pi/.emulationstation/es_input.cfg <<EOF
    #<?xml version="1.0"?>
    #<inputList>
    # <inputAction type="onfinish">
    # <command>/opt/retropie/supplementary/emulationstation/scripts/inputconfiguration.sh</command>
    # </inputAction>
    # <inputConfig type="keyboard" deviceName="Keyboard" deviceGUID="-1">
    # <input name="a" type="key" id="97" value="1" />
    # <input name="b" type="key" id="98" value="1" />
    # <input name="down" type="key" id="1073741905" value="1" />
    # <input name="hotkeyenable" type="key" id="32" value="1" />
    # <input name="left" type="key" id="1073741904" value="1" />
    # <input name="leftshoulder" type="key" id="108" value="1" />
    # <input name="right" type="key" id="1073741903" value="1" />
    # <input name="rightshoulder" type="key" id="114" value="1" />
    # <input name="select" type="key" id="32" value="1" />
    # <input name="start" type="key" id="13" value="1" />
    # <input name="up" type="key" id="1073741906" value="1" />
    # <input name="x" type="key" id="120" value="1" />
    # <input name="y" type="key" id="121" value="1" />
    # </inputConfig>
    #</inputList>
    #EOF

    fi

    @@ -145,8 +146,7 @@ if [ ! -f /home/pi/.fbtft ]; then
    sudo raspi-config nonint do_spi 0
    sudo raspi-config nonint do_overscan 1
    sudo modprobe fbtft_device custom name=fb_ili9341 gpios=reset:25,dc:24 speed=80000000 fps=60 bgr=1 rotate=90
    dmesg | tail
    con2fbmap 1 1
    dmesg | tail
    sudo cat >> /etc/modules <<EOF
    spi-bcm2835
    fbtft_device
    @@ -157,6 +157,7 @@ EOF
    options fbtft_device custom name=fb_ili9341 gpios=reset:25,dc:24 speed=80000000 fps=60 bgr=1 rotate=90 custom=1
    EOF

    con2fbmap 1 1
    fi

    echo "Done."
  4. mashurex revised this gist Feb 18, 2019. 1 changed file with 14 additions and 27 deletions.
    41 changes: 14 additions & 27 deletions gbz-install.sh
    Original file line number Diff line number Diff line change
    @@ -42,33 +42,20 @@ EOF
    <command>/opt/retropie/supplementary/emulationstation/scripts/inputconfiguration.sh</command>
    </inputAction>
    <inputConfig type="keyboard" deviceName="Keyboard" deviceGUID="-1">
    <input name="pageup" type="key" id="108" value="1"/>
    <input name="start" type="key" id="13" value="1"/>
    <input name="up" type="key" id="1073741906" value="1"/>
    <input name="a" type="key" id="97" value="1"/>
    <input name="b" type="key" id="98" value="1"/>
    <input name="down" type="key" id="1073741905" value="1"/>
    <input name="pagedown" type="key" id="114" value="1"/>
    <input name="right" type="key" id="1073741903" value="1"/>
    <input name="x" type="key" id="120" value="1"/>
    <input name="select" type="key" id="32" value="1"/>
    <input name="y" type="key" id="121" value="1"/>
    <input name="left" type="key" id="1073741904" value="1"/>
    </inputConfig>
    <inputConfig type="joystick" deviceName="USB Gamepad " deviceGUID="03000000790000001100000010010000">
    <input name="pageup" type="button" id="4" value="1"/>
    <input name="start" type="button" id="9" value="1"/>
    <input name="up" type="axis" id="1" value="-1"/>
    <input name="a" type="button" id="1" value="1"/>
    <input name="b" type="button" id="2" value="1"/>
    <input name="down" type="axis" id="1" value="1"/>
    <input name="pagedown" type="button" id="5" value="1"/>
    <input name="right" type="axis" id="0" value="1"/>
    <input name="x" type="button" id="0" value="1"/>
    <input name="select" type="button" id="8" value="1"/>
    <input name="y" type="button" id="3" value="1"/>
    <input name="left" type="axis" id="0" value="-1"/>
    </inputConfig>
    <input name="a" type="key" id="97" value="1" />
    <input name="b" type="key" id="98" value="1" />
    <input name="down" type="key" id="1073741905" value="1" />
    <input name="hotkeyenable" type="key" id="32" value="1" />
    <input name="left" type="key" id="1073741904" value="1" />
    <input name="leftshoulder" type="key" id="108" value="1" />
    <input name="right" type="key" id="1073741903" value="1" />
    <input name="rightshoulder" type="key" id="114" value="1" />
    <input name="select" type="key" id="32" value="1" />
    <input name="start" type="key" id="13" value="1" />
    <input name="up" type="key" id="1073741906" value="1" />
    <input name="x" type="key" id="120" value="1" />
    <input name="y" type="key" id="121" value="1" />
    </inputConfig>
    </inputList>
    EOF

  5. mashurex revised this gist Feb 18, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gbz-install.sh
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ if [ ! -f /usr/local/bin/retrogame ]; then
    curl -f -s -o /tmp/retrogame https://raw.githubusercontent.com/adafruit/Adafruit-Retrogame/master/retrogame
    sudo mv /tmp/retrogame /usr/local/bin
    sudo chmod 755 /usr/local/bin/retrogame
    sudo sed -i '/\"exit 0\"/!s/exit 0/\/usr\/local\/bind\/retrogame \&\nexit 0/g' /etc/rc.local
    sudo sed -i '/\"exit 0\"/!s/exit 0/\/usr\/local\/bin\/retrogame \&\nexit 0/g' /etc/rc.local
    fi

    if [ ! -f /boot/retrogame.cfg ]; then
  6. mashurex revised this gist Feb 18, 2019. 1 changed file with 4 additions and 11 deletions.
    15 changes: 4 additions & 11 deletions gbz-install.sh
    Original file line number Diff line number Diff line change
    @@ -127,14 +127,8 @@ if [ ! -f /home/pi/.fbcp ]; then
    sudo chmod 777 /usr/bin/fbcp
    cd /home/pi
    rm -rf rpi-fbcp
    grep fbcp /etc/rc.local >/dev/null
    if [ $? -eq 0 ]; then
    # fbcp already in rc.local, but make sure correct:
    sed -i "s/^.*fbcp.*$/\/usr\/local\/bin\/fbcp \&/g" /etc/rc.local >/dev/null
    else
    # Insert fbcp into rc.local before final 'exit 0'
    sed -i "s/^exit 0/\/usr\/local\/bin\/fbcp \&\\nexit 0/g" /etc/rc.local >/dev/null
    fi
    sudo sed -i '/\"exit 0\"/!s/exit 0/\/usr\/bin\/fbcp \&\nexit 0/g' /etc/rc.local
    touch .fbcp
    fi

    if [ ! -f /home/pi/safe.py ]; then
    @@ -164,8 +158,7 @@ if [ ! -f /home/pi/.fbtft ]; then
    sudo raspi-config nonint do_spi 0
    sudo raspi-config nonint do_overscan 1
    sudo modprobe fbtft_device custom name=fb_ili9341 gpios=reset:25,dc:24 speed=80000000 fps=60 bgr=1 rotate=90
    dmesg | tail
    fbset -fb /dev/fb1
    dmesg | tail
    con2fbmap 1 1
    sudo cat >> /etc/modules <<EOF
    spi-bcm2835
    @@ -183,4 +176,4 @@ echo "Done."
    echo
    echo "Rebooting..."
    reboot
    exit 0
    exit 0
  7. mashurex created this gist Feb 18, 2019.
    186 changes: 186 additions & 0 deletions gbz-install.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,186 @@
    #!/bin/bash
    set -e
    set -u

    sudo apt-get update
    sudo apt-get -y install git build-essential python-dev cmake

    if [ ! -f /usr/local/bin/retrogame ]; then
    cd /home/pi
    echo "Downloading and installing Adafruit-Retrogame binary..."
    curl -f -s -o /tmp/retrogame https://raw.githubusercontent.com/adafruit/Adafruit-Retrogame/master/retrogame
    sudo mv /tmp/retrogame /usr/local/bin
    sudo chmod 755 /usr/local/bin/retrogame
    sudo sed -i '/\"exit 0\"/!s/exit 0/\/usr\/local\/bind\/retrogame \&\nexit 0/g' /etc/rc.local
    fi

    if [ ! -f /boot/retrogame.cfg ]; then
    cd /home/pi
    echo "Installing retrogame.cfg..."
    cat > retrogame.cfg <<EOF
    UP 26 # 'UP' button
    DOWN 13 # 'DOWN' button
    LEFT 19 # 'LEFT' Button
    RIGHT 6 # 'RIGHT' button
    A 12 # 'A' Button
    B 7 # 'B' button
    X 22 # 'X' button
    Y 23 # 'Y' button
    ENTER 0 # 'ENTER' button
    SPACE 5 # 'SELECT' button
    L 21 # 'Left' Shoulder button
    R 20 # 'Right' Shoulder button
    EOF

    sudo chown root:root retrogame.cfg
    sudo chmod 555 retrogame.cfg
    sudo mv retrogame.cfg /boot/retrogame.cfg
    cat > /home/pi/.emulationstation/es_input.cfg <<EOF
    <?xml version="1.0"?>
    <inputList>
    <inputAction type="onfinish">
    <command>/opt/retropie/supplementary/emulationstation/scripts/inputconfiguration.sh</command>
    </inputAction>
    <inputConfig type="keyboard" deviceName="Keyboard" deviceGUID="-1">
    <input name="pageup" type="key" id="108" value="1"/>
    <input name="start" type="key" id="13" value="1"/>
    <input name="up" type="key" id="1073741906" value="1"/>
    <input name="a" type="key" id="97" value="1"/>
    <input name="b" type="key" id="98" value="1"/>
    <input name="down" type="key" id="1073741905" value="1"/>
    <input name="pagedown" type="key" id="114" value="1"/>
    <input name="right" type="key" id="1073741903" value="1"/>
    <input name="x" type="key" id="120" value="1"/>
    <input name="select" type="key" id="32" value="1"/>
    <input name="y" type="key" id="121" value="1"/>
    <input name="left" type="key" id="1073741904" value="1"/>
    </inputConfig>
    <inputConfig type="joystick" deviceName="USB Gamepad " deviceGUID="03000000790000001100000010010000">
    <input name="pageup" type="button" id="4" value="1"/>
    <input name="start" type="button" id="9" value="1"/>
    <input name="up" type="axis" id="1" value="-1"/>
    <input name="a" type="button" id="1" value="1"/>
    <input name="b" type="button" id="2" value="1"/>
    <input name="down" type="axis" id="1" value="1"/>
    <input name="pagedown" type="button" id="5" value="1"/>
    <input name="right" type="axis" id="0" value="1"/>
    <input name="x" type="button" id="0" value="1"/>
    <input name="select" type="button" id="8" value="1"/>
    <input name="y" type="button" id="3" value="1"/>
    <input name="left" type="axis" id="0" value="-1"/>
    </inputConfig>
    </inputList>
    EOF

    fi

    if [ ! -f /home/pi/.ads1x15 ]; then
    cd /home/pi
    if [ ! -d Adafruit_Python_ADS1x15 ]; then
    echo "Cloning Adafruit ADS1x15 Python library..."
    git clone https://github.com/adafruit/Adafruit_Python_ADS1x15.git
    fi

    cd Adafruit_Python_ADS1x15
    if [ ! -f setuptools-3.5.1.zip ]; then
    echo "Downloading setuptools-3.5.1.zip..."
    wget https://pypi.python.org/packages/source/s/setuptools/setuptools-3.5.1.zip
    fi
    echo "Installing Adafruit ADS1x15 Python library..."
    sudo python setup.py install
    touch /home/pi/.ads1x15
    fi

    if [ ! -f /home/pi/.minty ]; then
    cd /home/pi
    wget https://raw.githubusercontent.com/HoolyHoo/Mintybatterymonitor/master/MintyInstall.sh
    chmod +x MintyInstall.sh

    if [ ! -d Mintybatterymonitor ]; then
    echo "Installing Minty Battery Monitor..."
    git clone https://github.com/HoolyHoo/Mintybatterymonitor.git
    echo "Reconfiguring MintStart.sh..."
    cat > Mintybatterymonitor/MintyStart.sh <<EOF
    #!/usr/bin/env bash
    python /home/pi/Mintybatterymonitor/MintyBatteryMonitor.py &
    EOF

    fi
    sudo ./MintyInstall.sh
    touch /home/pi/.minty
    fi

    if [ ! -f /home/pi/.fbcp ]; then
    echo "Installing RPi FBCP..."
    cd /home/pi
    if [ ! -d rpi-fbcp ]; then
    git clone https://github.com/tasanakorn/rpi-fbcp.git
    fi

    cd rpi-fbcp
    rm -rf build
    mkdir build
    cd build
    cmake ..
    make
    sudo cp fbcp /usr/bin
    sudo chmod 777 /usr/bin/fbcp
    cd /home/pi
    rm -rf rpi-fbcp
    grep fbcp /etc/rc.local >/dev/null
    if [ $? -eq 0 ]; then
    # fbcp already in rc.local, but make sure correct:
    sed -i "s/^.*fbcp.*$/\/usr\/local\/bin\/fbcp \&/g" /etc/rc.local >/dev/null
    else
    # Insert fbcp into rc.local before final 'exit 0'
    sed -i "s/^exit 0/\/usr\/local\/bin\/fbcp \&\\nexit 0/g" /etc/rc.local >/dev/null
    fi
    fi

    if [ ! -f /home/pi/safe.py ]; then
    cd /home/pi
    echo "Installing safe shutdown script..."
    cat > safe.py <<EOF
    #!/usr/bin/python
    import RPi.GPIO as GPIO
    import os, time
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP) #Power switch
    GPIO.setup(17, GPIO.OUT) #ON control
    GPIO.output(17, GPIO.HIGH)
    while True:
    if (GPIO.input(27)):
    time.sleep(0.25)
    else:
    print ("Shutting down...")
    os.system("sudo shutdown -h now")
    break
    EOF

    sudo sed -i '/\"exit 0\"/!s/exit 0/\/home\/pi\/safe.py \&\nexit 0/g' /etc/rc.local
    fi

    if [ ! -f /home/pi/.fbtft ]; then
    sudo raspi-config nonint do_spi 0
    sudo raspi-config nonint do_overscan 1
    sudo modprobe fbtft_device custom name=fb_ili9341 gpios=reset:25,dc:24 speed=80000000 fps=60 bgr=1 rotate=90
    dmesg | tail
    fbset -fb /dev/fb1
    con2fbmap 1 1
    sudo cat >> /etc/modules <<EOF
    spi-bcm2835
    fbtft_device
    EOF

    touch /home/pi/.fbtft
    sudo cat > /etc/modprobe.d/fbtft.conf <<EOF
    options fbtft_device custom name=fb_ili9341 gpios=reset:25,dc:24 speed=80000000 fps=60 bgr=1 rotate=90 custom=1
    EOF

    fi

    echo "Done."
    echo
    echo "Rebooting..."
    reboot
    exit 0