-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpio_button.py
More file actions
38 lines (32 loc) · 814 Bytes
/
gpio_button.py
File metadata and controls
38 lines (32 loc) · 814 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
30
31
32
33
34
35
36
37
38
import RPi.GPIO as GPIO
class GPIOButton():
def __init__(self, button_pin, led_pin):
self.button_pin = button_pin
self.led_pin = led_pin
self.triggered = False
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.button_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(self.led_pin, GPIO.OUT)
GPIO.add_event_detect(
self.button_pin,
GPIO.FALLING,
callback=self.trigger,
bouncetime=100
)
def trigger(self, channel):
self.triggered = True
def check(self):
#return not GPIO.input(self.button_pin)
return self.triggered
def reset(self):
self.triggered = False
def light(self, on_off):
GPIO.output(self.led_pin, on_off)
if __name__ == '__main__':
from time import sleep
donbut = GPIOButton(11, 7)
while not donbut.check():
sleep(.1)
donbut.light(1)
sleep(1)
donbut.light(0)