Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make System.Device.Gpio work on Mono #1095

Merged
merged 1 commit into from
Jun 1, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,24 @@ public RaspberryPi3Driver()
}
else
{
_internalDriver = new Windows10Driver();
_internalDriver = CreateWindows10GpioDriver();
_setSetRegister = (value) => throw new PlatformNotSupportedException();
_setClearRegister = (value) => throw new PlatformNotSupportedException();
_getSetRegister = () => throw new PlatformNotSupportedException();
_getClearRegister = () => throw new PlatformNotSupportedException();
}
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static GpioDriver CreateWindows10GpioDriver()
{
// This wrapper is needed to prevent Mono from loading Windows10Driver
// 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 Windows10Driver();
}

/// <inheritdoc/>
protected internal override int PinCount => 28;

Expand Down
14 changes: 13 additions & 1 deletion src/System.Device.Gpio/System/Device/I2c/I2cDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// 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>
Expand Down Expand Up @@ -67,14 +69,24 @@ public static I2cDevice Create(I2cConnectionSettings settings)
{
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
return new Windows10I2cDevice(settings);
return CreateWindows10I2cDevice(settings);
}
else
{
return new UnixI2cDevice(settings);
}
}

[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);
}

/// <inheritdoc cref="IDisposable.Dispose"/>
public void Dispose()
{
Expand Down
18 changes: 17 additions & 1 deletion src/System.Device.Gpio/System/Device/Pwm/PwmChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// 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>
Expand Down Expand Up @@ -61,7 +63,7 @@ public static PwmChannel Create(
{
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
return new Channels.Windows10PwmChannel(
return CreateWindows10PwmChannel(
chip,
channel,
frequency,
Expand All @@ -76,5 +78,19 @@ public static PwmChannel Create(
dutyCyclePercentage);
}
}

[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);
}
}
}
14 changes: 13 additions & 1 deletion src/System.Device.Gpio/System/Device/Spi/SpiDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// 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>
Expand Down Expand Up @@ -60,14 +62,24 @@ public static SpiDevice Create(SpiConnectionSettings settings)
{
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
return new Windows10SpiDevice(settings);
return CreateWindows10SpiDevice(settings);
}
else
{
return new UnixSpiDevice(settings);
}
}

[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);
}

/// <inheritdoc cref="IDisposable.Dispose"/>
public void Dispose()
{
Expand Down