1
+ namespace Raspberry . IO . GeneralPurpose
2
+ {
3
+ /// <summary>
4
+ /// Represents a bidirectional pin on GPIO interface.
5
+ /// </summary>
6
+ public class GpioInputOutputBinaryPin : IInputOutputBinaryPin
7
+ {
8
+ private readonly IGpioConnectionDriver driver ;
9
+ private readonly ProcessorPin pin ;
10
+ private readonly PinResistor resistor ;
11
+ private PinDirection ? direction ;
12
+
13
+ /// <summary>
14
+ /// Initializes a new instance of the <see cref="GpioInputOutputBinaryPin"/> class.
15
+ /// </summary>
16
+ /// <param name="driver">The driver.</param>
17
+ /// <param name="pin">The pin.</param>
18
+ /// <param name="resistor">The resistor.</param>
19
+ public GpioInputOutputBinaryPin ( IGpioConnectionDriver driver , ProcessorPin pin , PinResistor resistor = PinResistor . None )
20
+ {
21
+ this . driver = driver ;
22
+ this . pin = pin ;
23
+ this . resistor = resistor ;
24
+ }
25
+
26
+ /// <summary>
27
+ /// Releases unmanaged and - optionally - managed resources.
28
+ /// </summary>
29
+ public void Dispose ( )
30
+ {
31
+ if ( direction . HasValue )
32
+ driver . Release ( pin ) ;
33
+ }
34
+
35
+ /// <summary>
36
+ /// Reads this instance.
37
+ /// </summary>
38
+ /// <returns>The value.</returns>
39
+ public bool Read ( )
40
+ {
41
+ SetDirection ( PinDirection . Input ) ;
42
+ return driver . Read ( pin ) ;
43
+ }
44
+
45
+ /// <summary>
46
+ /// Waits the specified wait for up.
47
+ /// </summary>
48
+ /// <param name="waitForUp">if set to <c>true</c> [wait for up].</param>
49
+ /// <param name="timeout">The timeout.</param>
50
+ public void Wait ( bool waitForUp = true , decimal timeout = 0 )
51
+ {
52
+ SetDirection ( PinDirection . Input ) ;
53
+ driver . Wait ( pin , waitForUp , timeout ) ;
54
+ }
55
+
56
+ /// <summary>
57
+ /// Writes the specified state.
58
+ /// </summary>
59
+ /// <param name="state">the state.</param>
60
+ public void Write ( bool state )
61
+ {
62
+ SetDirection ( PinDirection . Output ) ;
63
+ driver . Write ( pin , state ) ;
64
+ }
65
+
66
+ private void SetDirection ( PinDirection newDirection )
67
+ {
68
+ if ( direction == newDirection )
69
+ return ;
70
+
71
+ if ( direction . HasValue )
72
+ driver . Release ( pin ) ;
73
+
74
+ driver . Allocate ( pin , newDirection ) ;
75
+ if ( newDirection == PinDirection . Input && resistor != PinResistor . None )
76
+ driver . SetPinResistor ( pin , resistor ) ;
77
+
78
+ direction = newDirection ;
79
+ }
80
+
81
+ }
82
+ }
0 commit comments