1
+ // Licensed to the .NET Foundation under one or more agreements.
2
+ // The .NET Foundation licenses this file to you under the MIT license.
3
+ // See the LICENSE file in the project root for more information.
4
+
5
+ using System . Diagnostics ;
6
+ using System . Threading ;
7
+ using Xunit ;
8
+ [ assembly: CollectionBehavior ( DisableTestParallelization = true ) ]
9
+
10
+ namespace System . Device . Gpio . Tests
11
+ {
12
+ public abstract class GpioControllerTestBase
13
+ {
14
+ private const int LedPin = 18 ;
15
+ private const int OutputPin = 16 ;
16
+ private const int InputPin = 12 ;
17
+
18
+ [ Fact ]
19
+ public void ControllerCanTurnOnLEDs ( )
20
+ {
21
+ using ( GpioController controller = new GpioController ( GetTestNumberingScheme ( ) , GetTestDriver ( ) ) )
22
+ {
23
+ controller . OpenPin ( LedPin , PinMode . Output ) ;
24
+ Thread . Sleep ( 1_000 ) ;
25
+ controller . Write ( LedPin , PinValue . High ) ;
26
+ Thread . Sleep ( TimeSpan . FromSeconds ( 1 ) ) ;
27
+ controller . Write ( LedPin , PinValue . Low ) ;
28
+ }
29
+ }
30
+
31
+ [ Fact ]
32
+ public void PinValueReturnsToLowAfterDispose ( )
33
+ {
34
+ using ( GpioController controller = new GpioController ( GetTestNumberingScheme ( ) , GetTestDriver ( ) ) )
35
+ {
36
+ controller . OpenPin ( OutputPin , PinMode . Output ) ;
37
+ controller . OpenPin ( InputPin , PinMode . Input ) ;
38
+ controller . Write ( OutputPin , PinValue . High ) ;
39
+ Thread . SpinWait ( 100 ) ;
40
+ Assert . Equal ( PinValue . High , controller . Read ( InputPin ) ) ;
41
+ }
42
+
43
+ using ( GpioController controller = new GpioController ( GetTestNumberingScheme ( ) , GetTestDriver ( ) ) )
44
+ {
45
+ controller . OpenPin ( OutputPin , PinMode . Output ) ;
46
+ controller . OpenPin ( InputPin , PinMode . Input ) ;
47
+ Assert . Equal ( PinValue . Low , controller . Read ( InputPin ) ) ;
48
+ }
49
+ }
50
+
51
+ [ Fact ]
52
+ public void PinValueReadAndWrite ( )
53
+ {
54
+ using ( GpioController controller = new GpioController ( GetTestNumberingScheme ( ) , GetTestDriver ( ) ) )
55
+ {
56
+ controller . OpenPin ( OutputPin , PinMode . Output ) ;
57
+ controller . OpenPin ( InputPin , PinMode . Input ) ;
58
+ controller . Write ( OutputPin , PinValue . High ) ;
59
+ Thread . SpinWait ( 100 ) ;
60
+ Assert . Equal ( PinValue . High , controller . Read ( InputPin ) ) ;
61
+ controller . Write ( OutputPin , PinValue . Low ) ;
62
+ Thread . SpinWait ( 100 ) ;
63
+ Assert . Equal ( PinValue . Low , controller . Read ( InputPin ) ) ;
64
+ controller . Write ( OutputPin , PinValue . High ) ;
65
+ Thread . SpinWait ( 100 ) ;
66
+ Assert . Equal ( PinValue . High , controller . Read ( InputPin ) ) ;
67
+ }
68
+ }
69
+
70
+ [ Fact ]
71
+ public void ThrowsInvalidOperationExceptionWhenPinIsNotOpened ( )
72
+ {
73
+ using ( GpioController controller = new GpioController ( GetTestNumberingScheme ( ) , GetTestDriver ( ) ) )
74
+ {
75
+ Assert . Throws < InvalidOperationException > ( ( ) => controller . Write ( OutputPin , PinValue . High ) ) ;
76
+ Assert . Throws < InvalidOperationException > ( ( ) => controller . Read ( InputPin ) ) ;
77
+ Assert . Throws < InvalidOperationException > ( ( ) => controller . ClosePin ( OutputPin ) ) ;
78
+ Assert . Throws < InvalidOperationException > ( ( ) => controller . SetPinMode ( OutputPin , PinMode . Output ) ) ;
79
+ Assert . Throws < InvalidOperationException > ( ( ) => controller . GetPinMode ( OutputPin ) ) ;
80
+ }
81
+ }
82
+
83
+ [ Fact ]
84
+ public void IsPinOpenTest ( )
85
+ {
86
+ using ( GpioController controller = new GpioController ( GetTestNumberingScheme ( ) , GetTestDriver ( ) ) )
87
+ {
88
+ Assert . False ( controller . IsPinOpen ( LedPin ) ) ;
89
+ controller . OpenPin ( LedPin ) ;
90
+ Assert . True ( controller . IsPinOpen ( LedPin ) ) ;
91
+ controller . ClosePin ( LedPin ) ;
92
+ Assert . False ( controller . IsPinOpen ( LedPin ) ) ;
93
+ }
94
+ }
95
+
96
+ [ Fact ]
97
+ public void ThrowsIfWritingOnInputPin ( )
98
+ {
99
+ using ( GpioController controller = new GpioController ( GetTestNumberingScheme ( ) , GetTestDriver ( ) ) )
100
+ {
101
+ controller . OpenPin ( InputPin , PinMode . Input ) ;
102
+ Assert . Throws < InvalidOperationException > ( ( ) => controller . Write ( InputPin , PinValue . High ) ) ;
103
+ }
104
+ }
105
+
106
+ [ Fact ]
107
+ public void ThrowsIfReadingFromOutputPin ( )
108
+ {
109
+ using ( GpioController controller = new GpioController ( GetTestNumberingScheme ( ) , GetTestDriver ( ) ) )
110
+ {
111
+ controller . OpenPin ( OutputPin , PinMode . Output ) ;
112
+ Assert . Throws < InvalidOperationException > ( ( ) => controller . Read ( OutputPin ) ) ;
113
+ }
114
+ }
115
+
116
+ [ Fact ]
117
+ public void OpenPinDefaultsModeToInput ( )
118
+ {
119
+ using ( GpioController controller = new GpioController ( GetTestNumberingScheme ( ) , GetTestDriver ( ) ) )
120
+ {
121
+ controller . OpenPin ( OutputPin ) ;
122
+ Assert . Equal ( PinMode . Input , controller . GetPinMode ( OutputPin ) ) ;
123
+ controller . SetPinMode ( OutputPin , PinMode . Output ) ;
124
+ controller . ClosePin ( OutputPin ) ;
125
+ controller . OpenPin ( OutputPin ) ;
126
+ Assert . Equal ( PinMode . Input , controller . GetPinMode ( OutputPin ) ) ;
127
+ }
128
+ }
129
+
130
+ [ Fact ]
131
+ public void AddCallbackTest ( )
132
+ {
133
+ while ( ! Debugger . IsAttached )
134
+ Thread . Sleep ( 1000 ) ;
135
+ Debugger . Break ( ) ;
136
+
137
+ ManualResetEvent mre = new ManualResetEvent ( false ) ;
138
+ bool wasCalled = false ;
139
+ using ( GpioController controller = new GpioController ( GetTestNumberingScheme ( ) , GetTestDriver ( ) ) )
140
+ {
141
+ controller . OpenPin ( InputPin , PinMode . Input ) ;
142
+ controller . OpenPin ( OutputPin , PinMode . Output ) ;
143
+ controller . RegisterCallbackForPinValueChangedEvent ( InputPin , PinEventTypes . Rising , callback ) ;
144
+ controller . Write ( OutputPin , PinValue . High ) ;
145
+ mre . WaitOne ( TimeSpan . FromSeconds ( 5 ) ) ;
146
+ Assert . True ( wasCalled ) ;
147
+ }
148
+
149
+ void callback ( object sender , PinValueChangedEventArgs pinValueChangedEventArgs )
150
+ {
151
+ wasCalled = true ;
152
+ mre . Set ( ) ;
153
+ }
154
+ }
155
+
156
+ protected abstract GpioDriver GetTestDriver ( ) ;
157
+ protected abstract PinNumberingScheme GetTestNumberingScheme ( ) ;
158
+ }
159
+ }
0 commit comments