Skip to content
This repository was archived by the owner on Jun 14, 2024. It is now read-only.

restructured the project to have each interface in one folder. #45

Merged
merged 2 commits into from
Mar 20, 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
81 changes: 81 additions & 0 deletions src/Microsoft.PowerShell.IoT/Gpio/Get/GetGpioPin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.Collections;
using System.Management.Automation; // PowerShell namespace.

[Cmdlet(VerbsCommon.Get, "GpioPin")]
public class GetGpioPin : Cmdlet
{
[Parameter(Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0)]
public int[] Id { get; set; }

[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 1)]
public PullMode? PullMode { get; set; }

[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)]
public SwitchParameter Raw { get; set; }

protected override void ProcessRecord()
{
try
{
ArrayList pinList = new ArrayList();

if ((this.Id == null) || (this.Id.Length <= 0))
{
foreach (var pin in Unosquare.RaspberryIO.Pi.Gpio.Pins)
{
pinList.Add(pin.PinNumber);
}
}
else
{
pinList.AddRange(this.Id);
}

foreach (int pinId in pinList)
{
var pin = Unosquare.RaspberryIO.Pi.Gpio[pinId];
try
{
pin.PinMode = Unosquare.RaspberryIO.Gpio.GpioPinDriveMode.Input;
if (this.PullMode.HasValue)
{
pin.InputPullMode = (Unosquare.RaspberryIO.Gpio.GpioPinResistorPullMode)this.PullMode.Value;
};
}
catch (System.NotSupportedException)
{
// We want to avoid errors like
// System.NotSupportedException : Get - GpioPin : Pin Pin15 'BCM 14 (UART Transmit)' does not support mode 'Input'.Pin capabilities are limited to: UARTTXD
// at the same time we need to return PinInfo for such pins, so we need to continue processing
}
bool pinBoolValue = pin.Read();

if (this.Raw)
{
WriteObject(pinBoolValue ? SignalLevel.High : SignalLevel.Low);
}
else
{
GpioPinData pinData = new GpioPinData(pinId, pinBoolValue ? SignalLevel.High : SignalLevel.Low, pin);
WriteObject(pinData);
}
}
}
catch (System.TypeInitializationException e) // Unosquare.RaspberryIO.Gpio.GpioController.Initialize throws this TypeInitializationException
{
if (!Unosquare.RaspberryIO.Computer.SystemInfo.Instance.IsRunningAsRoot)
{
throw new PlatformNotSupportedException(Resources.ErrNeedRootPrivileges, e);
}
throw;
}
}
}

public enum PullMode
{
Off = 0,
PullDown = 1,
PullUp = 2
}
19 changes: 19 additions & 0 deletions src/Microsoft.PowerShell.IoT/Gpio/GpioPinData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public class GpioPinData
{
public int Id;
public SignalLevel Value;
//public Unosquare.RaspberryIO.Gpio.GpioPin PinInfo; //not in use

public GpioPinData(int id, SignalLevel value, Unosquare.RaspberryIO.Gpio.GpioPin pinInfo)
{
this.Id = id;
this.Value = value;
//this.PinInfo = pinInfo;
}
}

public enum SignalLevel
{
Low = 0,
High = 1
}
44 changes: 44 additions & 0 deletions src/Microsoft.PowerShell.IoT/Gpio/Set/SetGpioPin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Management.Automation; // PowerShell namespace.

[Cmdlet(VerbsCommon.Set, "GpioPin")]
public class SetGpioPin : Cmdlet
{
[Parameter(Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0)]
public int[] Id { get; set; }

[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, Position = 1)]
public SignalLevel Value { get; set; }

[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)]
public SwitchParameter PassThru { get; set; }

protected override void ProcessRecord()
{
try
{
if (this.Id != null)
{
foreach (int pinId in this.Id)
{
var pin = Unosquare.RaspberryIO.Pi.Gpio[pinId];
pin.PinMode = Unosquare.RaspberryIO.Gpio.GpioPinDriveMode.Output;
pin.Write((Unosquare.RaspberryIO.Gpio.GpioPinValue)this.Value);
if (this.PassThru)
{
GpioPinData pinData = new GpioPinData(pinId, this.Value, pin);
WriteObject(pinData);
}
}
}
}
catch (System.TypeInitializationException e) // Unosquare.RaspberryIO.Gpio.GpioController.Initialize throws this TypeInitializationException
{
if (!Unosquare.RaspberryIO.Computer.SystemInfo.Instance.IsRunningAsRoot)
{
throw new PlatformNotSupportedException(Resources.ErrNeedRootPrivileges, e);
}
throw;
}
}
}
27 changes: 27 additions & 0 deletions src/Microsoft.PowerShell.IoT/I2c/Get/GetI2cDevice.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Management.Automation; // PowerShell namespace.
using System.Device.I2c;

