Skip to content

Commit b403104

Browse files
authored
Aligning button implementation with .NET IoT (#178)
1 parent 0811e76 commit b403104

File tree

3 files changed

+27
-26
lines changed

3 files changed

+27
-26
lines changed

devices/Button/Button.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ VisualStudioVersion = 16.0.31702.278
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "Button", "Button.nfproj", "{954E94A7-75BA-4F8E-876F-E9C90BDCBEFE}"
77
EndProject
8-
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "Samples", "Sample\Samples.nfproj", "{0B4708C9-EBFE-4B43-A79A-A9F6E7D7C486}"
8+
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "Samples", "samples\Samples.nfproj", "{0B4708C9-EBFE-4B43-A79A-A9F6E7D7C486}"
99
EndProject
1010
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "Tests", "Tests\Tests.nfproj", "{83306802-B317-4851-9673-8CFBA215F749}"
1111
EndProject

devices/Button/ButtonHoldingState.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
namespace Iot.Device.Button
55
{
66
/// <summary>
7-
/// The different button holding states.
7+
/// The different states of a button that is being held.
88
/// </summary>
99
public enum ButtonHoldingState
1010
{
11-
/// <summary>Started</summary>
11+
/// <summary>Button holding started.</summary>
1212
Started,
1313

14-
/// <summary>Completed</summary>
14+
/// <summary>Button holding completed.</summary>
1515
Completed,
1616

17-
/// <summary>Canceled</summary>
17+
/// <summary>Button holding cancelled.</summary>
1818
Canceled,
1919
}
2020
}

devices/Button/GpioButton.cs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)