Skip to content
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
65 changes: 0 additions & 65 deletions src/System.Device.Gpio.Tests/GpioControllerSoftwareTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,71 +135,6 @@ public void CallbackOnEventWorks()
ctrl.UnregisterCallbackForPinValueChangedEvent(1, eventHandler);
}

[Fact]
public void WriteSpan()
{
_mockedGpioDriver.Setup(x => x.OpenPinEx(1));
_mockedGpioDriver.Setup(x => x.OpenPinEx(2));
_mockedGpioDriver.Setup(x => x.IsPinModeSupportedEx(1, PinMode.Output)).Returns(true);
_mockedGpioDriver.Setup(x => x.IsPinModeSupportedEx(2, PinMode.Output)).Returns(true);
_mockedGpioDriver.Setup(x => x.GetPinModeEx(1)).Returns(PinMode.Output);
_mockedGpioDriver.Setup(x => x.GetPinModeEx(2)).Returns(PinMode.Output);
_mockedGpioDriver.Setup(x => x.WriteEx(1, PinValue.High));
_mockedGpioDriver.Setup(x => x.WriteEx(2, PinValue.Low));
_mockedGpioDriver.Setup(x => x.ClosePinEx(1));
_mockedGpioDriver.Setup(x => x.ClosePinEx(2));
var ctrl = new GpioController(_mockedGpioDriver.Object);
Assert.NotNull(ctrl);
ctrl.OpenPin(1, PinMode.Output);
ctrl.OpenPin(2, PinMode.Output);
Assert.True(ctrl.IsPinOpen(1));
Span<PinValuePair> towrite = stackalloc PinValuePair[2];
towrite[0] = new PinValuePair(1, PinValue.High);
towrite[1] = new PinValuePair(2, PinValue.Low);
ctrl.Write(towrite);
ctrl.ClosePin(1);
ctrl.ClosePin(2);
Assert.False(ctrl.IsPinOpen(1));
}

[Fact]
public void ReadSpan()
{
_mockedGpioDriver.Setup(x => x.OpenPinEx(1));
_mockedGpioDriver.Setup(x => x.OpenPinEx(2));
_mockedGpioDriver.Setup(x => x.IsPinModeSupportedEx(1, PinMode.Input)).Returns(true);
_mockedGpioDriver.Setup(x => x.IsPinModeSupportedEx(2, PinMode.Input)).Returns(true);
_mockedGpioDriver.Setup(x => x.ReadEx(1)).Returns(PinValue.Low);
_mockedGpioDriver.Setup(x => x.ReadEx(2)).Returns(PinValue.High);
_mockedGpioDriver.Setup(x => x.ClosePinEx(1));
_mockedGpioDriver.Setup(x => x.ClosePinEx(2));
var ctrl = new GpioController(_mockedGpioDriver.Object);
Assert.NotNull(ctrl);
ctrl.OpenPin(1, PinMode.Input);
ctrl.OpenPin(2, PinMode.Input);
Assert.True(ctrl.IsPinOpen(1));

// Invalid usage (we need to prefill the array)
// Was this the intended use case?
Assert.Throws<InvalidOperationException>(() =>
{
Span<PinValuePair> wrongArg = stackalloc PinValuePair[2];
ctrl.Read(wrongArg);
});

Span<PinValuePair> toread = stackalloc PinValuePair[2];
toread[0] = new PinValuePair(1, PinValue.Low);
toread[1] = new PinValuePair(2, PinValue.Low);
ctrl.Read(toread);
Assert.Equal(1, toread[0].PinNumber);
Assert.Equal(2, toread[1].PinNumber);
Assert.Equal(PinValue.Low, toread[0].PinValue);
Assert.Equal(PinValue.High, toread[1].PinValue);
ctrl.ClosePin(1);
ctrl.ClosePin(2);
Assert.False(ctrl.IsPinOpen(1));
}

