From 1a2c39d186bddea224ac3c3fde59ebbd74e9bd8a Mon Sep 17 00:00:00 2001 From: Toni Lekic <96660920+tonilekic@users.noreply.github.com> Date: Mon, 30 Sep 2024 20:30:37 +0200 Subject: [PATCH] fix: add a custom function for polling the switch The original script throws a RuntimeError on a RPi4: GPIO.wait_for_edge(3, GPIO.FALLING) RuntimeError: Error waiting for edge. This is due the wait_for_edge function that is not compatible with newer python versions. This commit will add a function that polls the corresponding GPIO input. --- listen-for-shutdown.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/listen-for-shutdown.py b/listen-for-shutdown.py index cecc42b..eb6e162 100755 --- a/listen-for-shutdown.py +++ b/listen-for-shutdown.py @@ -3,10 +3,16 @@ import RPi.GPIO as GPIO import subprocess +import time +switchPin = 3 + +def wait_for_falling_edge(gpioNumber): + while GPIO.input(gpioNumber): + time.sleep(.01) GPIO.setmode(GPIO.BCM) -GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP) -GPIO.wait_for_edge(3, GPIO.FALLING) +GPIO.setup(switchPin, GPIO.IN) +wait_for_falling_edge(switchPin) subprocess.call(['shutdown', '-h', 'now'], shell=False)