-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathsample_input.py
More file actions
132 lines (98 loc) · 3.48 KB
/
sample_input.py
File metadata and controls
132 lines (98 loc) · 3.48 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
class Counter:
def __init__(self):
self.count = 0
def count_up(self, channel):
self.count += 1
print('GPIO%02d count=%d' % (channel, self.count))
def __eq__(self, other):
return self.count == other
def __lt__(self, other):
return self.count < other
def main():
import RPi.GPIO as GPIO
import time
try:
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup([5, 6, 13, 19, 26], GPIO.OUT)
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_OFF)
GPIO.setup(9, GPIO.IN, pull_up_down=GPIO.PUD_OFF)
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_OFF)
print('Press GPIO10.')
while GPIO.input(10) == GPIO.LOW:
time.sleep(0.1)
GPIO.output(5, GPIO.HIGH)
channel = GPIO.wait_for_edge(10, GPIO.FALLING)
if channel == 10:
GPIO.output(6, GPIO.HIGH)
channel = GPIO.wait_for_edge(10, GPIO.RISING)
if channel == 10:
GPIO.output(13, GPIO.HIGH)
channel = GPIO.wait_for_edge(10, GPIO.BOTH)
if channel == 10:
GPIO.output(19, GPIO.HIGH)
while True:
print('Wait timeout.')
channel = GPIO.wait_for_edge(10, GPIO.BOTH, timeout=1000)
if channel is None:
GPIO.output(26, GPIO.HIGH)
break
print('Press GPIO10.')
GPIO.add_event_detect(10, GPIO.RISING)
while not GPIO.event_detected(10):
time.sleep(1)
GPIO.output(5, GPIO.LOW)
GPIO.remove_event_detect(10)
GPIO.add_event_detect(10, GPIO.FALLING)
while not GPIO.event_detected(10):
time.sleep(1)
GPIO.output(6, GPIO.LOW)
GPIO.remove_event_detect(10)
GPIO.add_event_detect(10, GPIO.BOTH)
while not GPIO.event_detected(10):
time.sleep(1)
GPIO.output(13, GPIO.LOW)
GPIO.remove_event_detect(10)
def risen(ch):
print('risen GPIO%02d' % ch)
def fallen(ch):
print('fallen GPIO%02d' % ch)
GPIO.add_event_detect(10, GPIO.RISING, risen)
while not GPIO.event_detected(10):
time.sleep(1)
GPIO.output(5, GPIO.HIGH)
GPIO.remove_event_detect(10)
GPIO.add_event_detect(10, GPIO.FALLING, fallen)
while not GPIO.event_detected(10):
time.sleep(1)
GPIO.output(6, GPIO.HIGH)
GPIO.remove_event_detect(10)
GPIO.add_event_detect(10, GPIO.BOTH, risen)
while not GPIO.event_detected(10):
time.sleep(1)
GPIO.output(13, GPIO.HIGH)
GPIO.remove_event_detect(10)
def changed(ch):
print('changed GPIO%02d' % ch)
GPIO.add_event_detect(10, GPIO.BOTH)
GPIO.add_event_callback(10, fallen)
GPIO.add_event_callback(10, changed)
while not GPIO.event_detected(10):
time.sleep(1)
GPIO.output(26, GPIO.LOW)
GPIO.remove_event_detect(10)
print('Press! Press! Press!')
counter = Counter()
GPIO.add_event_detect(10, GPIO.RISING, callback=counter.count_up, bouncetime=100)
GPIO.add_event_callback(10, counter.count_up, bouncetime=500)
while counter < 10:
time.sleep(1)
GPIO.output(19, GPIO.LOW)
GPIO.remove_event_detect(10)
time.sleep(1)
finally:
GPIO.cleanup()
if __name__ == '__main__':
import logging
logging.basicConfig(level=logging.DEBUG)
main()