[Fact]
public async Task WaitForEventAsyncFail()
{
Expand Down
10 changes: 8 additions & 2 deletions src/System.Device.Gpio.Tests/GpioControllerTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ public void AddCallbackFallingEdgeNotDetectedTest()
controller.OpenPin(OutputPin, PinMode.Output);
controller.Write(OutputPin, PinValue.Low);
controller.RegisterCallbackForPinValueChangedEvent(InputPin, PinEventTypes.Falling, Callback);
// Sometimes, we get an extra event just at the beginning - wait for it and then drop it
ev.WaitOne(1000);
// Sometimes, we get an extra event just at the beginning - therefore wait a bit
Thread.Sleep(100);
wasCalled = false;
controller.Write(OutputPin, PinValue.High);
controller.UnregisterCallbackForPinValueChangedEvent(InputPin, Callback);
Expand Down Expand Up @@ -334,9 +334,15 @@ public void AddCallbackRemoveAllCallbackTest()
controller.RegisterCallbackForPinValueChangedEvent(InputPin, PinEventTypes.Falling, Callback3);
controller.RegisterCallbackForPinValueChangedEvent(InputPin, PinEventTypes.Rising, Callback4);

Thread.Sleep(100);
risingEventOccurredCount = 0;
fallingEventOccurredCount = 0;
controller.Write(OutputPin, PinValue.High);
Thread.Sleep(WaitMilliseconds);

Assert.Equal(1, risingEventOccurredCount);
Assert.Equal(0, fallingEventOccurredCount);

controller.UnregisterCallbackForPinValueChangedEvent(InputPin, Callback1);
controller.UnregisterCallbackForPinValueChangedEvent(InputPin, Callback2);
controller.UnregisterCallbackForPinValueChangedEvent(InputPin, Callback3);
Expand Down
69 changes: 69 additions & 0 deletions src/System.Device.Gpio.Tests/LibGpiodV2DriverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Device.Gpio.Drivers;
using System.Diagnostics;
using System.Threading;
using Microsoft.Extensions.Logging;
using Xunit;
using Xunit.Abstractions;

Expand All @@ -15,6 +18,11 @@ public class LibGpiodV2DriverTests : GpioControllerTestBase
{
private const int ChipNumber = 0;

/// <summary>
/// Leave this pin open (unconnected) for the tests
/// </summary>
private const int OpenPin = 23;

public LibGpiodV2DriverTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
{
Expand Down Expand Up @@ -44,10 +52,71 @@ public void CheckAllChipsCanBeConstructed()
var driverInfo = driver.GetChipInfo();
Assert.NotNull(driverInfo);
Assert.Equal(chip, driverInfo);

var otherInfo = ctrl.QueryComponentInformation();
Assert.NotNull(otherInfo);
Logger.WriteLine(otherInfo.ToString());
ctrl.Dispose();
}
}

/// <summary>
/// This uses the RPI3 driver to verify that the libgpiod driver did what it was expected to do
/// by directly reading out the hardware registers.
/// </summary>
private void VerifyStateUsingRpiDriver(int pin, PinMode expectedMode)
{
try
{
using var rpi = new GpioController(new RaspberryPi3Driver());
rpi.OpenPin(pin);
var mode = rpi.GetPinMode(pin);
Assert.Equal(expectedMode, mode);
}
catch (PlatformNotSupportedException x)
{
Logger.WriteLine($"Unable to compare with RaspberryPi driver, as not a supported board type: {x.Message}");
}
}

/// <summary>
/// Tests for setting the pull up/pull down resistors on the Raspberry Pi (supported on Pi3 and Pi4, but with different techniques)
/// </summary>
[Fact]
public void InputPullResistorsWork()
{
using (GpioController controller = new GpioController(GetTestDriver()))
{
// Verify all states
controller.OpenPin(OpenPin, PinMode.Input);
VerifyStateUsingRpiDriver(OpenPin, PinMode.Input);
controller.ClosePin(OpenPin);

controller.OpenPin(OpenPin, PinMode.InputPullUp);
Thread.Sleep(50);
VerifyStateUsingRpiDriver(OpenPin, PinMode.InputPullUp);
Assert.Equal(PinValue.High, controller.Read(OpenPin));

for (int i = 0; i < 100; i++)
{
controller.SetPinMode(OpenPin, PinMode.Input);
VerifyStateUsingRpiDriver(OpenPin, PinMode.Input);
controller.SetPinMode(OpenPin, PinMode.InputPullDown);
Thread.Sleep(10);
VerifyStateUsingRpiDriver(OpenPin, PinMode.InputPullDown);
Assert.Equal(PinValue.Low, controller.Read(OpenPin));

controller.SetPinMode(OpenPin, PinMode.InputPullUp);
Thread.Sleep(10);
Assert.Equal(PinValue.High, controller.Read(OpenPin));
}

// change one more time so that when running test in a loop we start with the inverted option
controller.SetPinMode(OpenPin, PinMode.InputPullDown);
Assert.Equal(PinValue.Low, controller.Read(OpenPin));
}
}

#pragma warning disable SDGPIO0001
protected override GpioDriver GetTestDriver() => new LibGpiodV2Driver(ChipNumber);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using static Interop;

namespace System.Device.Gpio.Drivers;

Expand All @@ -32,6 +34,8 @@ public sealed class LibGpiodV2Driver : UnixDriver
private readonly CancellationTokenSource _disposalTokenSource = new();
private readonly LibGpiodV2EventObserver _eventObserver;

private bool _isDisposed;

/// <summary>
/// Creates a driver instance for the specified GPIO chip.
/// </summary>
Expand Down Expand Up @@ -133,7 +137,8 @@ void ChangeExistingLineSettings(LineSettings lineSettings)
lineSettings.SetEdgeDetection(GpiodLineEdge.None);
}
}
else if (bias != null)

