-
Notifications
You must be signed in to change notification settings - Fork 0
/
push_button.py
57 lines (44 loc) · 1.56 KB
/
push_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
50
51
52
53
54
55
56
57
import RPi.GPIO as GPIO
from time import sleep
from enum import Enum
import logging
DEFAULT_SLEEP_TIME = 0.05
class PushButtonType(Enum):
NC = 1 # normally CLOSED
NO = 2 # normally OPEN
def is_triggered(self, gpio_high):
return (self == PushButtonType.NC and gpio_high) or \
(self == PushButtonType.NO and not gpio_high)
class PushButton(object):
DEFAULT_GPIO_PIN = 14
def __init__(
self,
pin=DEFAULT_GPIO_PIN,
button_type=PushButtonType.NC,
sleep_time=DEFAULT_SLEEP_TIME,
verbose=False
):
self.sleep_time = sleep_time
self.button_gpio_pin = pin
self.button_type = button_type
self.gpio_inited = False
self.verbose = verbose
logging.basicConfig(level=logging.DEBUG if verbose else logging.INFO)
def wait_for_push(self, do_on_push, **kwargs):
if not self.gpio_inited:
self.init_gpio()
while True:
gpio_is_high = GPIO.input(self.button_gpio_pin)
if self.button_type.is_triggered(gpio_is_high):
self.__trigger(do_on_push, **kwargs)
sleep(self.sleep_time)
def __trigger(self, do_on_push, **kwargs):
if do_on_push is not None:
logging.debug('Push button at %s activated' % self.button_gpio_pin)
do_on_push(**kwargs)
def init_gpio(self):
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(self.verbose)
GPIO.setup(self.button_gpio_pin, GPIO.IN)
self.gpio_inited = True
return