-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding crosstargeting in order to be able to call WinRT APIs in net5.0 (
#1217) * Adding crosstargeting in order to be able to call WinRT APIs in net5.0 * Only build Windows-specific build when running on Windows SDK * Addressing PR Feedback * Removing accidental push changing TFMs * Removing blank spaces on Error message
- Loading branch information
Showing
16 changed files
with
298 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
namespace System.Device | ||
{ | ||
internal static class CommonHelpers | ||
{ | ||
private const string WindowsPlatformTargetingFormat = "In order to use {0} on Windows with .NET 5.0 it is required for your application to target net5.0-windows10.0.17763.0 or higher. Please add that to your target frameworks in your project file."; | ||
|
||
public static string GetFormattedWindowsPlatformTargetingErrorMessage(string className) | ||
=> string.Format(WindowsPlatformTargetingFormat, className); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
src/System.Device.Gpio/System/Device/Gpio/Drivers/Windows10Driver.notSupported.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Threading; | ||
|
||
namespace System.Device.Gpio.Drivers | ||
{ | ||
/// <summary> | ||
/// A GPIO driver for Windows 10 IoT. | ||
/// </summary> | ||
public class Windows10Driver : GpioDriver | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="Windows10Driver"/> class. | ||
/// </summary> | ||
public Windows10Driver() | ||
{ | ||
// If we land in this method it means the console application is running on Windows and targetting net5.0 (without specifying Windows platform) | ||
// In order to call WinRT code in net5.0 it is required for the application to target the specific platform | ||
// so we throw the bellow exception with a detailed message in order to instruct the consumer on how to move forward. | ||
throw new PlatformNotSupportedException(CommonHelpers.GetFormattedWindowsPlatformTargetingErrorMessage(nameof(Windows10Driver))); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected internal override int PinCount => throw new PlatformNotSupportedException(); | ||
|
||
/// <inheritdoc /> | ||
protected internal override void AddCallbackForPinValueChangedEvent(int pinNumber, PinEventTypes eventTypes, PinChangeEventHandler callback) | ||
{ | ||
throw new PlatformNotSupportedException(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected internal override void ClosePin(int pinNumber) | ||
{ | ||
throw new PlatformNotSupportedException(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected internal override int ConvertPinNumberToLogicalNumberingScheme(int pinNumber) | ||
{ | ||
throw new PlatformNotSupportedException(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected internal override PinMode GetPinMode(int pinNumber) | ||
{ | ||
throw new PlatformNotSupportedException(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected internal override bool IsPinModeSupported(int pinNumber, PinMode mode) | ||
{ | ||
throw new PlatformNotSupportedException(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected internal override void OpenPin(int pinNumber) | ||
{ | ||
throw new PlatformNotSupportedException(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected internal override PinValue Read(int pinNumber) | ||
{ | ||
throw new PlatformNotSupportedException(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected internal override void RemoveCallbackForPinValueChangedEvent(int pinNumber, PinChangeEventHandler callback) | ||
{ | ||
throw new PlatformNotSupportedException(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected internal override void SetPinMode(int pinNumber, PinMode mode) | ||
{ | ||
throw new PlatformNotSupportedException(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected internal override WaitForEventResult WaitForEvent(int pinNumber, PinEventTypes eventTypes, CancellationToken cancellationToken) | ||
{ | ||
throw new PlatformNotSupportedException(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected internal override void Write(int pinNumber, PinValue value) | ||
{ | ||
throw new PlatformNotSupportedException(); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/System.Device.Gpio/System/Device/I2c/I2cDevice.Windows.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Runtime.CompilerServices; | ||
|
||
namespace System.Device.I2c | ||
{ | ||
/// <summary> | ||
/// The communications channel to a device on an I2C bus. | ||
/// </summary> | ||
public abstract partial class I2cDevice | ||
{ | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private static I2cDevice CreateWindows10I2cDevice(I2cConnectionSettings settings) | ||
{ | ||
// This wrapper is needed to prevent Mono from loading Windows10I2cDevice | ||
// which causes all fields to be loaded - one of such fields is WinRT type which does not | ||
// exist on Linux which causes TypeLoadException. | ||
// Using NoInlining and no explicit type prevents this from happening. | ||
return new Windows10I2cDevice(settings); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/System.Device.Gpio/System/Device/I2c/I2cDevice.nonWindows.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Runtime.CompilerServices; | ||
|
||
namespace System.Device.I2c | ||
{ | ||
/// <summary> | ||
/// The communications channel to a device on an I2C bus. | ||
/// </summary> | ||
public abstract partial class I2cDevice | ||
{ | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private static I2cDevice CreateWindows10I2cDevice(I2cConnectionSettings settings) | ||
{ | ||
// If we land in this method it means the console application is running on Windows and targetting net5.0 (without specifying Windows platform) | ||
// In order to call WinRT code in net5.0 it is required for the application to target the specific platform | ||
// so we throw the bellow exception with a detailed message in order to instruct the consumer on how to move forward. | ||
throw new PlatformNotSupportedException(CommonHelpers.GetFormattedWindowsPlatformTargetingErrorMessage(nameof(I2cDevice))); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/System.Device.Gpio/System/Device/Pwm/PwmChannel.Windows.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Runtime.CompilerServices; | ||
|
||
namespace System.Device.Pwm | ||
{ | ||
/// <summary> | ||
/// Represents a single PWM channel. | ||
/// </summary> | ||
public abstract partial class PwmChannel | ||
{ | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private static PwmChannel CreateWindows10PwmChannel(int chip, int channel, int frequency, double dutyCyclePercentage) | ||
{ | ||
// This wrapper is needed to prevent Mono from loading Windows10PwmChannel | ||
// which causes all fields to be loaded - one of such fields is WinRT type which does not | ||
// exist on Linux which causes TypeLoadException. | ||
// Using NoInlining and no explicit type prevents this from happening. | ||
return new Channels.Windows10PwmChannel( | ||
chip, | ||
channel, | ||
frequency, | ||
dutyCyclePercentage); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/System.Device.Gpio/System/Device/Pwm/PwmChannel.nonWindows.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Runtime.CompilerServices; | ||
|
||
namespace System.Device.Pwm | ||
{ | ||
/// <summary> | ||
/// Represents a single PWM channel. | ||
/// </summary> | ||
public abstract partial class PwmChannel | ||
{ | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private static PwmChannel CreateWindows10PwmChannel(int chip, int channel, int frequency, double dutyCyclePercentage) | ||
{ | ||
// If we land in this method it means the console application is running on Windows and targetting net5.0 (without specifying Windows platform) | ||
// In order to call WinRT code in net5.0 it is required for the application to target the specific platform | ||
// so we throw the bellow exception with a detailed message in order to instruct the consumer on how to move forward. | ||
throw new PlatformNotSupportedException(CommonHelpers.GetFormattedWindowsPlatformTargetingErrorMessage(nameof(PwmChannel))); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/System.Device.Gpio/System/Device/Spi/SpiDevice.Windows.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Runtime.CompilerServices; | ||
|
||
namespace System.Device.Spi | ||
{ | ||
/// <summary> | ||
/// The communications channel to a device on a SPI bus. | ||
/// </summary> | ||
public abstract partial class SpiDevice | ||
{ | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private static SpiDevice CreateWindows10SpiDevice(SpiConnectionSettings settings) | ||
{ | ||
// This wrapper is needed to prevent Mono from loading Windows10SpiDevice | ||
// which causes all fields to be loaded - one of such fields is WinRT type which does not | ||
// exist on Linux which causes TypeLoadException. | ||
// Using NoInlining and no explicit type prevents this from happening. | ||
return new Windows10SpiDevice(settings); | ||
} | ||
} | ||
} |
Oops, something went wrong.