if (bias != null)
{
lineSettings.SetBias(bias.Value);
}
Expand All @@ -143,12 +148,6 @@ void ChangeExistingLineSettings(LineSettings lineSettings)
{
if (_requestedLineByLineOffset.TryGetValue(offset, out RequestedLines? requestedLines))
{
GpiodLineDirection gpiodLineDirection = requestedLines.LineConfig.GetLineSettings(offset).GetDirection();
if (gpiodLineDirection == direction)
{
return;
}

ReconfigureExistingRequest(requestedLines, offset, ChangeExistingLineSettings);
return;
}
Expand Down Expand Up @@ -474,9 +473,16 @@ public override GpioChipInfo GetChipInfo()
return new GpioChipInfo(info.ChipNumber, info.GetName(), info.GetLabel(), info.GetNumLines());
}

#region Dispose

private bool _isDisposed;
/// <inheritdoc/>
public override ComponentInformation QueryComponentInformation()
{
var self = new ComponentInformation(this, "LibGpiodDriver");
IntPtr libgpiodVersionPtr = LibgpiodV2.gpiod_api_version();
string libgpiodVersion = Marshal.PtrToStringAnsi(libgpiodVersionPtr) ?? string.Empty;
self.Properties["LibGpiod-api-version"] = libgpiodVersion;
self.Properties["ChipInfo"] = GetChipInfo().ToString();
return self;
}

/// <inheritdoc/>
protected override void Dispose(bool disposing)
Expand Down Expand Up @@ -519,7 +525,5 @@ protected override void Dispose(bool disposing)
}
}

#endregion

private sealed record RequestedLines(LineConfig LineConfig, IReadOnlyDictionary<Offset, LineSettings> SettingsByLine, LineRequest LineRequest);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ public static (GpiodLineDirection? _direction, GpiodLineBias? _bias) Translate(P
{
return pinMode switch
{
PinMode.Input => (GpiodLineDirection.Input, null),
PinMode.Input => (GpiodLineDirection.Input, GpiodLineBias.Disabled),
PinMode.Output => (GpiodLineDirection.Output, null),
PinMode.InputPullDown => (null, GpiodLineBias.PullDown),
PinMode.InputPullUp => (null, GpiodLineBias.PullUp),
PinMode.InputPullDown => (GpiodLineDirection.Input, GpiodLineBias.PullDown),
PinMode.InputPullUp => (GpiodLineDirection.Input, GpiodLineBias.PullUp),
_ => throw new ArgumentOutOfRangeException(nameof(pinMode), pinMode, null)
};
}
Expand Down
Loading