未验证 提交 293ce364 编写于 作者: J John Tasler 提交者: GitHub

Implement the Windows I2C device (#46)

* Implement the Windows I2C device
上级 929287df
// 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.I2c
{
public class Windows10I2cDevice : I2cDevice
{
public Windows10I2cDevice(I2cConnectionSettings settings) =>
throw new PlatformNotSupportedException($"The {GetType().Name} class is not available on Linux.");
public override I2cConnectionSettings ConnectionSettings => throw new PlatformNotSupportedException();
public override byte ReadByte() => throw new PlatformNotSupportedException();
public override void Read(Span<byte> buffer) => throw new PlatformNotSupportedException();
public override void WriteByte(byte data) => throw new PlatformNotSupportedException();
public override void Write(Span<byte> data) => throw new PlatformNotSupportedException();
}
}
// 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 Windows.Devices.Enumeration;
using WinI2c = Windows.Devices.I2c;
namespace System.Device.I2c
{
public class Windows10I2cDevice : I2cDevice
{
private readonly I2cConnectionSettings _settings;
private WinI2c.I2cDevice _winDevice;
public Windows10I2cDevice(I2cConnectionSettings settings)
{
_settings = settings;
var winSettings = new WinI2c.I2cConnectionSettings(settings.DeviceAddress);
string deviceSelector = WinI2c.I2cDevice.GetDeviceSelector($"I2C{settings.BusId}");
DeviceInformationCollection deviceInformationCollection = DeviceInformation.FindAllAsync(deviceSelector).GetResults();
_winDevice = WinI2c.I2cDevice.FromIdAsync(deviceInformationCollection[0].Id, winSettings).GetResults();
}
public override I2cConnectionSettings ConnectionSettings => _settings;
public override byte ReadByte()
{
byte[] buffer = new byte[1];
_winDevice.Read(buffer);
return buffer[0];
}
public override void Read(Span<byte> buffer)
{
byte[] byteArray = new byte[buffer.Length];
_winDevice.Read(byteArray);
new Span<byte>(byteArray).CopyTo(buffer);
}
public override void WriteByte(byte data)
{
_winDevice.Write(new[] { data });
}
public override void Write(Span<byte> data)
{
_winDevice.Write(data.ToArray());
}
public override void Dispose(bool disposing)
{
_winDevice?.Dispose();
_winDevice = null;
base.Dispose(disposing);
}
}
}
......@@ -6,7 +6,8 @@ namespace System.Device.Spi
{
public class Windows10SpiDevice : SpiDevice
{
public Windows10SpiDevice(SpiConnectionSettings settings) => throw new PlatformNotSupportedException();
public Windows10SpiDevice(SpiConnectionSettings settings) =>
throw new PlatformNotSupportedException($"The {GetType().Name} class is not available on Linux.");
public override SpiConnectionSettings ConnectionSettings => throw new PlatformNotSupportedException();
......
......@@ -9,7 +9,7 @@ namespace System.Device.Spi
{
public class Windows10SpiDevice : SpiDevice
{
private SpiConnectionSettings _settings;
private readonly SpiConnectionSettings _settings;
private WinSpi.SpiDevice _winDevice;
public Windows10SpiDevice(SpiConnectionSettings settings)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册