63 lines
1.8 KiB
Python
63 lines
1.8 KiB
Python
#!/usr/bin/env python2.7
|
|
|
|
import time
|
|
import RPi.GPIO as GPIO
|
|
from time import sleep # this lets us have a time delay (see line 12)
|
|
|
|
GPIO.setmode(GPIO.BCM) # set up BCM GPIO numbering
|
|
#GPIO.setup(20, GPIO.IN) # set GPIO25 as input (button)
|
|
#GPIO.setup(20, GPIO.IN, pull_up_down = GPIO.PUD_UP)
|
|
|
|
class Button:
|
|
"""Handle clocky button stuff"""
|
|
starttime=0
|
|
ispushed=0
|
|
waspushed=0
|
|
presstime=0
|
|
ignore=0
|
|
|
|
def __init__(self, gpio):
|
|
print("initialize button on gpio %d" %gpio)
|
|
self.gpio = gpio
|
|
GPIO.setup(self.gpio, GPIO.IN, pull_up_down = GPIO.PUD_UP)
|
|
GPIO.add_event_detect(self.gpio, GPIO.BOTH, callback=self.my_callback)
|
|
self.presstime=0
|
|
self.starttime=0
|
|
self.presstime=0
|
|
self.waslong=0
|
|
|
|
def my_callback(self,channel):
|
|
if not GPIO.input(channel): # button is pressed
|
|
# print "[press] on %d"%self.gpio
|
|
self.starttime=time.time()
|
|
self.ispushed=1
|
|
else: # button released
|
|
self.presstime=time.time()-self.starttime
|
|
# print "[release] on %d / <pressed> for=%f sconds"%(self.gpio,self.presstime)
|
|
if self.ispushed:
|
|
self.waspushed=1
|
|
self.ispushed=0
|
|
|
|
def WasPressed(self):
|
|
if self.waspushed:
|
|
self.waspushed=0
|
|
return 1
|
|
elif self.ispushed:
|
|
self.presstime=time.time()-self.starttime
|
|
if self.presstime > 2:
|
|
self.ispushed=0
|
|
self.waspushed=1
|
|
self.waslong=1
|
|
|
|
else:
|
|
return 0
|
|
|
|
def WasLong(self):
|
|
if self.presstime > 2:
|
|
self.waslong=0
|
|
self.ignore=1
|
|
return 1
|
|
else:
|
|
return 0
|
|
|