-
Notifications
You must be signed in to change notification settings - Fork 153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Possible to add long press wait to listen-for-shutdown.py? #2
Comments
I think in this case you would not call I think the logic of your script makes sense. Have you tested this? If I have time soon, perhaps I'll add this feature to the script. |
+1 |
I recently replaced RPi.GPIO by gpizero library on my Raspberry. Using this library should give simplier result (not yet tested).
See answer to using a button long press vs short press |
I had some time to test my previous suggestion for longpress. It does not work as when_pressed is called anytime button is pressed (obviously 😁 ). Thus the script should wait for button to be released. It is still more compact than using RPi.GPIO as the timing and wait are handled by gpiozero library.
|
Is this tested working? Any chance of a pull request? Edit, didn't work for me. Pi instantly shuts down upon button press |
@Malakai-Mods yes and only @ZLevine can tell. Short press will trigger a reboot and you'll see the following in an open shell:
Long press will trigger a halt with the following message:
Anyway it will probably not make it to the main repository because this script support an article about the power off/on switch. However, you can use https://github.com/Nizhile/pi-power-button and use branch gpiozero_long_press to get this feature. |
@Nizhile can this be modified such that only long press causes shutdown: from gpiozero import Button
import subprocess
perform_halt = 0
def longpress():
global perform_halt
perform_halt = 1
button = Button(3, hold_time=2)
button.when_held = longpress
while True:
button.wait_for_press()
button.wait_for_release()
if (perform_halt):
subprocess.call(['shutdown', '-h', 'now'], shell=False)
break |
🙈 😅 I realized I could have done just this: from gpiozero import Button
import subprocess
def longpress():
subprocess.call(['shutdown', '-h', 'now'], shell=False)
button = Button(3, hold_time=2)
button.when_held = longpress
pause() |
That is the reason why I find gpiozero interesting 😊 |
@Nizhile I can't figure out, how to adjust your script in order to achieve the following. I hope you or anyone else can help me out here. longpress -> run bash-script A To test this, I adjusted The file "reboot" is generated but for the first part it shuts down the pi anyways. I also tried to change Python is just not mine but I wanted to show that I at least tried. |
The current snippets in the comment all rely on the fact that the script will launch a single command. |
Yeah, you're right! Once one of the presses is triggered, the script will end itself. 🙈 |
I've been using your script for a while now and I really like how clean it is. I've used it on every Pi setup I've done so far (7 total for various around the house projects and for friends) and it has worked flawlessly!
Anyway I recently set up a Octoprint for my Maker Select Plus and installed a button on the side of my case. I have accidentally tapped the button while loading and unloading filament several times. I was looking to add a wait/time delay so that a long press would trigger the script. There are other tutorials out there but i really like what I've got with your script. Also many of these tutorials offer a reboot on a short press/shutdown on a long press. I am not a professional coder, changed majors from computer science way back in 1999 so it is all quite rusty.
So really I'd like your opinion on wether the modified code below would actually work or what you would do to change it. It's hacked together from other sources and modified to the best of my abilities. I imported time and sleep in the header. Only big issue based on my limited knowledge is the 'if' call at the beginning since there isn't an 'else' that follows. I learned C way back in the day so I thought it might need a 'do'.
Note: Formatting on inserting code is strange on Github. I'm pretty new here so I added quote indentions because the code did not format correctly when copying it here.
Please take a look and let me know!
The text was updated successfully, but these errors were encountered: