Skip to content

Instantly share code, notes, and snippets.

@scruss
Last active October 19, 2017 03:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scruss/f6f3d99bbb3d499384b5f9768a9a015d to your computer and use it in GitHub Desktop.
Save scruss/f6f3d99bbb3d499384b5f9768a9a015d to your computer and use it in GitHub Desktop.
gpiozero example that could allow reboot and shutdown button on one key
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# example gpiozero code that could be used to have a reboot
# and a shutdown function on one GPIO button
# scruss - 2017-10
from gpiozero import Button
from signal import pause
held_for=0.0
def rls():
global held_for
print("released!")
print("released after", held_for, "seconds.")
# example code showing what to do with this time
if (held_for > 5.0):
print("I could shut down now …")
elif (held_for > 2.0):
print("I could reboot now …")
else:
print("I'm not going to do anything now …")
held_for = 0.0
def hld():
# callback for when button is held
# is called every hold_time seconds
global held_for
# need to use max() as held_time resets to zero on last callback
held_for = max(held_for, button.held_time + button.hold_time)
print("held for", held_for, "seconds.")
button=Button(4, hold_time=1.0, hold_repeat=True)
button.when_held = hld
button.when_released = rls
pause()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment