/
nuove-funz-gpio-test.py
executable file
·208 lines (186 loc) · 5.77 KB
/
nuove-funz-gpio-test.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env python
# -*- coding: latin-1 *-*
# -*- coding: cp1252 -*-
#Raspberry Pi - GPIO interface
#Prova test GPIO INPUT-OUTPUT-PWM
import RPi.GPIO as GPIO
import time
################################### Inizializzazioni
#LED_PIN = 12
#SWITCH_PIN = 18
LED_PIN = 18 #Output PWM
SWITCH_PIN = 17 #Input
GPIO.setmode(GPIO.BCM)
def test_output():
print "OUTPUT test"
for i in range(10):
time.sleep(0.1)
GPIO.output(LED_PIN, GPIO.HIGH)
if GPIO.input(LED_PIN) != GPIO.HIGH:
print "Read back of output failed."
time.sleep(0.1)
GPIO.output(LED_PIN, GPIO.LOW)
if GPIO.input(LED_PIN) != GPIO.LOW:
print "Read back of output failed."
def test_input():
print "INPUT test (Ctrl-C to stop)"
try:
while 1:
GPIO.output(LED_PIN, GPIO.input(SWITCH_PIN))
time.sleep(0.02) # 20 ms
except KeyboardInterrupt:
return
def test_rising():
def cb(chan):
print "Callback 1 - this should produce an exception"
def cb2(chan):
print "Callback 2 called - channel", s, chan
print "Rising edge test"
print "5 second sample for event_detected function"
try:
GPIO.add_event_detect(LED_PIN, GPIO.RISING)
print "Fail - added event to an output, not produced RuntimeError"
except RuntimeError:
pass
GPIO.add_event_detect(SWITCH_PIN, GPIO.RISING)
time.sleep(5)
if GPIO.event_detected(SWITCH_PIN):
print "Event detected"
else:
print "Event not detected"
print "5 seconds for callback function (which should produce exceptions)"
input ("Press return to start: ")
GPIO.add_event_callback(SWITCH_PIN, cb)
GPIO.add_event_callback(SWITCH_PIN, cb2)
time.sleep(5)
GPIO.remove_event_detect(SWITCH_PIN);
print "Blocking wait for rising edge..."
GPIO.wait_for_edge(SWITCH_PIN, GPIO.RISING)
def test_falling():
def cb(chan):
print "Callback called - channel", chan
print "Falling edge test"
print "5 second sample for event_detected function"
GPIO.add_event_detect(SWITCH_PIN, GPIO.FALLING)
time.sleep(5)
if GPIO.event_detected(SWITCH_PIN):
print "Event detected"
else:
print "Event not detected"
print "5 seconds for callback function"
input ("Press return to start: ")
GPIO.remove_event_detect(SWITCH_PIN);
GPIO.add_event_detect(SWITCH_PIN, GPIO.FALLING, callback=cb)
time.sleep(5)
GPIO.remove_event_detect(SWITCH_PIN);
def test_switchbounce():
global count
count = 0
def cb(chan):
global count
count += 1
print "Switch on channel", s, "pressed", s, chan, count
print "Switch bounce test - Ctrl-C to stop..."
GPIO.add_event_detect(SWITCH_PIN, GPIO.FALLING, callback=cb, bouncetime=200)
try:
while 1:
time.sleep(3600)
except KeyboardInterrupt:
pass
GPIO.remove_event_detect(SWITCH_PIN);
def test_gpio_function():
for chan in range(54):
f = GPIO.gpio_function(chan)
if f == GPIO.IN:
func = 'INPUT'
elif f == GPIO.OUT:
func = 'OUTPUT'
elif f == GPIO.ALT0:
func = 'ALT0'
else:
func = 'UNKNOWN'
print "chan=", s, "func=",s, chan, func
def test_warnings():
GPIO.setwarnings(False)
print "No warning should be produced vvv"
GPIO.setup(14, GPIO.OUT) # is ALT0 serial TXD by default
print "Done!"
GPIO.setwarnings(True)
print "Warning should be produced vvv"
GPIO.setup(15, GPIO.OUT) # is ALT0 serial RXD by default
print "Done!"
def test_setup():
print "Running setup tests..."
GPIO.setmode(GPIO.BCM)
GPIO.setup(7, GPIO.OUT, initial=GPIO.HIGH) # or True
if not GPIO.input(7):
print "Initial state test failed"
GPIO.setup(LED_PIN, GPIO.OUT, initial=GPIO.LOW) # or False
if GPIO.input(LED_PIN):
print "Initial state test failed"
GPIO.setup(LED_PIN, GPIO.OUT)
GPIO.setup(SWITCH_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def test_soft_pwm():
print "Running software PWM tests (ctrl-c to stop)..."
pwm = GPIO.PWM(LED_PIN, 50) # frequency - 50Hz
pwm.start(0) # duty cycle - 0 (off)
try:
while 1: # make the LED fade in and out
for x in range(0,101,5):
pwm.ChangeDutyCycle(x)
time.sleep(0.1)
for x in range(100,-1,-5):
pwm.ChangeDutyCycle(x)
time.sleep(0.1)
except KeyboardInterrupt:
pass
pwm.stop()
def test_hard_pwm():
print "Hardware PWM - not yet implemented"
# main program starts here
while 1:
print "0 - Setup"
print('1 - Output')
print('2 - Input')
print('3 - Rising edge')
print('4 - Falling edge')
print('5 - Software PWM')
print('6 - Hardware PWM')
print('7 - Switchbounce')
print('8 - gpio_function')
print('9 - Board revision')
print('10 - Test warnings')
print('11 - Version')
print('12 - eXit')
command = input ("Enter your choice: ") #.upper()
print command
print
if command==0:
test_setup()
elif command==1:
test_output()
elif command==2:
test_input()
elif command==3:
test_rising()
elif command==4:
test_falling()
elif command==5:
test_soft_pwm()
elif command==6:
test_hard_pwm()
elif command==7:
test_switchbounce()
elif command==8:
test_gpio_function()
elif command==9:
test_warnings()
elif command==10:
print "Board revision -", GPIO.RPI_REVISION
print " "
elif command==11:
print "RPi.GPIO Version",GPIO.VERSION
print " "
elif command==12:
break
GPIO.cleanup()