Skip to content

Commit c1f04d7

Browse files
author
Andrew
authored
Merge pull request PowerShell#45 from DanielSSilva/master
restructured the project to have each interface in one folder.
2 parents 6f28494 + 83c3b17 commit c1f04d7

12 files changed

+386
-408
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System;
2+
using System.Collections;
3+
using System.Management.Automation; // PowerShell namespace.
4+
5+
[Cmdlet(VerbsCommon.Get, "GpioPin")]
6+
public class GetGpioPin : Cmdlet
7+
{
8+
[Parameter(Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0)]
9+
public int[] Id { get; set; }
10+
11+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 1)]
12+
public PullMode? PullMode { get; set; }
13+
14+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)]
15+
public SwitchParameter Raw { get; set; }
16+
17+
protected override void ProcessRecord()
18+
{
19+
try
20+
{
21+
ArrayList pinList = new ArrayList();
22+
23+
if ((this.Id == null) || (this.Id.Length <= 0))
24+
{
25+
foreach (var pin in Unosquare.RaspberryIO.Pi.Gpio.Pins)
26+
{
27+
pinList.Add(pin.PinNumber);
28+
}
29+
}
30+
else
31+
{
32+
pinList.AddRange(this.Id);
33+
}
34+
35+
foreach (int pinId in pinList)
36+
{
37+
var pin = Unosquare.RaspberryIO.Pi.Gpio[pinId];
38+
try
39+
{
40+
pin.PinMode = Unosquare.RaspberryIO.Gpio.GpioPinDriveMode.Input;
41+
if (this.PullMode.HasValue)
42+
{
43+
pin.InputPullMode = (Unosquare.RaspberryIO.Gpio.GpioPinResistorPullMode)this.PullMode.Value;
44+
};
45+
}
46+
catch (System.NotSupportedException)
47+
{
48+
// We want to avoid errors like
49+
// System.NotSupportedException : Get - GpioPin : Pin Pin15 'BCM 14 (UART Transmit)' does not support mode 'Input'.Pin capabilities are limited to: UARTTXD
50+
// at the same time we need to return PinInfo for such pins, so we need to continue processing
51+
}
52+
bool pinBoolValue = pin.Read();
53+
54+
if (this.Raw)
55+
{
56+
WriteObject(pinBoolValue ? SignalLevel.High : SignalLevel.Low);
57+
}
58+
else
59+
{
60+
GpioPinData pinData = new GpioPinData(pinId, pinBoolValue ? SignalLevel.High : SignalLevel.Low, pin);
61+
WriteObject(pinData);
62+
}
63+
}
64+
}
65+
catch (System.TypeInitializationException e) // Unosquare.RaspberryIO.Gpio.GpioController.Initialize throws this TypeInitializationException
66+
{
67+
if (!Unosquare.RaspberryIO.Computer.SystemInfo.Instance.IsRunningAsRoot)
68+
{
69+
throw new PlatformNotSupportedException(Resources.ErrNeedRootPrivileges, e);
70+
}
71+
throw;
72+
}
73+
}
74+
}
75+
76+
public enum PullMode
77+
{
78+
Off = 0,
79+
PullDown = 1,
80+
PullUp = 2
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
public class GpioPinData
2+
{
3+
public int Id;
4+
public SignalLevel Value;
5+
//public Unosquare.RaspberryIO.Gpio.GpioPin PinInfo; //not in use
6+
7+
public GpioPinData(int id, SignalLevel value, Unosquare.RaspberryIO.Gpio.GpioPin pinInfo)
8+
{
9+
this.Id = id;
10+
this.Value = value;
11+
//this.PinInfo = pinInfo;
12+
}
13+
}
14+
15+
public enum SignalLevel
16+
{
17+
Low = 0,
18+
High = 1
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Management.Automation; // PowerShell namespace.
3+
4+
[Cmdlet(VerbsCommon.Set, "GpioPin")]
5+
public class SetGpioPin : Cmdlet
6+
{
7+
[Parameter(Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0)]
8+
public int[] Id { get; set; }
9+
10+
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, Position = 1)]
11+
public SignalLevel Value { get; set; }
12+
13+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)]
14+
public SwitchParameter PassThru { get; set; }
15+
16+
protected override void ProcessRecord()
17+
{
18+
try
19+
{
20+
if (this.Id != null)
21+
{
22+
foreach (int pinId in this.Id)
23+
{
24+
var pin = Unosquare.RaspberryIO.Pi.Gpio[pinId];
25+
pin.PinMode = Unosquare.RaspberryIO.Gpio.GpioPinDriveMode.Output;
26+
pin.Write((Unosquare.RaspberryIO.Gpio.GpioPinValue)this.Value);
27+
if (this.PassThru)
28+
{
29+
GpioPinData pinData = new GpioPinData(pinId, this.Value, pin);
30+
WriteObject(pinData);
31+
}
32+
}
33+
}
34+
}
35+
catch (System.TypeInitializationException e) // Unosquare.RaspberryIO.Gpio.GpioController.Initialize throws this TypeInitializationException
36+
{
37+
if (!Unosquare.RaspberryIO.Computer.SystemInfo.Instance.IsRunningAsRoot)
38+
{
39+
throw new PlatformNotSupportedException(Resources.ErrNeedRootPrivileges, e);
40+
}
41+
throw;
42+
}
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Management.Automation; // PowerShell namespace.
3+
using System.Device.I2c;
4+
5+
[Cmdlet(VerbsCommon.Get, "I2CDevice")]
6+
public class GetI2CDevice : Cmdlet
7+
{
8+
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, Position = 0)]
9+
public int Id { get; set; }
10+
11+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 1)]
12+
public string FriendlyName { get; set; }
13+
14+
public GetI2CDevice()
15+
{
16+
this.FriendlyName = string.Empty;
17+
this.Id = 0;
18+
}
19+
20+
protected override void ProcessRecord()
21+
{
22+
var settings = new I2cConnectionSettings(1, this.Id);
23+
I2cDevice device = I2cDevice.Create(settings);
24+
WriteObject(new I2CDevice(device, this.Id, this.FriendlyName));
25+
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Management.Automation; // PowerShell namespace.
3+
[Cmdlet(VerbsCommon.Get, "I2CRegister")]
4+
public class GetI2CRegister : Cmdlet
5+
{
6+
[Parameter(Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0)]
7+
public I2CDevice Device { get; set; }
8+
9+
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, Position = 1)]
10+
public ushort Register { get; set; }
11+
12+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 2)]
13+
public byte ByteCount { get; set; }
14+
15+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)]
16+
public SwitchParameter Raw { get; set; }
17+
18+
public GetI2CRegister()
19+
{
20+
this.ByteCount = 1;
21+
this.Raw = false;
22+
}
23+
24+
protected override void ProcessRecord()
25+
{
26+
if (this.ByteCount > 1)
27+
{
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)
34+
{
35+
WriteObject(readBuffer.ToArray());
36+
}
37+
else
38+
{
39+
I2CDeviceRegisterData result = new I2CDeviceRegisterData(this.Device, this.Register)
40+
{
41+
Data = readBuffer.ToArray() // optimize to be Span? How does PowerShell deal with it?
42+
};
43+
WriteObject(result);
44+
}
45+
}
46+
else
47+
{
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
56+
{
57+
I2CDeviceRegisterData result = new I2CDeviceRegisterData(this.Device, this.Register, new byte[1] { value });
58+
WriteObject(result);
59+
}
60+
}
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class I2CDevice
2+
{
3+
internal System.Device.I2c.I2cDevice device = null;
4+
5+
public string FriendlyName { get; set; }
6+
public int Id { get; set; }
7+
8+
public I2CDevice(System.Device.I2c.I2cDevice device, int Id, string FriendlyName)
9+
{
10+
this.device = device;
11+
this.Id = Id;
12+
this.FriendlyName = FriendlyName;
13+
}
14+
15+
public override string ToString()
16+
{
17+
if (string.IsNullOrEmpty(this.FriendlyName))
18+
{
19+
return this.Id.ToString();
20+
}
21+
else
22+
{
23+
return this.FriendlyName;
24+
}
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class I2CDeviceRegisterData
2+
{
3+
public I2CDevice Device { get; set; }
4+
public ushort Register { get; set; }
5+
public byte[] Data { get; set; }
6+
7+
public I2CDeviceRegisterData(I2CDevice device, ushort register, byte[] data)
8+
{
9+
this.Device = device;
10+
this.Register = register;
11+
this.Data = data;
12+
}
13+
14+
public I2CDeviceRegisterData(I2CDevice device, ushort register)
15+
: this(device, register, new byte[0])
16+
{
17+
}
18+
19+
public I2CDeviceRegisterData()
20+
: this(null, 0)
21+
{
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Management.Automation; // PowerShell namespace.
3+
4+
[Cmdlet(VerbsCommon.Set, "I2CRegister")]
5+
public class SetI2CRegister : Cmdlet
6+
{
7+
[Parameter(Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0)]
8+
public I2CDevice Device { get; set; }
9+
10+
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, Position = 1)]
11+
public ushort Register { get; set; }
12+
13+
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, Position = 2)]
14+
public byte[] Data { get; set; }
15+
16+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)]
17+
public SwitchParameter PassThru { get; set; }
18+
19+
protected override void ProcessRecord()
20+
{
21+
Span<byte> dataOut = stackalloc byte[] { (byte)Register, Data[0] };
22+
this.Device.device.Write(dataOut);
23+
if (this.PassThru)
24+
{
25+
I2CDeviceRegisterData result = new I2CDeviceRegisterData(this.Device, this.Register, this.Data);
26+
WriteObject(result);
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)