Changed code to python3, add README.md
This commit is contained in:
94
button.py
94
button.py
@@ -9,54 +9,54 @@ GPIO.setmode(GPIO.BCM) # set up BCM GPIO numbering
|
||||
#GPIO.setup(20, GPIO.IN, pull_up_down = GPIO.PUD_UP)
|
||||
|
||||
class Button:
|
||||
'Handle button stuff'
|
||||
starttime=0
|
||||
ispushed=0
|
||||
waspushed=0
|
||||
presstime=0
|
||||
ignore=0
|
||||
"""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 __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 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
|
||||
def WasLong(self):
|
||||
if self.presstime > 2:
|
||||
self.waslong=0
|
||||
self.ignore=1
|
||||
return 1
|
||||
else:
|
||||
return 0
|
||||
|
||||
|
||||
Reference in New Issue
Block a user