-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitch.py
More file actions
29 lines (25 loc) · 751 Bytes
/
switch.py
File metadata and controls
29 lines (25 loc) · 751 Bytes
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
from threading import Thread
import time
import RPi.GPIO as GPIO
POLL_FREQUENCY = .1
class Switch:
def __init__(self, pin, callback):
self.callback = callback
self.pin = pin
self.start(pin, callback)
self.thread = None
def start(self, pin, callback):
try:
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(self.pin, GPIO.IN)
self.thread = Thread(target=self.poll, args=())
self.thread.start()
except Exception as e:
print e
def poll(self):
print "Starting Switch"
while True:
time.sleep(POLL_FREQUENCY)
val = GPIO.input(self.pin)
self.callback(self, val)