diff --git a/src/Microsoft.PowerShell.IoT/Gpio/Get/GetGpioPin.cs b/src/Microsoft.PowerShell.IoT/Gpio/Get/GetGpioPin.cs new file mode 100644 index 0000000..ad00c4e --- /dev/null +++ b/src/Microsoft.PowerShell.IoT/Gpio/Get/GetGpioPin.cs @@ -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 +} \ No newline at end of file diff --git a/src/Microsoft.PowerShell.IoT/Gpio/GpioPinData.cs b/src/Microsoft.PowerShell.IoT/Gpio/GpioPinData.cs new file mode 100644 index 0000000..2518516 --- /dev/null +++ b/src/Microsoft.PowerShell.IoT/Gpio/GpioPinData.cs @@ -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 +} \ No newline at end of file diff --git a/src/Microsoft.PowerShell.IoT/Gpio/Set/SetGpioPin.cs b/src/Microsoft.PowerShell.IoT/Gpio/Set/SetGpioPin.cs new file mode 100644 index 0000000..8a640a6 --- /dev/null +++ b/src/Microsoft.PowerShell.IoT/Gpio/Set/SetGpioPin.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/src/Microsoft.PowerShell.IoT/I2c/Get/GetI2cDevice.cs b/src/Microsoft.PowerShell.IoT/I2c/Get/GetI2cDevice.cs new file mode 100644 index 0000000..3a74513 --- /dev/null +++ b/src/Microsoft.PowerShell.IoT/I2c/Get/GetI2cDevice.cs @@ -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)); + + } +} \ No newline at end of file diff --git a/src/Microsoft.PowerShell.IoT/I2c/Get/GetI2cRegister.cs b/src/Microsoft.PowerShell.IoT/I2c/Get/GetI2cRegister.cs new file mode 100644 index 0000000..b74e68c --- /dev/null +++ b/src/Microsoft.PowerShell.IoT/I2c/Get/GetI2cRegister.cs @@ -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 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); + } + } + } +} \ No newline at end of file diff --git a/src/Microsoft.PowerShell.IoT/I2c/I2cDevice.cs b/src/Microsoft.PowerShell.IoT/I2c/I2cDevice.cs new file mode 100644 index 0000000..7eb8b20 --- /dev/null +++ b/src/Microsoft.PowerShell.IoT/I2c/I2cDevice.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/src/Microsoft.PowerShell.IoT/I2c/I2cRegisterData.cs b/src/Microsoft.PowerShell.IoT/I2c/I2cRegisterData.cs new file mode 100644 index 0000000..79b5f64 --- /dev/null +++ b/src/Microsoft.PowerShell.IoT/I2c/I2cRegisterData.cs @@ -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) + { + } +} \ No newline at end of file diff --git a/src/Microsoft.PowerShell.IoT/I2c/Set/SetI2cRegister.cs b/src/Microsoft.PowerShell.IoT/I2c/Set/SetI2cRegister.cs new file mode 100644 index 0000000..f88e791 --- /dev/null +++ b/src/Microsoft.PowerShell.IoT/I2c/Set/SetI2cRegister.cs @@ -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 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); + } + } +} \ No newline at end of file diff --git a/src/Microsoft.PowerShell.IoT/Microsoft.PowerShell.IoT.cs b/src/Microsoft.PowerShell.IoT/Microsoft.PowerShell.IoT.cs deleted file mode 100644 index 8c999b6..0000000 --- a/src/Microsoft.PowerShell.IoT/Microsoft.PowerShell.IoT.cs +++ /dev/null @@ -1,408 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Collections; -using System.Management.Automation; // PowerShell namespace. - -namespace Microsoft.PowerShell.IoT -{ - public class I2CDevice - { - internal Unosquare.RaspberryIO.Gpio.I2CDevice device = null; - - public string FriendlyName { get; set; } - public int Id { get; set; } - - public I2CDevice(Unosquare.RaspberryIO.Gpio.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; - } - } - } - - 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) - { - } - } - - public enum SignalLevel - { - Low = 0, - High = 1 - } - - public enum PullMode - { - Off = 0, - PullDown = 1, - PullUp = 2 - } - - public class GpioPinData - { - public int Id; - public SignalLevel Value; - public Unosquare.RaspberryIO.Gpio.GpioPin PinInfo; - - public GpioPinData(int id, SignalLevel value, Unosquare.RaspberryIO.Gpio.GpioPin pinInfo) - { - this.Id = id; - this.Value = value; - this.PinInfo = pinInfo; - } - } - - public class SPIData - { - public uint Channel { get; set; } - public uint Frequency { get; set; } - public byte[] Data { get; set; } - public byte[] Response { get; set; } - - public SPIData(uint channel, uint frequency, byte[] data, byte[] response) - { - this.Channel = channel; - this.Frequency = frequency; - this.Data = data; - this.Response = response; - } - } - - [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() - { - try - { - WriteObject(new I2CDevice(Unosquare.RaspberryIO.Pi.I2C.AddDevice(this.Id), this.Id, this.FriendlyName)); - } - 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; - } - } - } - - [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() - { - try - { - if (this.ByteCount > 1) - { - this.Device.device.Write((byte)this.Register); - byte[] value = this.Device.device.Read(this.ByteCount); - if (this.Raw) - { - WriteObject(value); - } - else - { - I2CDeviceRegisterData result = new I2CDeviceRegisterData(this.Device, this.Register); - result.Data = value; - WriteObject(result); - } - } - else - { - 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); - } - } - } - 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; - } - } - } - - [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() - { - try - { - this.Device.device.WriteAddressByte(this.Register, this.Data[0]); - if (this.PassThru) - { - I2CDeviceRegisterData result = new I2CDeviceRegisterData(this.Device, this.Register, this.Data); - WriteObject(result); - } - } - 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; - } - } - } - - [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; - } - } - } - - [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; - } - } - } - - [Cmdlet(VerbsCommunications.Send, "SPIData")] - public class SendSPIData : Cmdlet - { - [Parameter(Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0)] - public byte[] Data { get; set; } - - [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 1)] - public uint Channel { get; set; } - - [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 2)] - public uint Frequency { get; set; } - - [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)] - public SwitchParameter Raw { get; set; } - - public SendSPIData() - { - this.Channel = 0; - this.Frequency = Unosquare.RaspberryIO.Gpio.SpiChannel.MinFrequency; - } - - protected override void ProcessRecord() - { - try - { - var spiChannel = Unosquare.RaspberryIO.Pi.Spi.Channel0; - if (this.Channel == 1) - { - spiChannel = Unosquare.RaspberryIO.Pi.Spi.Channel1; - Unosquare.RaspberryIO.Pi.Spi.Channel1Frequency = (int)this.Frequency; - } - else - { - Unosquare.RaspberryIO.Pi.Spi.Channel0Frequency = (int)this.Frequency; - }; - - var response = spiChannel.SendReceive(this.Data); - if (this.Raw) - { - WriteObject(response); - } - else - { - SPIData spiData = new SPIData(this.Channel, this.Frequency, this.Data, response); - WriteObject(spiData); - } - } - 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; - } - } - } -} diff --git a/src/Microsoft.PowerShell.IoT/Microsoft.PowerShell.IoT.csproj b/src/Microsoft.PowerShell.IoT/Microsoft.PowerShell.IoT.csproj index aba0716..1d219b7 100644 --- a/src/Microsoft.PowerShell.IoT/Microsoft.PowerShell.IoT.csproj +++ b/src/Microsoft.PowerShell.IoT/Microsoft.PowerShell.IoT.csproj @@ -16,6 +16,7 @@ + diff --git a/src/Microsoft.PowerShell.IoT/SPI/SPIData.cs b/src/Microsoft.PowerShell.IoT/SPI/SPIData.cs new file mode 100644 index 0000000..9cb4249 --- /dev/null +++ b/src/Microsoft.PowerShell.IoT/SPI/SPIData.cs @@ -0,0 +1,15 @@ + public class SPIData +{ + public uint Channel { get; set; } + public uint Frequency { get; set; } + public byte[] Data { get; set; } + public byte[] Response { get; set; } + + public SPIData(uint channel, uint frequency, byte[] data, byte[] response) + { + this.Channel = channel; + this.Frequency = frequency; + this.Data = data; + this.Response = response; + } +} \ No newline at end of file diff --git a/src/Microsoft.PowerShell.IoT/SPI/Send/SendSPIData.cs b/src/Microsoft.PowerShell.IoT/SPI/Send/SendSPIData.cs new file mode 100644 index 0000000..40690c3 --- /dev/null +++ b/src/Microsoft.PowerShell.IoT/SPI/Send/SendSPIData.cs @@ -0,0 +1,59 @@ +using System; +using System.Management.Automation; // PowerShell namespace. +[Cmdlet(VerbsCommunications.Send, "SPIData")] +public class SendSPIData : Cmdlet +{ + [Parameter(Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0)] + public byte[] Data { get; set; } + + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 1)] + public uint Channel { get; set; } + + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 2)] + public uint Frequency { get; set; } + + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)] + public SwitchParameter Raw { get; set; } + + public SendSPIData() + { + this.Channel = 0; + this.Frequency = Unosquare.RaspberryIO.Gpio.SpiChannel.MinFrequency; + } + + protected override void ProcessRecord() + { + try + { + var spiChannel = Unosquare.RaspberryIO.Pi.Spi.Channel0; + if (this.Channel == 1) + { + spiChannel = Unosquare.RaspberryIO.Pi.Spi.Channel1; + Unosquare.RaspberryIO.Pi.Spi.Channel1Frequency = (int)this.Frequency; + } + else + { + Unosquare.RaspberryIO.Pi.Spi.Channel0Frequency = (int)this.Frequency; + }; + + var response = spiChannel.SendReceive(this.Data); + if (this.Raw) + { + WriteObject(response); + } + else + { + SPIData spiData = new SPIData(this.Channel, this.Frequency, this.Data, response); + WriteObject(spiData); + } + } + 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; + } + } +} \ No newline at end of file