Module button
[hide private]
[frames] | no frames]

Source Code for Module button

  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 ------------------------------- 
17 -class ClickThread(Thread):
18 - def __init__(self, button):
19 Thread.__init__(self) 20 self.start() 21 self.button = button
22
23 - def run(self):
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"
37
38 - def stop(self):
39 self.isRunning = False
40 41 # --------------- class ButtonThread ------------------------------
42 -class ButtonThread(Thread):
43 - def __init__(self, button):
44 Thread.__init__(self) 45 self.isRunning = False 46 self.button = button
47
48 - def run(self):
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"
60
61 - def stop(self):
62 self.isRunning = False
63 64 # --------------- class Button ------------------------------------
65 -class Button():
66 DEBUG = False
67 - def __init__(self, buttonPin):
68 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)
77
78 - def onXButtonEvent(self, button, event):
79 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)
106
107 - def onButtonEvent(self, channel):
108 # 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
127 - def addXButtonListener(self, listener):
128 self.addButtonListener(self.onXButtonEvent) 129 self.xButtonListener = listener
130
131 - def addButtonListener(self, listener):
132 self.buttonListener = listener
133