Skip to content

Commit 83c3b17

Browse files
committed
I2C seems to be working correctly :)
1 parent 5715e58 commit 83c3b17

File tree

4 files changed

+35
-57
lines changed

4 files changed

+35
-57
lines changed
Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Management.Automation; // PowerShell namespace.
3+
using System.Device.I2c;
34

45
[Cmdlet(VerbsCommon.Get, "I2CDevice")]
56
public class GetI2CDevice : Cmdlet
@@ -18,17 +19,9 @@ public GetI2CDevice()
1819

1920
protected override void ProcessRecord()
2021
{
21-
try
22-
{
23-
WriteObject(new I2CDevice(Unosquare.RaspberryIO.Pi.I2C.AddDevice(this.Id), this.Id, this.FriendlyName));
24-
}
25-
catch (System.TypeInitializationException e) // Unosquare.RaspberryIO.Gpio.GpioController.Initialize throws this TypeInitializationException
26-
{
27-
if (!Unosquare.RaspberryIO.Computer.SystemInfo.Instance.IsRunningAsRoot)
28-
{
29-
throw new PlatformNotSupportedException(Resources.ErrNeedRootPrivileges, e);
30-
}
31-
throw;
32-
}
22+
var settings = new I2cConnectionSettings(1, this.Id);
23+
I2cDevice device = I2cDevice.Create(settings);
24+
WriteObject(new I2CDevice(device, this.Id, this.FriendlyName));
25+
3326
}
3427
}

src/Microsoft.PowerShell.IoT/I2c/Get/GetI2cRegister.cs

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,44 +23,40 @@ public GetI2CRegister()
2323

2424
protected override void ProcessRecord()
2525
{
26-
try
26+
if (this.ByteCount > 1)
2727
{
28-
if (this.ByteCount > 1)
28+
byte[] writeBuffer = new byte[] { (byte)this.Register };
29+
Span<byte> readBuffer = stackalloc byte[ByteCount];
30+
31+
this.Device.device.Read(readBuffer);
32+
33+
if (this.Raw)
2934
{
30-
this.Device.device.Write((byte)this.Register);
31-
byte[] value = this.Device.device.Read(this.ByteCount);
32-
if (this.Raw)
33-
{
34-
WriteObject(value);
35-
}
36-
else
37-
{
38-
I2CDeviceRegisterData result = new I2CDeviceRegisterData(this.Device, this.Register);
39-
result.Data = value;
40-
WriteObject(result);
41-
}
35+
WriteObject(readBuffer.ToArray());
4236
}
4337
else
4438
{
45-
byte value = this.Device.device.ReadAddressByte(this.Register);
46-
if (this.Raw)
39+
I2CDeviceRegisterData result = new I2CDeviceRegisterData(this.Device, this.Register)
4740
{
48-
WriteObject(value);
49-
}
50-
else
51-
{
52-
I2CDeviceRegisterData result = new I2CDeviceRegisterData(this.Device, this.Register, new byte[1] { value });
53-
WriteObject(result);
54-
}
41+
Data = readBuffer.ToArray() // optimize to be Span? How does PowerShell deal with it?
42+
};
43+
WriteObject(result);
5544
}
5645
}
57-
catch (System.TypeInitializationException e) // Unosquare.RaspberryIO.Gpio.GpioController.Initialize throws this TypeInitializationException
46+
else
5847
{
59-
if (!Unosquare.RaspberryIO.Computer.SystemInfo.Instance.IsRunningAsRoot)
48+
this.Device.device.WriteByte((byte)this.Register);
49+
byte value = this.Device.device.ReadByte();
50+
//byte value = this.Device.device.ReadAddressByte(this.Register);
51+
if (this.Raw)
52+
{
53+
WriteObject(value);
54+
}
55+
else
6056
{
61-
throw new PlatformNotSupportedException(Resources.ErrNeedRootPrivileges, e);
57+
I2CDeviceRegisterData result = new I2CDeviceRegisterData(this.Device, this.Register, new byte[1] { value });
58+
WriteObject(result);
6259
}
63-
throw;
6460
}
6561
}
6662
}

src/Microsoft.PowerShell.IoT/I2c/I2cDevice.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
public class I2CDevice
22
{
3-
internal Unosquare.RaspberryIO.Gpio.I2CDevice device = null;
4-
internal System.Device.I2c.I2cDevice device_v2 = null;
3+
internal System.Device.I2c.I2cDevice device = null;
54

65
public string FriendlyName { get; set; }
76
public int Id { get; set; }
87

9-
public I2CDevice(Unosquare.RaspberryIO.Gpio.I2CDevice device, int Id, string FriendlyName)
8+
public I2CDevice(System.Device.I2c.I2cDevice device, int Id, string FriendlyName)
109
{
1110
this.device = device;
1211
this.Id = Id;

src/Microsoft.PowerShell.IoT/I2c/Set/SetI2cRegister.cs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,12 @@ public class SetI2CRegister : Cmdlet
1818

1919
protected override void ProcessRecord()
2020
{
21-
try
21+
Span<byte> dataOut = stackalloc byte[] { (byte)Register, Data[0] };
22+
this.Device.device.Write(dataOut);
23+
if (this.PassThru)
2224
{
23-
this.Device.device.WriteAddressByte(this.Register, this.Data[0]);
24-
if (this.PassThru)
25-
{
26-
I2CDeviceRegisterData result = new I2CDeviceRegisterData(this.Device, this.Register, this.Data);
27-
WriteObject(result);
28-
}
29-
}
30-
catch (System.TypeInitializationException e) // Unosquare.RaspberryIO.Gpio.GpioController.Initialize throws this TypeInitializationException
31-
{
32-
if (!Unosquare.RaspberryIO.Computer.SystemInfo.Instance.IsRunningAsRoot)
33-
{
34-
throw new PlatformNotSupportedException(Resources.ErrNeedRootPrivileges, e);
35-
}
36-
throw;
25+
I2CDeviceRegisterData result = new I2CDeviceRegisterData(this.Device, this.Register, this.Data);
26+
WriteObject(result);
3727
}
3828
}
3929
}

0 commit comments

Comments
 (0)