Skip to content
This repository has been archived by the owner on Apr 19, 2022. It is now read-only.

Commit

Permalink
Add drivemode for alternate function (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Christophe Gerbier authored and josesimoes committed Dec 22, 2017
1 parent 9072265 commit 930e36b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
6 changes: 5 additions & 1 deletion source/GpioPinDriveMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public enum GpioPinDriveMode
/// <summary>
/// Configures the GPIO pin in open collector mode with resistive pull-down mode.
/// </summary>
OutputOpenSourcePullDown
OutputOpenSourcePullDown,
/// <summary>
/// Configures the GPIO pin for an alternate function
/// </summary>
Alternate
}
}
15 changes: 9 additions & 6 deletions source/Gpio​Pin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public sealed class Gpio​Pin : IDisposable
private object _syncLock = new object();

private readonly int _pinNumber;
private int _alternateFunction = 0;
private GpioPinDriveMode _driveMode = GpioPinDriveMode.Input;
private TimeSpan _debounceTimeout = TimeSpan.Zero;
private GpioPinValueChangedEventHandler _callbacks = null;
Expand Down Expand Up @@ -148,26 +149,28 @@ public bool IsDriveModeSupported(GpioPinDriveMode driveMode)
/// </summary>
/// <param name="value">An enumeration value that specifies drive mode to use for the GPIO pin.
/// The drive mode specifies whether the pin is configured as an input or an output, and determines how values are driven onto the pin.</param>
/// <param name="alternateFunction">An integer value that specifies the alternate function assigned to the GPIO pin.</param>
/// <remarks>The following exceptions can be thrown by this method:
/// <list type="bullet">
/// <item><term>E_INVALIDARG : The GPIO pin does not support the specified drive mode.</term></item>
/// <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 drive mode of the pin.</term></item>
/// </list>
/// </remarks>
public void SetDriveMode(GpioPinDriveMode value)
public void SetDriveMode(GpioPinDriveMode value, int alternateFunction = 0)
{
lock (_syncLock)
{
// check if pin has been disposed
if (_disposedValue) { throw new ObjectDisposedException(); }

if (_driveMode == value) return;
if (_driveMode == value && alternateFunction == _alternateFunction) return;

// check if the request drive mode is supported
// need to call the native method directly because we are already inside a lock
if (NativeIsDriveModeSupported(value))
{
NativeSetDriveMode(value);
NativeSetDriveMode(value, alternateFunction);
_alternateFunction = alternateFunction;
_driveMode = value;
}
}
Expand Down Expand Up @@ -246,7 +249,7 @@ public event GpioPinValueChangedEventHandler ValueChanged
try
{
_callbacks = callbacksNew;
NativeSetDriveMode(_driveMode);
NativeSetDriveMode(_driveMode, _alternateFunction);
}
catch
{
Expand All @@ -271,7 +274,7 @@ public event GpioPinValueChangedEventHandler ValueChanged
try
{
_callbacks = callbacksNew;
NativeSetDriveMode(_driveMode);
NativeSetDriveMode(_driveMode, _alternateFunction);
}
catch
{
Expand Down Expand Up @@ -351,7 +354,7 @@ public void Dispose()
private extern bool NativeIsDriveModeSupported(GpioPinDriveMode driveMode);

[MethodImpl(MethodImplOptions.InternalCall)]
private extern void NativeSetDriveMode(GpioPinDriveMode driveMode);
private extern void NativeSetDriveMode(GpioPinDriveMode driveMode, int alternateFunction);

[MethodImpl(MethodImplOptions.InternalCall)]
private extern bool NativeInit(int pinNumber);
Expand Down

0 comments on commit 930e36b

Please sign in to comment.