-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.py
35 lines (29 loc) · 934 Bytes
/
input.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import threading
import RPi.GPIO as GPIO
import time
'''
A named input that reads a GPIO pin
'''
class Input(threading.Thread):
_pressed = False
def __init__(self, name, pin):
threading.Thread.__init__(self)
self._pressed = False
self.channel = pin
self.name = name
print "Initializing channel {} as input for {}".format(self.channel, self.name)
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.channel, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
self.daemon = True
self.start()
def run(self):
previous = 0
while True:
current = GPIO.input(self.channel)
time.sleep(0.05)
if current == 1 and previous == 0:
self._pressed = True
print "{} was triggered.".format(self.name)
while self._pressed:
time.sleep(0.05)
previous = current