Skip to content

Commit e46099c

Browse files
authored
Improvements in GpioPin (#7)
1 parent 8a317c0 commit e46099c

File tree

3 files changed

+17
-29
lines changed

3 files changed

+17
-29
lines changed

System.Device.Gpio/GpioController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public GpioPin OpenPin(
8181
{
8282
var gpioPin = InternalOpenPin(pinNumber);
8383

84-
SetPinMode(pinNumber, mode);
84+
gpioPin.SetPinMode(mode);
8585

8686
// add to array
8787
s_GpioPins.Add(new GpioPinBundle() { PinNumber = pinNumber, GpioPin = gpioPin });

System.Device.Gpio/GpioPin.cs

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ public sealed class Gpio​Pin : IDisposable
2626

2727
private readonly int _pinNumber;
2828

29-
private PinMode _pinMode = PinMode.Input;
29+
private readonly PinMode _pinMode = PinMode.Input;
3030
private TimeSpan _debounceTimeout = TimeSpan.Zero;
3131
private PinValueChangedEventHandler _callbacks = null;
3232
private PinValue _lastOutputValue = PinValue.Low;
3333

34-
#pragma warning disable 0414
34+
#pragma warning disable 0414
3535
// this field is used in native so it must be kept here despite "not being used"
3636
private PinValue _lastInputValue = PinValue.Low;
37-
#pragma warning restore 0414
37+
#pragma warning restore 0414
3838

3939
internal GpioPin(int pinNumber)
4040
{
@@ -43,7 +43,7 @@ public sealed class Gpio​Pin : IDisposable
4343

4444
internal bool Init()
4545
{
46-
if(NativeInit(_pinNumber))
46+
if (NativeInit(_pinNumber))
4747
{
4848
// add the pin to the event listener in order to receive the callbacks from the native interrupts
4949
s_gpioPinEventManager.AddPin(this);
@@ -82,7 +82,7 @@ public TimeSpan DebounceTimeout
8282
/// <value>
8383
/// The pin number of the GPIO pin.
8484
/// </value>
85-
public int PinNumber
85+
public int PinNumber
8686
{
8787
get
8888
{
@@ -112,9 +112,8 @@ public PinMode GetPinMode()
112112
/// <param name="pinMode">The pin mode that you want to check for support.</param>
113113
/// <returns>
114114
/// <see langword="true"/> if the GPIO pin supports the pin mode that pinMode specifies; otherwise false.
115-
/// If you specify a pin mode for which this method returns false when you call <see cref="SetPinMode"/>, <see cref="SetPinMode"/> generates an exception.
115+
/// If you specify a pin mode for which this method returns <see langword="false"/> when you call <see cref="SetPinMode"/>, <see cref="SetPinMode"/> generates an exception.
116116
/// </returns>
117-
118117
public bool IsPinModeSupported(PinMode pinMode)
119118
{
120119
lock (_syncLock)
@@ -132,27 +131,20 @@ public bool IsPinModeSupported(PinMode pinMode)
132131
/// </summary>
133132
/// <param name="value">An enumeration value that specifies pin mode to use for the GPIO pin.
134133
/// The pin mode specifies whether the pin is configured as an input or an output, and determines how values are driven onto the pin.</param>
135-
/// <remarks>The following exceptions can be thrown by this method:
136-
/// <list type="bullet">
137-
/// <item><term>E_INVALIDARG : The GPIO pin does not support the specified pin mode.</term></item>
138-
/// <item><term>E_ACCESSDENIED : The pin is open in shared read-only mode. Close the pin and reopen it in exclusive mode to change the pin mode of the pin.</term></item>
139-
/// </list>
140-
/// </remarks>
134+
/// <exception cref="ArgumentException">The GPIO pin does not support the specified pin mode.</exception>
141135
public void SetPinMode(PinMode value)
142136
{
143137
lock (_syncLock)
144138
{
145139
// check if pin has been disposed
146140
if (_disposedValue) { throw new ObjectDisposedException(); }
147141

148-
// check if the request pin mode is supported
149-
// need to call the native method directly because we are already inside a lock
150-
if (NativeIsPinModeSupported(value))
151-
{
152-
NativeSetPinMode(value);
153-
_pinMode = value;
154-
}
155-
}
142+
// the native call takes care of:
143+
// 1) validating if the requested pin mode is supported
144+
// 2) throwing ArgumentException otherwise
145+
// 3) store the requested pin mode in _pinMode field
146+
NativeSetPinMode(value);
147+
}
156148
}
157149

158150
/// <summary>
@@ -170,11 +162,7 @@ public void SetPinMode(PinMode value)
170162
/// <para>If the GPIO pin is configured as an output, the method drives the specified value onto the pin according to the current pin mode for the pin.</para>
171163
/// <para>If the GPIO pin is configured as an input, the method updates the latched output value for the pin. The latched output value is driven onto the pin when the configuration for the pin changes to output.</para>
172164
/// </param>
173-
/// <remarks>The following exceptions can be thrown by this method:
174-
/// <list type="bullet">
175-
/// <item><term>E_ACCESSDENIED : The GPIO pin is open in shared read-only mode. To write to the pin, close the pin and reopen the pin in exclusive mode.</term></item>
176-
/// </list>
177-
/// </remarks>
165+
/// <exception cref="InvalidOperationException">This exception will be thrown on an attempt to write to a pin that hasn't been opened or is not configured as output.</exception>
178166
public void Write(PinValue value)
179167
{
180168
lock (_syncLock)
@@ -311,7 +299,7 @@ private void Dispose(bool disposing)
311299
[MethodImpl(MethodImplOptions.InternalCall)]
312300
private extern void DisposeNative();
313301

314-
#pragma warning disable 1591
302+
#pragma warning disable 1591
315303
~Gpio​Pin()
316304
{
317305
Dispose(false);

System.Device.Gpio/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
////////////////////////////////////////////////////////////////
1414
// update this whenever the native assembly signature changes //
15-
[assembly: AssemblyNativeVersion("100.1.0.2")]
15+
[assembly: AssemblyNativeVersion("100.1.0.3")]
1616
////////////////////////////////////////////////////////////////
1717

1818
// Setting ComVisible to false makes the types in this assembly not visible

0 commit comments

Comments
 (0)