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

Setting Gpio alternate funcion is now an extension #8

Merged
merged 1 commit into from
Feb 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions source/Extensions/Gpio​PinExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// Copyright (c) 2017 The nanoFramework project contributors
// See LICENSE file in the project root for full license information.
//

using System;
using System.Runtime.CompilerServices;

namespace Windows.Devices.Gpio
{
/// <summary>
/// nanoFramework extensions for <see cref="GpioPin"/>.
/// </summary>
public static class Gpio​PinExtensions
{
/// <summary>
/// Sets the pin to the specified alternate function.
/// </summary>
/// <param name="pin"></param>
/// <param name="alternateFunction">The value of the alternate function.</param>
/// <remarks>
/// This extension is exclusive of nanoFramework and it may not be supported in all platforms.
/// WARNING: Use with caution! There is no validation on the execution of this call and there is the potential for breaking things,
/// so be sure to know what you are doing when using it.
/// Platforms supporting this feature: Cortex-M and ESP32.
/// Platforms not supporting this feature: none.
/// </remarks>
public static void SetAlternateFunction(this Gpio​Pin pin, int alternateFunction)
{
pin.NativeSetAlternateFunction(alternateFunction);
}
}
}
6 changes: 1 addition & 5 deletions source/GpioPinDriveMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ public enum GpioPinDriveMode
/// <summary>
/// Configures the GPIO pin in open collector mode with resistive pull-down mode.
/// </summary>
OutputOpenSourcePullDown,
/// <summary>
/// Configures the GPIO pin for an alternate function
/// </summary>
Alternate
OutputOpenSourcePullDown
}
}
19 changes: 9 additions & 10 deletions source/Gpio​Pin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ 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 @@ -156,21 +155,18 @@ public bool IsDriveModeSupported(GpioPinDriveMode driveMode)
/// <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, int alternateFunction = 0)
public void SetDriveMode(GpioPinDriveMode value)
{
lock (_syncLock)
{
// check if pin has been disposed
if (_disposedValue) { throw new ObjectDisposedException(); }

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, alternateFunction);
_alternateFunction = alternateFunction;
NativeSetDriveMode(value);
_driveMode = value;
}
}
Expand Down Expand Up @@ -249,7 +245,7 @@ public event GpioPinValueChangedEventHandler ValueChanged
try
{
_callbacks = callbacksNew;
NativeSetDriveMode(_driveMode, _alternateFunction);
NativeSetDriveMode(_driveMode);
}
catch
{
Expand All @@ -274,7 +270,7 @@ public event GpioPinValueChangedEventHandler ValueChanged
try
{
_callbacks = callbacksNew;
NativeSetDriveMode(_driveMode, _alternateFunction);
NativeSetDriveMode(_driveMode);
}
catch
{
Expand Down Expand Up @@ -348,13 +344,13 @@ public void Dispose()

#endregion

#region extenal calls to native implementations
#region external calls to native implementations

[MethodImpl(MethodImplOptions.InternalCall)]
private extern bool NativeIsDriveModeSupported(GpioPinDriveMode driveMode);

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

[MethodImpl(MethodImplOptions.InternalCall)]
private extern bool NativeInit(int pinNumber);
Expand All @@ -365,6 +361,9 @@ public void Dispose()
[MethodImpl(MethodImplOptions.InternalCall)]
private extern void WriteNative(GpioPinValue value);

[MethodImpl(MethodImplOptions.InternalCall)]
internal extern void NativeSetAlternateFunction(int alternateFunction);

#endregion
}
}
1 change: 1 addition & 0 deletions source/Windows.Devices.Gpio.nfproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
</ItemGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
<ItemGroup>
<Compile Include="Extensions\Gpio​PinExtensions.cs" />
<Compile Include="GpioOpenStatus.cs" />
<Compile Include="GpioPinDriveMode.cs" />
<Compile Include="GpioPinEdge.cs" />
Expand Down