From 9ccef91fe1e57f7fb666f0882c544b738b634a7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Tue, 6 Feb 2018 09:07:40 +0000 Subject: [PATCH] Setting Gpio alternate funcion is now an extension MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Extracted this from SetDriveMode to an extension - Remove Alternate from GpioPinDriveMode enum - Fixes nanoframework/Home#282 - Requires image v0.1.0-preview460 Signed-off-by: José Simões --- .../Gpio\342\200\213PinExtensions.cs" | 33 +++++++++++++++++++ source/GpioPinDriveMode.cs | 6 +--- "source/Gpio\342\200\213Pin.cs" | 19 +++++------ source/Windows.Devices.Gpio.nfproj | 1 + 4 files changed, 44 insertions(+), 15 deletions(-) create mode 100644 "source/Extensions/Gpio\342\200\213PinExtensions.cs" diff --git "a/source/Extensions/Gpio\342\200\213PinExtensions.cs" "b/source/Extensions/Gpio\342\200\213PinExtensions.cs" new file mode 100644 index 0000000..09f9fbd --- /dev/null +++ "b/source/Extensions/Gpio\342\200\213PinExtensions.cs" @@ -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 +{ + /// + /// nanoFramework extensions for . + /// + public static class Gpio​PinExtensions + { + /// + /// Sets the pin to the specified alternate function. + /// + /// + /// The value of the alternate function. + /// + /// 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. + /// + public static void SetAlternateFunction(this Gpio​Pin pin, int alternateFunction) + { + pin.NativeSetAlternateFunction(alternateFunction); + } + } +} diff --git a/source/GpioPinDriveMode.cs b/source/GpioPinDriveMode.cs index 5d1ecd0..a64583b 100644 --- a/source/GpioPinDriveMode.cs +++ b/source/GpioPinDriveMode.cs @@ -41,10 +41,6 @@ public enum GpioPinDriveMode /// /// Configures the GPIO pin in open collector mode with resistive pull-down mode. /// - OutputOpenSourcePullDown, - /// - /// Configures the GPIO pin for an alternate function - /// - Alternate + OutputOpenSourcePullDown } } diff --git "a/source/Gpio\342\200\213Pin.cs" "b/source/Gpio\342\200\213Pin.cs" index d6f6562..06a35ed 100644 --- "a/source/Gpio\342\200\213Pin.cs" +++ "b/source/Gpio\342\200\213Pin.cs" @@ -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; @@ -156,21 +155,18 @@ public bool IsDriveModeSupported(GpioPinDriveMode driveMode) /// 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. /// /// - 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; } } @@ -249,7 +245,7 @@ public event GpioPinValueChangedEventHandler ValueChanged try { _callbacks = callbacksNew; - NativeSetDriveMode(_driveMode, _alternateFunction); + NativeSetDriveMode(_driveMode); } catch { @@ -274,7 +270,7 @@ public event GpioPinValueChangedEventHandler ValueChanged try { _callbacks = callbacksNew; - NativeSetDriveMode(_driveMode, _alternateFunction); + NativeSetDriveMode(_driveMode); } catch { @@ -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); @@ -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 } } diff --git a/source/Windows.Devices.Gpio.nfproj b/source/Windows.Devices.Gpio.nfproj index bb2a524..46e3b0c 100644 --- a/source/Windows.Devices.Gpio.nfproj +++ b/source/Windows.Devices.Gpio.nfproj @@ -54,6 +54,7 @@ +