-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontrols.py
52 lines (31 loc) · 1.14 KB
/
controls.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
"""
Test a few ideas about responding to user input in my Raspberry Pi.
For this example a couple of LEDs and buttons are wired up.
I'm using the cool RPIO feature of setting up a callback function that
gets called when I do something like push a button, or release a button.
"""
import time
import RPIO
ix_power = 17
ix_led = 23
ix_button_red = 24
ix_button_green = 25
info = {'keep_looping': True}
def button_red(gpio_id, val):
print("red %s: %s" % (gpio_id, val))
info['keep_looping'] = False
def button_green(gpio_id, val):
print("green %s: %s" % (gpio_id, val))
# Initialize GPIO interrupt callbacks
RPIO.add_interrupt_callback(ix_button_red, button_red, edge='falling', pull_up_down=RPIO.PUD_UP,
threaded_callback=True, debounce_timeout_ms=100)
RPIO.add_interrupt_callback(ix_button_green, button_green, edge='falling', pull_up_down=RPIO.PUD_UP,
threaded_callback=True, debounce_timeout_ms=100)
# Enter main event loop.
RPIO.wait_for_interrupts(threaded=True)
while info['keep_looping']:
time.sleep(0.01)
# Finish,
print('\nExit loop!')
RPIO.cleanup()
print('Done')