Home | Trees | Indices | Help |
---|
|
1 # button.py 2 3 import RPi.GPIO as GPIO 4 import time 5 from threading import Thread 6 7 # Button event constants 8 BUTTON_PRESSED = 1 9 BUTTON_RELEASED = 2 10 BUTTON_LONGPRESSED = 3 11 BUTTON_CLICKED = 4 12 BUTTON_DOUBLECLICKED = 5 13 BUTTON_LONGPRESS_DURATION = 2 # default (in s) the button must be pressed to be a long press 14 BUTTON_DOUBLECLICK_TIME = 1 # default time (in s) to wait for a double click event 15 16 # --------------- class ClickThread -------------------------------2240 41 # --------------- class ButtonThread ------------------------------24 if Button.DEBUG: 25 print "===>ClickThread started" 26 self.isRunning = True 27 startTime = time.time() 28 while self.isRunning and (time.time() - startTime < BUTTON_DOUBLECLICK_TIME): 29 time.sleep(0.1) 30 if self.button.clickCount == 1 and not self.button.isLongPressEvent: 31 if self.button.xButtonListener != None: 32 self.button.xButtonListener(self.button, BUTTON_CLICKED) 33 self.button.clickThread = None 34 self.isRunning = False 35 if Button.DEBUG: 36 print "===>ClickThread terminated"374763 64 # --------------- class Button ------------------------------------49 if Button.DEBUG: 50 print "===>ButtonThread started" 51 self.isRunning = True 52 startTime = time.time() 53 while self.isRunning and (time.time() - startTime < BUTTON_LONGPRESS_DURATION): 54 time.sleep(0.1) 55 if self.isRunning: 56 if self.button.buttonListener != None: 57 self.button.buttonListener(self.button, BUTTON_LONGPRESSED) 58 if Button.DEBUG: 59 print "===>ButtonThread terminated"6066 DEBUG = False13368 self.buttonListener = None 69 self.xButtonListener = None 70 self.buttonThread = None 71 self.clickThread = None 72 self.clickCount = 0 73 self.isLongPressEvent = False 74 self.buttonPin = buttonPin 75 GPIO.setup(self.buttonPin, GPIO.IN, GPIO.PUD_UP) 76 GPIO.add_event_detect(self.buttonPin, GPIO.BOTH, self.onButtonEvent)7779 if event == BUTTON_PRESSED: 80 if self.buttonListener != None: 81 self.xButtonListener(button, BUTTON_PRESSED) 82 self.isLongPressEvent = False 83 if self.clickThread == None: 84 self.clickCount = 0 85 self.clickThread = ClickThread(self) 86 elif event == BUTTON_RELEASED: 87 if self.xButtonListener != None: 88 self.xButtonListener(button, BUTTON_RELEASED) 89 if self.isLongPressEvent and self.clickThread != None: 90 self.clickThread.stop() 91 self.clickThread = None 92 return 93 if self.clickThread != None and self.clickThread.isRunning: 94 self.clickCount += 1 95 if self.clickCount == 2: 96 self.clickThread.stop() 97 self.clickThread = None 98 if self.xButtonListener != None: 99 self.xButtonListener(button, BUTTON_DOUBLECLICKED) 100 else: 101 self.clickThread = None 102 elif event == BUTTON_LONGPRESSED: 103 self.isLongPressEvent = True 104 if self.xButtonListener != None: 105 self.xButtonListener(self, BUTTON_LONGPRESSED)106108 # switch may bounce: down-up-up, down-up-down, down-down-up etc. in fast sequence 109 if GPIO.input(self.buttonPin) == GPIO.LOW: 110 if self.buttonThread == None: # down-down is suppressed 111 if Button.DEBUG: 112 print "->ButtonDown" 113 self.buttonThread = ButtonThread(self) 114 self.buttonThread.start() 115 if self.buttonListener != None: 116 self.buttonListener(self, BUTTON_PRESSED) 117 else: 118 if self.buttonThread != None: # up-up is suppressed 119 if Button.DEBUG: 120 print "->ButtonUp" 121 self.buttonThread.stop() 122 self.buttonThread.join(200) # wait until finished 123 self.buttonThread = None 124 if self.buttonListener != None: 125 self.buttonListener(self, BUTTON_RELEASED)126 130
Home | Trees | Indices | Help |
---|
Generated by Epydoc 3.0.1 on Tue May 03 07:51:22 2016 | http://epydoc.sourceforge.net |