[Cmdlet(VerbsCommon.Get, "I2CDevice")]
public class GetI2CDevice : Cmdlet
{
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, Position = 0)]
public int Id { get; set; }

[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 1)]
public string FriendlyName { get; set; }

public GetI2CDevice()
{
this.FriendlyName = string.Empty;
this.Id = 0;
}

protected override void ProcessRecord()
{
var settings = new I2cConnectionSettings(1, this.Id);
I2cDevice device = I2cDevice.Create(settings);
WriteObject(new I2CDevice(device, this.Id, this.FriendlyName));

}
}
62 changes: 62 additions & 0 deletions src/Microsoft.PowerShell.IoT/I2c/Get/GetI2cRegister.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.Management.Automation; // PowerShell namespace.
[Cmdlet(VerbsCommon.Get, "I2CRegister")]
public class GetI2CRegister : Cmdlet
{
[Parameter(Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0)]
public I2CDevice Device { get; set; }

[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, Position = 1)]
public ushort Register { get; set; }

[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 2)]
public byte ByteCount { get; set; }

[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)]
public SwitchParameter Raw { get; set; }

public GetI2CRegister()
{
this.ByteCount = 1;
this.Raw = false;
}

protected override void ProcessRecord()
{
if (this.ByteCount > 1)
{
byte[] writeBuffer = new byte[] { (byte)this.Register };
Span<byte> readBuffer = stackalloc byte[ByteCount];

this.Device.device.Read(readBuffer);

if (this.Raw)
{
WriteObject(readBuffer.ToArray());
}
else
{
I2CDeviceRegisterData result = new I2CDeviceRegisterData(this.Device, this.Register)
{
Data = readBuffer.ToArray() // optimize to be Span? How does PowerShell deal with it?
};
WriteObject(result);
}
}
else
{
this.Device.device.WriteByte((byte)this.Register);
byte value = this.Device.device.ReadByte();
//byte value = this.Device.device.ReadAddressByte(this.Register);
if (this.Raw)
{
WriteObject(value);
}
else
{
I2CDeviceRegisterData result = new I2CDeviceRegisterData(this.Device, this.Register, new byte[1] { value });
WriteObject(result);
}
}
}
}
26 changes: 26 additions & 0 deletions src/Microsoft.PowerShell.IoT/I2c/I2cDevice.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public class I2CDevice
{
internal System.Device.I2c.I2cDevice device = null;

public string FriendlyName { get; set; }
public int Id { get; set; }

public I2CDevice(System.Device.I2c.I2cDevice device, int Id, string FriendlyName)
{
this.device = device;
this.Id = Id;
this.FriendlyName = FriendlyName;
}

public override string ToString()
{
if (string.IsNullOrEmpty(this.FriendlyName))
{
return this.Id.ToString();
}
else
{
return this.FriendlyName;
}
}
}
23 changes: 23 additions & 0 deletions src/Microsoft.PowerShell.IoT/I2c/I2cRegisterData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
public class I2CDeviceRegisterData
{
public I2CDevice Device { get; set; }
public ushort Register { get; set; }
public byte[] Data { get; set; }

public I2CDeviceRegisterData(I2CDevice device, ushort register, byte[] data)
{
this.Device = device;
this.Register = register;
this.Data = data;
}

public I2CDeviceRegisterData(I2CDevice device, ushort register)
: this(device, register, new byte[0])
{
}

public I2CDeviceRegisterData()
: this(null, 0)
{
}
}
29 changes: 29 additions & 0 deletions src/Microsoft.PowerShell.IoT/I2c/Set/SetI2cRegister.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Management.Automation; // PowerShell namespace.

[Cmdlet(VerbsCommon.Set, "I2CRegister")]
public class SetI2CRegister : Cmdlet
{
[Parameter(Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0)]
public I2CDevice Device { get; set; }

[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, Position = 1)]
public ushort Register { get; set; }

[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, Position = 2)]
public byte[] Data { get; set; }

[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)]
public SwitchParameter PassThru { get; set; }

protected override void ProcessRecord()
{
Span<byte> dataOut = stackalloc byte[] { (byte)Register, Data[0] };
this.Device.device.Write(dataOut);
if (this.PassThru)
{
I2CDeviceRegisterData result = new I2CDeviceRegisterData(this.Device, this.Register, this.Data);
WriteObject(result);
}
}
}
Loading