Hardware: Banana pi M2 Zero
OS Armbian_community_25.5.0-trunk.185_Bananapim2zero_bookworm_current_6.6.75_minimal
pyGPIO - A 'more general' python GPIO library from "chwe".
Question:
Could the below python script work for a safe "shutdown" of the MBI M2Z?
_____________________________________________________________________________
#!/usr/bin/python
# shutdown/reboot Banana Pi M2 Zero with pushbutton
from pyGPIO.gpio import gpio, connector
from subprocess import call
from datetime import datetime
import time
# Initialize GPIO
gpio.init()
# Pushbutton connected to this GPIO pin
shutdownPin = connector.GPIOp11 # Adjust based on your wiring
# If button pressed for at least this long, shut down. If less, reboot.
shutdownMinSeconds = 3
# Button debounce time in seconds
debounceSeconds = 0.1
# Set up the GPIO pin as input with pull-up resistor
gpio.setcfg(shutdownPin, gpio.INPUT)
gpio.pullup(shutdownPin, gpio.PULLUP)
buttonPressedTime = None
def buttonStateChanged(pin):
global buttonPressedTime
if gpio.input(pin) == gpio.LOW: # Button is down (active low)
if buttonPressedTime is None:
buttonPressedTime = datetime.now()
else: # Button is up
if buttonPressedTime is not None:
elapsed = (datetime.now() - buttonPressedTime).total_seconds()
buttonPressedTime = None
if elapsed >= shutdownMinSeconds:
# Button pressed for more than specified time, shutdown
call(['shutdown', '-h', 'now'], shell=False)
# Uncomment the following lines if you want to enable reboot functionality
# elif elapsed >= debounceSeconds:
# # Button pressed for a shorter time, reboot
# call(['shutdown', '-r', 'now'], shell=False)
# Subscribe to button presses
gpio.set_irq(shutdownPin, gpio.BOTH, buttonStateChanged)
while True:
# Sleep to reduce unnecessary CPU usage
time.sleep(5)