-
Notifications
You must be signed in to change notification settings - Fork 262
/
test_all_apis.py
executable file
·1089 lines (926 loc) · 28.9 KB
/
test_all_apis.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# Copyright (c) 2019-2023, NVIDIA CORPORATION. All rights reserved.
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
from __future__ import print_function
import threading
import time
import warnings
import RPi.GPIO as GPIO
# If a board has PWM support, the PWM tests expect 'out_a' to be PWM-capable.
pin_datas = {
'JETSON_ORIN_NANO': {
# Pre-test configuration, if boot-time pinmux doesn't set up PWM pins:
# Set BOARD pin 15 as mux function PWM:
# busybox devmem 0x02440020 32 0x400
# Set BOARD pin 33 as mux function PWM:
# busybox devmem 0x02434040 32 0x401
# Board mode pins
'out_a': 33,
'in_a': 19,
'out_b': 11,
'in_b': 13,
'unimplemented_pins': (),
# Other pin modes:
'cvm_pin': 'GPIO09',
'tegra_soc_pin': 'GP167',
'all_pwms': (15, 33),
},
'JETSON_ORIN_NX': {
# Pre-test configuration, if boot-time pinmux doesn't set up PWM pins:
# Set BOARD pin 15 as mux function PWM:
# busybox devmem 0x02440020 32 0x400
# Set BOARD pin 33 as mux function PWM:
# busybox devmem 0x02434040 32 0x401
# Board mode pins
'out_a': 33,
'in_a': 19,
'out_b': 11,
'in_b': 13,
'unimplemented_pins': (),
# Other pin modes:
'cvm_pin': 'GPIO09',
'tegra_soc_pin': 'GP167',
'all_pwms': (15, 33),
},
'JETSON_ORIN': {
# Pre-test configuration, if boot-time pinmux doesn't set up PWM pins:
# Set BOARD pin 15 as mux function PWM:
# busybox devmem 0x02440020 32 0x400
# Set BOARD pin 18 as mux function PWM:
# busybox devmem 0x02434040 32 0x401
# Board mode pins
'out_a': 18,
'in_a': 19,
'out_b': 11,
'in_b': 13,
'unimplemented_pins': (),
# Other pin modes:
'cvm_pin': 'MCLK05',
'tegra_soc_pin': 'GP66',
'all_pwms': (15, 18),
},
'JETSON_XAVIER': {
# Pre-test configuration, if boot-time pinmux doesn't set up PWM pins:
# Set BOARD pin 18 as mux function PWM:
# busybox devmem 0x2434090 32 0x401
# Board mode pins
'out_a': 18,
'in_a': 19,
'out_b': 21,
'in_b': 22,
'unimplemented_pins': (),
# Other pin modes:
'cvm_pin': 'MCLK05',
'tegra_soc_pin': 'SOC_GPIO42',
'all_pwms': (13, 15, 18),
},
'JETSON_TX2': {
# Board mode pins
'out_a': 18,
'in_a': 19,
'out_b': 21,
'in_b': 22,
'unimplemented_pins': (26,),
# Other pin modes:
'cvm_pin': 'AUDIO_MCLK',
'tegra_soc_pin': 'AUD_MCLK',
},
'JETSON_TX1': {
# Board mode pins
'out_a': 18,
'in_a': 19,
'out_b': 21,
'in_b': 22,
'unimplemented_pins': (),
# Other pin modes:
'cvm_pin': 'AUDIO_MCLK',
'tegra_soc_pin': 'AUD_MCLK',
},
'JETSON_NANO': {
# Pre-test configuration, if boot-time pinmux doesn't set up PWM pins:
# Set BOARD pin 32 as mux function PWM (set bits 1:0 to 1 not 3):
# sudo busybox devmem 0x700031fc 32 0x45
# Set BOARD pin 32 as SFIO (clear bit 0):
# sudo busybox devmem 0x6000d504 32 0x2
# Board mode pins
'out_a': 32,
'in_a': 31,
'out_b': 29,
'in_b': 26,
'unimplemented_pins': (),
# Other pin modes:
'cvm_pin': 'GPIO9',
'tegra_soc_pin': 'AUD_MCLK',
'all_pwms': (32, 33),
},
'JETSON_NX': {
# Pre-test configuration, if boot-time pinmux doesn't set up PWM pins:
# Set BOARD pin 32 as mux function PWM (func 1):
# busybox devmem 0x2430040 32 0x401
# Set BOARD pin 33 as mux function PWM (func 2):
# busybox devmem 0x2440020 32 0x402
# Board mode pins
'out_a': 32,
'in_a': 31,
'out_b': 29,
'in_b': 26,
'unimplemented_pins': (),
# Other pin modes:
'cvm_pin': 'GPIO09',
'tegra_soc_pin': 'AUD_MCLK',
'all_pwms': (15, 32, 33),
},
'CLARA_AGX_XAVIER': {
# Pre-test configuration, if boot-time pinmux doesn't set up PWM pins:
# Set BOARD pin 18 as mux function PWM:
# busybox devmem 0x2434090 32 0x401
# Board mode pins
'out_a': 18,
'in_a': 19,
'out_b': 21,
'in_b': 22,
'unimplemented_pins': (),
# Other pin modes:
'cvm_pin': 'MCLK05',
'tegra_soc_pin': 'SOC_GPIO42',
'all_pwms': (15, 18),
},
'JETSON_TX2_NX': {
# Pre-test configuration, if boot-time pinmux doesn't set up PWM pins:
# Set BOARD pin 33 as mux function PWM (func 1):
# busybox devmem 0x0c3010a8 32 0x401
# Set BOARD pin 32 as mux function PWM (func 2):
# busybox devmem 0x0c301080 32 0x401
# Board mode pins
'out_a': 32,
'in_a': 31,
'out_b': 29,
'in_b': 26,
'unimplemented_pins': (),
# Other pin modes:
'cvm_pin': 'GPIO09',
'tegra_soc_pin': 'AUD_MCLK',
'all_pwms': (32, 33),
},
}
pin_data = pin_datas.get(GPIO.model)
# Board mode
all_board_pins = (7, 11, 12, 13, 15, 16, 18, 19, 21, 22, 23, 24, 26, 29, 31,
32, 33, 35, 36, 37, 38, 40,)
bcm_pin = 4
tests = []
def test(f):
tests.append(f)
return f
def pwmtest(f):
if pin_data.get('all_pwms', None):
tests.append(f)
return f
# Tests of:
# def setwarnings(state):
@test
def test_warnings_off():
GPIO.setwarnings(False)
with warnings.catch_warnings(record=True) as w:
# cleanup() warns if no GPIOs were set up
GPIO.cleanup()
if len(w):
raise Exception("Unexpected warning occured")
@test
def test_warnings_on():
GPIO.setwarnings(True)
with warnings.catch_warnings(record=True) as w:
# cleanup() warns if no GPIOs were set up
GPIO.cleanup()
if not len(w):
raise Exception("Expected warning did not occur")
# Tests of:
# def setmode(mode):
# def getmode():
# def setup(channels, direction, pull_up_down=PUD_OFF, initial=None):
@test
def test_setup_one_board():
GPIO.setmode(GPIO.BOARD)
assert GPIO.getmode() == GPIO.BOARD
GPIO.setup(pin_data['in_a'], GPIO.IN)
GPIO.cleanup()
assert GPIO.getmode() is None
@test
def test_setup_one_bcm():
GPIO.setmode(GPIO.BCM)
assert GPIO.getmode() == GPIO.BCM
GPIO.setup(bcm_pin, GPIO.IN)
GPIO.cleanup()
assert GPIO.getmode() is None
@test
def test_setup_one_cvm():
GPIO.setmode(GPIO.CVM)
assert GPIO.getmode() == GPIO.CVM
GPIO.setup(pin_data['cvm_pin'], GPIO.IN)
GPIO.cleanup()
assert GPIO.getmode() is None
@test
def test_setup_one_tegra_soc():
GPIO.setmode(GPIO.TEGRA_SOC)
assert GPIO.getmode() == GPIO.TEGRA_SOC
GPIO.setup(pin_data['tegra_soc_pin'], GPIO.IN)
GPIO.cleanup()
assert GPIO.getmode() is None
@test
def test_setup_twice():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin_data['out_a'], GPIO.OUT, initial=GPIO.HIGH)
GPIO.setup(pin_data['out_a'], GPIO.OUT, initial=GPIO.HIGH)
GPIO.setup(pin_data['in_a'], GPIO.IN)
GPIO.setup(pin_data['in_a'], GPIO.IN)
val = GPIO.input(pin_data['in_a'])
assert(val == GPIO.HIGH)
GPIO.cleanup()
@test
def test_setup_one_out_no_init():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin_data['out_a'], GPIO.OUT)
GPIO.cleanup()
@test
def test_setup_one_out_high():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin_data['out_a'], GPIO.OUT, initial=GPIO.HIGH)
GPIO.cleanup()
@test
def test_setup_one_out_low():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin_data['out_a'], GPIO.OUT, initial=GPIO.LOW)
GPIO.cleanup()
@test
def test_setup_many_out_no_init():
GPIO.setmode(GPIO.BOARD)
GPIO.setup((pin_data['out_a'], pin_data['out_b']), GPIO.OUT)
GPIO.cleanup()
@test
def test_setup_many_out_one_init():
GPIO.setmode(GPIO.BOARD)
GPIO.setup((pin_data['out_a'], pin_data['out_b']), GPIO.OUT,
initial=GPIO.HIGH)
GPIO.cleanup()
@test
def test_setup_many_out_many_init():
GPIO.setmode(GPIO.BOARD)
GPIO.setup((pin_data['out_a'], pin_data['out_b']), GPIO.OUT,
initial=(GPIO.HIGH, GPIO.HIGH))
GPIO.cleanup()
@test
def test_setup_one_in():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin_data['in_a'], GPIO.IN)
GPIO.cleanup()
@test
def test_setup_one_in_pull():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin_data['in_a'], GPIO.IN, GPIO.PUD_OFF)
GPIO.cleanup()
@test
def test_setup_many_in():
GPIO.setmode(GPIO.BOARD)
GPIO.setup((pin_data['in_a'], pin_data['in_b']), GPIO.IN)
GPIO.cleanup()
@test
def test_setup_all():
GPIO.setmode(GPIO.BOARD)
for pin in all_board_pins:
if pin in pin_data['unimplemented_pins']:
continue
GPIO.setup(pin, GPIO.IN)
GPIO.cleanup()
# Tests of:
# def cleanup(channel=None):
# def getmode():
@test
def test_cleanup_one():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin_data['in_a'], GPIO.IN)
GPIO.cleanup(pin_data['in_a'])
assert GPIO.getmode() == GPIO.BOARD
GPIO.cleanup()
assert GPIO.getmode() is None
@test
def test_cleanup_many():
GPIO.setmode(GPIO.BOARD)
GPIO.setup((pin_data['in_a'], pin_data['in_b']), GPIO.IN)
GPIO.cleanup((pin_data['in_a'], pin_data['in_b']))
assert GPIO.getmode() == GPIO.BOARD
GPIO.cleanup()
assert GPIO.getmode() is None
@test
def test_cleanup_all():
GPIO.setmode(GPIO.BOARD)
GPIO.setup((pin_data['in_a'], pin_data['in_b']), GPIO.IN)
GPIO.cleanup()
assert GPIO.getmode() is None
# Tests of:
# def input(channel):
@test
def test_input():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin_data['in_a'], GPIO.IN)
GPIO.input(pin_data['in_a'])
GPIO.cleanup()
# Tests of:
# def output(channels, values):
@test
def test_output_one():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin_data['out_a'], GPIO.OUT)
GPIO.output(pin_data['out_a'], GPIO.HIGH)
GPIO.output(pin_data['out_a'], GPIO.LOW)
GPIO.cleanup()
@test
def test_output_many_one_value():
GPIO.setmode(GPIO.BOARD)
GPIO.setup((pin_data['out_a'], pin_data['out_b']), GPIO.OUT)
GPIO.output((pin_data['out_a'], pin_data['out_b']), GPIO.HIGH)
GPIO.output((pin_data['out_a'], pin_data['out_b']), GPIO.LOW)
GPIO.cleanup()
@test
def test_output_many_many_value():
GPIO.setmode(GPIO.BOARD)
GPIO.setup((pin_data['out_a'], pin_data['out_b']), GPIO.OUT)
GPIO.output((pin_data['out_a'], pin_data['out_b']), (GPIO.HIGH, GPIO.LOW))
GPIO.output((pin_data['out_a'], pin_data['out_b']), (GPIO.LOW, GPIO.HIGH))
GPIO.cleanup()
# Tests of combined (looped back) output/input
@test
def test_out_in_init_high():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin_data['out_a'], GPIO.OUT, initial=GPIO.HIGH)
GPIO.setup(pin_data['in_a'], GPIO.IN)
val = GPIO.input(pin_data['in_a'])
assert(val == GPIO.HIGH)
GPIO.output(pin_data['out_a'], GPIO.LOW)
val = GPIO.input(pin_data['in_a'])
assert(val == GPIO.LOW)
GPIO.output(pin_data['out_a'], GPIO.HIGH)
val = GPIO.input(pin_data['in_a'])
assert(val == GPIO.HIGH)
GPIO.cleanup()
@test
def test_out_in_init_low():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin_data['out_a'], GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(pin_data['in_a'], GPIO.IN)
val = GPIO.input(pin_data['in_a'])
assert(val == GPIO.LOW)
GPIO.output(pin_data['out_a'], GPIO.HIGH)
val = GPIO.input(pin_data['in_a'])
assert(val == GPIO.HIGH)
GPIO.output(pin_data['out_a'], GPIO.LOW)
val = GPIO.input(pin_data['in_a'])
assert(val == GPIO.LOW)
GPIO.cleanup()
# Tests of:
# def gpio_function(channel):
@test
def test_gpio_function_unexported():
GPIO.setmode(GPIO.BOARD)
val = GPIO.gpio_function(pin_data['in_a'])
assert val == GPIO.UNKNOWN
GPIO.cleanup()
@test
def test_gpio_function_in():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin_data['in_a'], GPIO.IN)
val = GPIO.gpio_function(pin_data['in_a'])
assert val == GPIO.IN
GPIO.cleanup()
@test
def test_gpio_function_out():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin_data['out_a'], GPIO.OUT)
val = GPIO.gpio_function(pin_data['out_a'])
assert val == GPIO.OUT
GPIO.cleanup()
# Tests of:
# def wait_for_edge(channel, edge, bouncetime=None, timeout=None):
@test
def test_wait_for_edge_timeout():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin_data['out_a'], GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(pin_data['in_a'], GPIO.IN)
for i in range(3):
val = GPIO.wait_for_edge(pin_data['in_a'], GPIO.BOTH, timeout=1)
assert val is None
GPIO.cleanup()
class DelayedSetChannel(threading.Thread):
def __init__(self, channel, value, delay):
super(DelayedSetChannel, self).__init__()
self.channel = channel
self.value = value
self.delay = delay
def run(self):
time.sleep(self.delay)
GPIO.output(self.channel, self.value)
@test
def test_wait_for_edge_rising():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin_data['out_a'], GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(pin_data['in_a'], GPIO.IN)
dsc = DelayedSetChannel(pin_data['out_a'], GPIO.HIGH, 0.5)
dsc.start()
val = GPIO.wait_for_edge(pin_data['in_a'], GPIO.RISING, timeout=10)
dsc.join()
assert val == pin_data['in_a']
GPIO.cleanup()
@test
def test_wait_for_edge_falling():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin_data['out_a'], GPIO.OUT, initial=GPIO.HIGH)
GPIO.setup(pin_data['in_a'], GPIO.IN)
dsc = DelayedSetChannel(pin_data['out_a'], GPIO.LOW, 0.5)
dsc.start()
val = GPIO.wait_for_edge(pin_data['in_a'], GPIO.FALLING, timeout=10)
dsc.join()
assert val == pin_data['in_a']
GPIO.cleanup()
# Tests of:
# def add_event_detect(channel, edge, callback=None, bouncetime=None):
# def event_detected(channel):
# def add_event_callback(channel, callback):
# def remove_event_detect(channel):
def _test_events(init, edge, tests, specify_callback, use_add_callback):
global event_callback_occurred
event_callback_occurred = False
# This is set as 0.5 sec delay because default remove event time is 0.5
time.sleep(0.5)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin_data['out_a'], GPIO.OUT, initial=init)
GPIO.setup(pin_data['in_a'], GPIO.IN)
def callback(channel):
global event_callback_occurred
if channel != pin_data['in_a']:
return
event_callback_occurred = True
def get_saw_event():
global event_callback_occurred
if specify_callback or use_add_callback:
val = event_callback_occurred
event_callback_occurred = False
return val
else:
return GPIO.event_detected(pin_data['in_a'])
if specify_callback:
args = {'callback': callback, 'polltime': 0.2}
else:
args = {'polltime': 0.2}
# After every pin state change, it is suggested to leave a time for the
# pin to setup itself. Here, we are using 0.2 seconds to make sure the pin
# state is stabilize. (Same reason as the following wait time after setting
# the output pin)
time.sleep(0.2)
# By default, the poll time is also 0.2 seconds. the poll time should be set
# to a large enough number to ensure the efficiency of thread, but also small
# enough so that it can respond to the event removal as fast as possible.
GPIO.add_event_detect(pin_data['in_a'], edge, **args)
if use_add_callback:
GPIO.add_event_callback(pin_data['in_a'], callback)
assert not get_saw_event()
for output, event_expected in tests:
GPIO.output(pin_data['out_a'], output)
time.sleep(0.2)
assert get_saw_event() == event_expected
assert not get_saw_event()
# By default, the timeout for removal is also 0.5 seconds
# The removal time should always be longer than the polltime, and it is
# suggested to be two times greater. Thus, in this example, as the poll
# time is set to 0.2, the timeout must be greater than 0.4.
GPIO.remove_event_detect(pin_data['in_a'], timeout=0.5)
GPIO.cleanup()
@test
def test_event_detected_falling():
_test_events(
GPIO.HIGH,
GPIO.FALLING,
(
(GPIO.LOW, True),
(GPIO.HIGH, False),
(GPIO.LOW, True),
(GPIO.HIGH, False),
),
False,
False
)
_test_events(
GPIO.LOW,
GPIO.FALLING,
(
(GPIO.HIGH, False),
(GPIO.LOW, True),
(GPIO.HIGH, False),
(GPIO.LOW, True),
),
True,
False
)
@test
def test_event_detected_rising():
_test_events(
GPIO.HIGH,
GPIO.RISING,
(
(GPIO.LOW, False),
(GPIO.HIGH, True),
(GPIO.LOW, False),
(GPIO.HIGH, True),
),
False,
False
)
_test_events(
GPIO.LOW,
GPIO.RISING,
(
(GPIO.HIGH, True),
(GPIO.LOW, False),
(GPIO.HIGH, True),
(GPIO.LOW, False),
),
True,
False
)
@test
def test_event_detected_both():
_test_events(
GPIO.HIGH,
GPIO.BOTH,
(
(GPIO.LOW, True),
(GPIO.HIGH, True),
(GPIO.LOW, True),
(GPIO.HIGH, True),
),
False,
False
)
_test_events(
GPIO.LOW,
GPIO.BOTH,
(
(GPIO.HIGH, True),
(GPIO.LOW, True),
(GPIO.HIGH, True),
(GPIO.LOW, True),
),
False,
True
)
# Tests of multiple:
# def add_event_callback(channel, callback):
def _test_callbacks(init, edge, tests, specify_callback, use_add_callback):
global event_callback_occurred
global event_callback_occurred_2
event_callback_occurred = False
event_callback_occurred_2 = False
# This is set as 0.5 sec delay because default remove event time is 0.5
time.sleep(0.5)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin_data['out_a'], GPIO.OUT, initial=init)
GPIO.setup(pin_data['in_a'], GPIO.IN)
def callback(channel):
global event_callback_occurred
if channel != pin_data['in_a']:
return
event_callback_occurred = True
def callback2(channel):
global event_callback_occurred_2
if channel != pin_data['in_a']:
return
event_callback_occurred_2 = True
# return true if every event has been detected
def get_saw_event():
global event_callback_occurred
global event_callback_occurred_2
if specify_callback:
val = event_callback_occurred
event_callback_occurred = False
return val
elif use_add_callback:
val = event_callback_occurred and event_callback_occurred_2
event_callback_occurred = False
event_callback_occurred_2 = False
return val
else:
return GPIO.event_detected(pin_data['in_a'])
# return true if every event has not been detected
# if any one event occurs, it will return false
def not_get_saw_event():
global event_callback_occurred
global event_callback_occurred_2
if specify_callback:
val = event_callback_occurred
event_callback_occurred = False
return not val
elif use_add_callback:
val = event_callback_occurred or event_callback_occurred_2
event_callback_occurred = False
event_callback_occurred_2 = False
return not val
else:
return not GPIO.event_detected(pin_data['in_a'])
if specify_callback:
args = {'callback': callback, 'polltime': 0.2}
else:
args = {'polltime': 0.2}
time.sleep(0.2)
GPIO.add_event_detect(pin_data['in_a'], edge, **args)
if use_add_callback:
# adding double callback functions
GPIO.add_event_callback(pin_data['in_a'], callback)
GPIO.add_event_callback(pin_data['in_a'], callback2)
assert not_get_saw_event()
for output, event_expected in tests:
GPIO.output(pin_data['out_a'], output)
time.sleep(0.2)
assert get_saw_event() == event_expected
assert not_get_saw_event()
GPIO.remove_event_detect(pin_data['in_a'], timeout=0.5)
GPIO.cleanup()
@test
def test_event_callbacks():
_test_callbacks(
GPIO.HIGH,
GPIO.FALLING,
(
(GPIO.LOW, True),
(GPIO.HIGH, False),
(GPIO.LOW, True),
(GPIO.HIGH, False),
),
False,
True
)
_test_callbacks(
GPIO.LOW,
GPIO.FALLING,
(
(GPIO.HIGH, False),
(GPIO.LOW, True),
(GPIO.HIGH, False),
(GPIO.LOW, True),
),
True,
False
)
_test_callbacks(
GPIO.LOW,
GPIO.FALLING,
(
(GPIO.HIGH, False),
(GPIO.LOW, True),
(GPIO.HIGH, False),
(GPIO.LOW, True),
),
False,
True
)
# Tests of multiple events at a time:
def _test_multi_events(init_a, edge_a, tests_a, init_b, edge_b, tests_b, specify_callback):
event_dict = { 'series_a': {'in_pin_name': 'in_a',
'out_pin_name': 'out_a',
'event_callback_occurred': False,
'init': init_a,
'edge': edge_a,
'tests': tests_a},
'series_b': {'in_pin_name': 'in_b',
'out_pin_name': 'out_b',
'event_callback_occurred': False,
'init': init_b,
'edge': edge_b,
'tests': tests_b}}
test_number = len(tests_a)
# internal functions
def callback(channel):
for pin_series in event_dict.keys():
input_pin_name = event_dict[pin_series]['in_pin_name']
if channel == pin_data[input_pin_name]:
event_dict[pin_series]['event_callback_occurred'] = True
return
def get_saw_event(series_name):
if specify_callback:
val = event_dict[series_name]['event_callback_occurred']
event_dict[series_name]['event_callback_occurred'] = False
return val
else:
pin_name = event_dict[series_name]['in_pin_name']
return GPIO.event_detected(pin_data[pin_name])
# setup
time.sleep(0.5)
GPIO.setmode(GPIO.BOARD)
for pin_series in event_dict.keys():
input_pin_name = event_dict[pin_series]['in_pin_name']
output_pin_name = event_dict[pin_series]['out_pin_name']
GPIO.setup(pin_data[output_pin_name], GPIO.OUT, initial=event_dict[pin_series]['init'])
GPIO.setup(pin_data[input_pin_name], GPIO.IN)
if specify_callback:
args = {'callback': callback, 'polltime': 0.2}
else:
args = {'polltime': 0.2}
time.sleep(0.2)
GPIO.add_event_detect(pin_data[input_pin_name], event_dict[pin_series]['edge'], **args)
assert not get_saw_event(pin_series)
# test edges
index=0
while index < test_number:
for pin_series in event_dict.keys():
output_pin_name = event_dict[pin_series]['out_pin_name']
input_pin_name = event_dict[pin_series]['in_pin_name']
pin_tests = event_dict[pin_series]['tests']
output, event_expected = pin_tests[index]
GPIO.output(pin_data[output_pin_name], output)
time.sleep(0.2)
assert get_saw_event(pin_series) == event_expected
assert not get_saw_event(pin_series)
index += 1
# cleanup
for pin_series in event_dict.keys():
input_pin_name = event_dict[pin_series]['in_pin_name']
GPIO.remove_event_detect(pin_data[input_pin_name], timeout=0.5)
GPIO.cleanup()
@test
def test_multi_events_detected_diff_edge():
_test_multi_events(
# series a
GPIO.HIGH,
GPIO.FALLING,
(
(GPIO.LOW, True),
(GPIO.HIGH, False),
(GPIO.LOW, True),
(GPIO.HIGH, False),
),
# series b
GPIO.HIGH,
GPIO.RISING,
(
(GPIO.LOW, False),
(GPIO.HIGH, True),
(GPIO.LOW, False),
(GPIO.HIGH, True),
),
False
)
_test_multi_events(
GPIO.LOW,
GPIO.FALLING,
(
(GPIO.HIGH, False),
(GPIO.LOW, True),
(GPIO.HIGH, False),
(GPIO.LOW, True),
),
GPIO.LOW,
GPIO.RISING,
(
(GPIO.HIGH, True),
(GPIO.LOW, False),
(GPIO.HIGH, True),
(GPIO.LOW, False),
),
True
)
@test
def test_multi_events_detected_same_edge():
_test_multi_events(
# series a
GPIO.HIGH,
GPIO.FALLING,
(
(GPIO.LOW, True),
(GPIO.HIGH, False),
(GPIO.LOW, True),
(GPIO.HIGH, False),
),
# series b
GPIO.HIGH,
GPIO.FALLING,
(
(GPIO.LOW, True),
(GPIO.HIGH, False),
(GPIO.LOW, True),
(GPIO.HIGH, False),
),
False
)
_test_multi_events(
GPIO.LOW,
GPIO.FALLING,
(
(GPIO.HIGH, False),
(GPIO.LOW, True),
(GPIO.HIGH, False),
(GPIO.LOW, True),
),
GPIO.HIGH,
GPIO.FALLING,
(
(GPIO.LOW, True),
(GPIO.HIGH, False),
(GPIO.LOW, True),
(GPIO.HIGH, False),
),
True
)
# Tests of class PWM
@pwmtest
def test_pwm_multi_duty():
for pct in (25, 50, 75):
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin_data['in_a'], GPIO.IN)
GPIO.setup(pin_data['out_a'], GPIO.OUT, initial=GPIO.HIGH)
p = GPIO.PWM(pin_data['out_a'], 500)