How To Shutdown Your GBZ By Pressing Two Buttons

Various user-contributed guides for software-related things
Post Reply
User avatar
tinkerBOY
Posts: 700
Joined: Tue May 30, 2017 4:00 am
Has thanked: 293 times
Been thanked: 199 times

How To Shutdown Your GBZ By Pressing Two Buttons

Post by tinkerBOY » Tue Jul 11, 2017 9:40 pm

I've been searching for a way to shutdown my GBZ using two GPIO buttons but couldn't find any solution online that does not eat too much CPU performance.

There's this GPIOZERO shutdown method that let's you specify a "hold_time" but I want a more immediate shutdown response instead of waiting for a few seconds.

Code: Select all

from gpiozero import Button
from subprocess import check_call
from signal import pause

def shutdown():
    check_call(['sudo', 'poweroff'])

shutdown_btn = Button(17, hold_time=2)
shutdown_btn.when_held = shutdown

pause()
My solution is to use two GPIO buttons that you press at the same time to issue a shutdown. I have tried GPIOZERO's "is_pressed" function but it consumes too much CPU and wasn't sure how to do it properly.

So here's how you do it.

1. Change the value of gpio_pin1 and gpio_pin2 to your own GPIO pins that correspond to the two buttons you want to use. Copy and save it to /home/pi as shutdown.py.

Code: Select all

import RPi.GPIO as GPIO
from subprocess import check_call
import time

GPIO.setmode(GPIO.BCM)
gpio_pin_1 = 13 
gpio_pin_2 = 23

GPIO.setup(gpio_pin_1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(gpio_pin_2, GPIO.IN, pull_up_down=GPIO.PUD_UP)

TD = 0.2
gpio_pin_1_ptime = 0
gpio_pin_2_ptime = 0 

def shutdown():
    if (abs(gpio_pin_1_ptime - gpio_pin_2_ptime)) < TD:
       check_call(['sudo', 'poweroff'])

def btn_func(b):
    if b == 1:
        global gpio_pin_1_ptime
        gpio_pin_1_ptime = time.time()
        
    if b == 2:
        global gpio_pin_2_ptime
        gpio_pin_2_ptime = time.time()
        
    shutdown()

GPIO.add_event_detect(gpio_pin_1, GPIO.FALLING, callback=lambda x: btn_func(1), bouncetime=500)
GPIO.add_event_detect(gpio_pin_2, GPIO.FALLING, callback=lambda x: btn_func(2), bouncetime=500)

while 1:
   time.sleep(10)
2. Edit the file /etc/rc.local and add the following before the line exit 0 and save. Reboot your GBZ. The script should now work systemwide.

Code: Select all

sudo python /home/pi/shutdown.py &
Update: Code uses native RPi.GPIO so you don't have to install any additional packages.
New! tinkerBOY PowerSwitch v1.0 with Safe Shutdown
Game Boy Zero Controllers available @ tinkerBOY.xyz * Support
tinkerBOY Controller v3.0 - built-in usb controller, usb audio, and usb hub
tinkerBOY Controller v2.0 - GPIO buttons and builtin PWM Audio and Amplifier
tinkerBOY Controller v1.1 - simple pcb button
* DPI Adapter - better display for GBZ
* keyboard converters - IBM XT/AT Soarer's Converter | ADB to USB Converter

User avatar
fraggle
Posts: 50
Joined: Mon Mar 06, 2017 2:28 pm
Has thanked: 18 times
Been thanked: 16 times

Re: How To Shutdown Your GBZ By Pressing Two Buttons

Post by fraggle » Sun Nov 05, 2017 8:31 pm

This is great, although editing rc.local with a sudo command is a bit of a kludge these days. Much better to create a service via systemd, which by default will run as root.

1. Create a service file
sudo nano /lib/systemd/system/shutdown.service
2. Enter the following (presuming your python script is as above)

Code: Select all

[Unit]
Description=Shutdown Service
After=multi-user.target

[Service]
Type=idle
ExecStart=/usr/bin/python /home/pi/shutdown.py

[Install]
WantedBy=multi-user.target
Then set the correct permissions, reload systemctl to pick up the service, enable it and reboot.
sudo chmod 644 /lib/systemd/system/shutdown.service
sudo systemctl daemon-reload
sudo systemctl enable myscript.service
sudo reboot
You can test this is running by running by
sudo systemctl status shutdown.service
Also start/stop with
sudo systemctl start shutdown.service
sudo systemctl stop shutdown.service

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest