-
Notifications
You must be signed in to change notification settings - Fork 0
/
button.py
49 lines (41 loc) · 1.3 KB
/
button.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
""" Notifies when button is clicked
"""
from threading import Thread
from multiprocessing import Process
from edi2c import ads1x15
from time import sleep
DEBUG = False
class Button():
adc = None
channel = None
def __init__(self, channel=1):
self.adc = ads1x15.ADS1X15(ic=ads1x15.IC_ADS1115)
self.channel = channel
def _monitor_button(self, callback, interrupt_callback):
already_down = False
try:
while True:
sleep(0.100)
if self.adc.read_single_ended(self.channel) < 1000:
down = True
if not already_down:
if not callback():
return
already_down = True
else:
already_down = False
except KeyboardInterrupt:
if callable(interrupt_callback):
interrupt_callback()
def monitor_button(self, callback, interrupt_callback=None):
task = Thread(target=self._monitor_button, args=(callback, interrupt_callback))
task.setDaemon(True)
task.start()
def update():
print 'toggled!'
return True
if __name__ == '__main__':
button = Button()
button.monitor_button(update)
while True:
sleep(0.100)