@@ -13,8 +13,9 @@ namespace Iot.Device.Button
1313 public class GpioButton : ButtonBase
1414 {
1515 private GpioController _gpioController ;
16+ private PinMode _pinMode ;
17+
1618 private int _buttonPin ;
17- private bool _pullUp ;
1819 private bool _shouldDispose ;
1920
2021 private bool _disposed = false ;
@@ -23,32 +24,40 @@ public class GpioButton : ButtonBase
2324 /// Initialization of the button.
2425 /// </summary>
2526 /// <param name="buttonPin">GPIO pin of the button.</param>
26- /// <param name="pullUp">If the system is pullup (false = pulldown) .</param>
27+ /// <param name="pinMode">Pin mode of the system .</param>
2728 /// <param name="gpio">Gpio Controller.</param>
2829 /// <param name="shouldDispose">True to dispose the GpioController.</param>
29- public GpioButton ( int buttonPin , bool pullUp = true , GpioController gpio = null , bool shouldDispose = true ) : this ( buttonPin , pullUp , TimeSpan . FromTicks ( DefaultDoublePressTicks ) , TimeSpan . FromMilliseconds ( DefaultHoldingMilliseconds ) , gpio , shouldDispose )
30+ public GpioButton ( int buttonPin , GpioController gpio = null , bool shouldDispose = true , PinMode pinMode = PinMode . InputPullUp )
31+ : this ( buttonPin , TimeSpan . FromTicks ( DefaultDoublePressTicks ) , TimeSpan . FromMilliseconds ( DefaultHoldingMilliseconds ) , gpio , shouldDispose , pinMode )
3032 {
3133 }
3234
3335 /// <summary>
3436 /// Initialization of the button.
3537 /// </summary>
3638 /// <param name="buttonPin">GPIO pin of the button.</param>
37- /// <param name="pullUp">If the system is pullup (false = pulldown) .</param>
39+ /// <param name="pinMode">Pin mode of the system .</param>
3840 /// <param name="doublePress">Max ticks between button presses to count as doublepress.</param>
3941 /// <param name="holding">Min ms a button is pressed to count as holding.</param>
4042 /// <param name="gpio">Gpio Controller.</param>
4143 /// <param name="shouldDispose">True to dispose the GpioController.</param>
42- public GpioButton ( int buttonPin , bool pullUp , TimeSpan doublePress , TimeSpan holding , GpioController gpio = null , bool shouldDispose = true )
44+ public GpioButton ( int buttonPin , TimeSpan doublePress , TimeSpan holding , GpioController gpio = null , bool shouldDispose = true , PinMode pinMode = PinMode . InputPullUp )
4345 : base ( doublePress , holding )
4446 {
4547 _gpioController = gpio ?? new GpioController ( ) ;
4648 _shouldDispose = shouldDispose ;
4749 _buttonPin = buttonPin ;
48- _pullUp = pullUp ;
50+ _pinMode = pinMode ;
4951
50- _gpioController . OpenPin ( _buttonPin , PinMode . Input ) ;
51- _gpioController . RegisterCallbackForPinValueChangedEvent ( _buttonPin , PinEventTypes . Falling | PinEventTypes . Rising , PinStateChanged ) ;
52+ if ( _pinMode == PinMode . Input | _pinMode == PinMode . InputPullDown | _pinMode == PinMode . InputPullUp )
53+ {
54+ _gpioController . OpenPin ( _buttonPin , _pinMode ) ;
55+ _gpioController . RegisterCallbackForPinValueChangedEvent ( _buttonPin , PinEventTypes . Falling | PinEventTypes . Rising , PinStateChanged ) ;
56+ }
57+ else
58+ {
59+ throw new ArgumentException ( "GPIO pin can only be set to input, not to output." ) ;
60+ }
5261 }
5362
5463 /// <summary>
@@ -61,7 +70,7 @@ private void PinStateChanged(object sender, PinValueChangedEventArgs pinValueCha
6170 switch ( pinValueChangedEventArgs . ChangeType )
6271 {
6372 case PinEventTypes . Falling :
64- if ( _pullUp )
73+ if ( _pinMode == PinMode . InputPullUp )
6574 {
6675 HandleButtonPressed ( ) ;
6776 }
@@ -72,7 +81,7 @@ private void PinStateChanged(object sender, PinValueChangedEventArgs pinValueCha
7281
7382 break ;
7483 case PinEventTypes . Rising :
75- if ( _pullUp )
84+ if ( _pinMode == PinMode . InputPullUp )
7685 {
7786 HandleButtonReleased ( ) ;
7887 }
@@ -85,14 +94,6 @@ private void PinStateChanged(object sender, PinValueChangedEventArgs pinValueCha
8594 }
8695 }
8796
88- /// <summary>
89- /// Cleanup.
90- /// </summary>
91- public new void Dispose ( )
92- {
93- Dispose ( true ) ;
94- }
95-
9697 /// <summary>
9798 /// Internal cleanup.
9899 /// </summary>
@@ -106,19 +107,19 @@ protected override void Dispose(bool disposing)
106107
107108 if ( disposing )
108109 {
110+ _gpioController . UnregisterCallbackForPinValueChangedEvent ( _buttonPin , PinStateChanged ) ;
109111 if ( _shouldDispose )
110112 {
111113 _gpioController ? . Dispose ( ) ;
112114 _gpioController = null ! ;
113115 }
114116 else
115117 {
116- _gpioController . UnregisterCallbackForPinValueChangedEvent ( _buttonPin , PinStateChanged ) ;
117118 _gpioController . ClosePin ( _buttonPin ) ;
118119 }
119120
120- base . Dispose ( disposing ) ;
121- _disposed = true ;
121+ base . Dispose ( disposing ) ;
122+ _disposed = true ;
122123 }
123124 }
124125 }
0 commit comments