From 3de223e8d1fae23a99b4825ff40562ce1dbbec85 Mon Sep 17 00:00:00 2001 From: N6RDV <97379462+N6RDV@users.noreply.github.com> Date: Mon, 9 May 2022 20:56:25 -0700 Subject: [PATCH 1/7] Add long press delay Add long_press delay to prevent inadvertent shutdowns. Variable long_press can be set to number of 0.1 s intervals (10 for 1 s, 30 for 3s, etc.), defining how long user have to keep button pressed to shutdown Raspberry Pi. If set to 0 - button press would execute shutdown command immediately --- listen-for-shutdown.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/listen-for-shutdown.py b/listen-for-shutdown.py index cecc42b..0a1f5ab 100755 --- a/listen-for-shutdown.py +++ b/listen-for-shutdown.py @@ -1,12 +1,27 @@ #!/usr/bin/env python - import RPi.GPIO as GPIO import subprocess +import time +from time import sleep +gpio_pin=3 +long_press=30 GPIO.setmode(GPIO.BCM) -GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP) -GPIO.wait_for_edge(3, GPIO.FALLING) +GPIO.setup(gpio_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) + +def main(): + while True: + GPIO.wait_for_edge(gpio_pin, GPIO.FALLING) + counter = 0 + while GPIO.input(gpio_pin) == 0 and counter < long_press: + time.sleep(0.1) + counter += 1 + print('Shutdown button is pressed ' + str(counter)) + if counter >= long_press: + print('Shutting down') + subprocess.call(['shutdown', '-h', 'now'], shell=False) -subprocess.call(['shutdown', '-h', 'now'], shell=False) +if __name__ == '__main__': + main() From 6846a9dab79e085a211059689dc0c62a958b859c Mon Sep 17 00:00:00 2001 From: N6RDV <97379462+N6RDV@users.noreply.github.com> Date: Mon, 9 May 2022 21:05:50 -0700 Subject: [PATCH 2/7] Update listen-for-shutdown.py --- listen-for-shutdown.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/listen-for-shutdown.py b/listen-for-shutdown.py index 0a1f5ab..7fb483b 100755 --- a/listen-for-shutdown.py +++ b/listen-for-shutdown.py @@ -6,7 +6,7 @@ from time import sleep gpio_pin=3 -long_press=30 +long_press=0 GPIO.setmode(GPIO.BCM) GPIO.setup(gpio_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) From 465bcafa350e5c44ee40c5f95a70c50d0cb248f4 Mon Sep 17 00:00:00 2001 From: N6RDV <97379462+N6RDV@users.noreply.github.com> Date: Mon, 9 May 2022 21:10:26 -0700 Subject: [PATCH 3/7] Update listen-for-shutdown.py --- listen-for-shutdown.py | 1 + 1 file changed, 1 insertion(+) diff --git a/listen-for-shutdown.py b/listen-for-shutdown.py index 7fb483b..e725544 100755 --- a/listen-for-shutdown.py +++ b/listen-for-shutdown.py @@ -1,5 +1,6 @@ #!/usr/bin/env python + import RPi.GPIO as GPIO import subprocess import time From b8507874aeae2bd68595ae43038aaded6a81569c Mon Sep 17 00:00:00 2001 From: N6RDV <97379462+N6RDV@users.noreply.github.com> Date: Tue, 27 Aug 2024 14:39:44 -0700 Subject: [PATCH 4/7] Update listen-for-shutdown.py --- listen-for-shutdown.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/listen-for-shutdown.py b/listen-for-shutdown.py index e725544..c588a2c 100755 --- a/listen-for-shutdown.py +++ b/listen-for-shutdown.py @@ -6,23 +6,33 @@ import time from time import sleep -gpio_pin=3 -long_press=0 +button_gpio=3 +relay_gpio=4 + +short_press=0.5 +long_press=10 GPIO.setmode(GPIO.BCM) -GPIO.setup(gpio_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) +GPIO.setup(button_gpio, GPIO.IN, pull_up_down=GPIO.PUD_UP) +GPIO.setup(relay_gpio, GPIO.OUT) def main(): + relay_state=False + GPIO.output(relay_gpio, relay_state) while True: - GPIO.wait_for_edge(gpio_pin, GPIO.FALLING) + GPIO.wait_for_edge(button_gpio, GPIO.FALLING) counter = 0 - while GPIO.input(gpio_pin) == 0 and counter < long_press: + while GPIO.input(button_gpio) == 0 and counter < long_press: time.sleep(0.1) - counter += 1 + counter = round(counter + 0.1, 1) print('Shutdown button is pressed ' + str(counter)) if counter >= long_press: print('Shutting down') subprocess.call(['shutdown', '-h', 'now'], shell=False) + elif counter >= short_press: + relay_state = not relay_state + print('Changing relay state to: ' + str(relay_state)) + GPIO.output(relay_gpio, relay_state) if __name__ == '__main__': main() From b244c3de87e72e5f77cb9e8f182ae7b5b37ed77d Mon Sep 17 00:00:00 2001 From: N6RDV <97379462+N6RDV@users.noreply.github.com> Date: Tue, 27 Aug 2024 14:43:28 -0700 Subject: [PATCH 5/7] Update listen-for-shutdown.py --- listen-for-shutdown.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/listen-for-shutdown.py b/listen-for-shutdown.py index c588a2c..172fa6c 100755 --- a/listen-for-shutdown.py +++ b/listen-for-shutdown.py @@ -9,8 +9,8 @@ button_gpio=3 relay_gpio=4 -short_press=0.5 -long_press=10 +short_press=0.1 +long_press=5 GPIO.setmode(GPIO.BCM) GPIO.setup(button_gpio, GPIO.IN, pull_up_down=GPIO.PUD_UP) @@ -33,6 +33,7 @@ def main(): relay_state = not relay_state print('Changing relay state to: ' + str(relay_state)) GPIO.output(relay_gpio, relay_state) + time.sleep(0.5) if __name__ == '__main__': main() From 76ebf78a0f0644a3c617801f1131484e391fcf0e Mon Sep 17 00:00:00 2001 From: N6RDV <97379462+N6RDV@users.noreply.github.com> Date: Tue, 27 Aug 2024 14:45:37 -0700 Subject: [PATCH 6/7] Update listen-for-shutdown.py --- listen-for-shutdown.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/listen-for-shutdown.py b/listen-for-shutdown.py index 172fa6c..c13406e 100755 --- a/listen-for-shutdown.py +++ b/listen-for-shutdown.py @@ -17,7 +17,7 @@ GPIO.setup(relay_gpio, GPIO.OUT) def main(): - relay_state=False + relay_state=True GPIO.output(relay_gpio, relay_state) while True: GPIO.wait_for_edge(button_gpio, GPIO.FALLING) From d96786f40a9d6595d2b0262160261e017e78d9b0 Mon Sep 17 00:00:00 2001 From: N6RDV <97379462+N6RDV@users.noreply.github.com> Date: Tue, 27 Aug 2024 14:57:58 -0700 Subject: [PATCH 7/7] Update listen-for-shutdown.py --- listen-for-shutdown.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/listen-for-shutdown.py b/listen-for-shutdown.py index c13406e..263cc86 100755 --- a/listen-for-shutdown.py +++ b/listen-for-shutdown.py @@ -17,8 +17,7 @@ GPIO.setup(relay_gpio, GPIO.OUT) def main(): - relay_state=True - GPIO.output(relay_gpio, relay_state) + relay_state=GPIO.input(relay_gpio) while True: GPIO.wait_for_edge(button_gpio, GPIO.FALLING) counter = 0