diff --git a/README.md b/README.md
index 9d6b8cc..557f765 100644
--- a/README.md
+++ b/README.md
@@ -48,10 +48,8 @@ Please see our [docs folder here](/docs) for an API reference, pin layout and ot
### Dependencies
-This project relies on [RaspberryIO](https://github.com/unosquare/raspberryio).
-It's an easy-to-use .NET library for interacting with Raspberry Pi's IO functionality.
-RaspberryIO is built on [Wiring Pi](http://wiringpi.com/) -
-a pin based GPIO access library written in C.
+This project relies on [.NET Core IoT Libraries](https://github.com/dotnet/iot).
+It is a .NET library for interacting with Raspberry Pi's IO functionality.
## Installation
@@ -60,7 +58,6 @@ a pin based GPIO access library written in C.
You can grab the latest version of PowerShell IoT by running:
```powershell
-sudo WIRINGPI_CODES=1 pwsh
Install-Module Microsoft.PowerShell.IoT
```
@@ -130,17 +127,12 @@ _NOTE: If you'd rather not use the script, simply copy the `out/Microsoft.PowerS
#### Running
-First, you must run pwsh with sudo:
+Start PowerShell:
```powershell
-sudo WIRINGPI_CODES=1 pwsh
+pwsh
```
-##### About `WIRINGPI_CODES` environment variable
-
-`Microsoft.PowerShell.IoT` module internally uses [WiringPi library](http://wiringpi.com/reference/setup) which has a default behavior of terminating current process (in this case - PowerShell) even on non-critical errors in setup functions.
-To avoid such crashes define `WIRINGPI_CODES` environment variable either when starting PowerShell (see example above) or through configuration scripts - example for an interactive login shell - `echo "export WIRINGPI_CODES=1"|sudo tee -a /etc/profile.d/WiringPiCodes.sh`
-
If you have the `Microsoft.PowerShell.IoT` module in your PSModulePath:
```powershell
diff --git a/ThirdPartyNotices.txt b/ThirdPartyNotices.txt
deleted file mode 100644
index 56f7911..0000000
--- a/ThirdPartyNotices.txt
+++ /dev/null
@@ -1,101 +0,0 @@
-
-THIRD-PARTY SOFTWARE NOTICES AND INFORMATION
-Do Not Translate or Localize
-
-The software is based on or incorporates material from the projects listed below (collectively, “Third Party Code”). Microsoft is not the original author of the Third Party Code. The original copyright notice and license, under which Microsoft received such Third Party Code, are set forth below. Microsoft reserves all rights not expressly granted herein, whether by implication, estoppel or otherwise.
-
-
--------------------------------------------------
-File: Unosquare.Rasberry.IO 0.14.0
--------------------------------------------------
-
-Copyright (c) 2016 Unosquare
-
-Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-
-This software contains a compiled, unmodified version of the WiringPi library WiringPi is a GPIO access library written in C for the BCM2835 used in the Raspberry Pi. It’s released under the GNU LGPLv3 license and is usable from C and C++ and many other languages with suitable wrappers. A program that contains no derivative of any portion of the Library, but is designed to work with the Library by being compiled or linked with it, is called a "work that uses the Library". Such a work, in isolation, is not a derivative work of the Library, and therefore falls outside the scope of this License. Raspberry IO is then, by definition, "work that uses the Library"
-
-
--------------------------------------------------
-File: WiringPi
--------------------------------------------------
-
-GNU LESSER GENERAL PUBLIC LICENSE
-Version 3, 29 June 2007
-
-Copyright (C) 2007 Free Software Foundation, Inc.
-
-Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
-
-This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below.
-
-
-
-0. Additional Definitions.
-
-As used herein, “this License” refers to version 3 of the GNU Lesser General Public License, and the “GNU GPL” refers to version 3 of the GNU General Public License.
-
-“The Library” refers to a covered work governed by this License, other than an Application or a Combined Work as defined below.
-
-An “Application” is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library.
-
-A “Combined Work” is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the “Linked Version”.
-
-The “Minimal Corresponding Source” for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version.
-
-The “Corresponding Application Code” for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work.
-
-
-1. Exception to Section 3 of the GNU GPL.
-
-You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL.
-
-
-2. Conveying Modified Versions.
-
-If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version:
-
-a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or
-b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy.
-
-
-3. Object Code Incorporating Material from Library Header Files.
-
-The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following:
-
-a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License.
-b) Accompany the object code with a copy of the GNU GPL and this license document.
-
-
-4. Combined Works.
-
-You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following:
-
-a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License.
-b) Accompany the Combined Work with a copy of the GNU GPL and this license document.
-c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document.
-d) Do one of the following:
-0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.
-1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version.
-e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.)
-
-
-5. Combined Libraries.
-
-You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following:
-
-a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License.
-b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work.
-
-
-6. Revised Versions of the GNU Lesser General Public License.
-
-The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns.
-
-Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation.
-
-If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library.
diff --git a/docs/rpi3_pin_layout.md b/docs/rpi3_pin_layout.md
index a6e5b0b..7326674 100644
--- a/docs/rpi3_pin_layout.md
+++ b/docs/rpi3_pin_layout.md
@@ -1,8 +1,8 @@
# Pin layout
-PowerShell IoT uses Wiring Pi under the hood.
-As such, it follow the Wiring Pi scheme for pin identification.
-For all of the `Pin` parameters in this module, use the WiringPi/wPi number listed in the following diagram:
+PowerShell IoT uses logical representation of the microcontroller's GPIOs.
+Refer to the microcontroller's datasheet to find this information.
+In case of Raspberry Pi use the BCM number listed in the following diagram:
## Image reference
diff --git a/src/Microsoft.PowerShell.IoT/Gpio/Get/GetGpioPin.cs b/src/Microsoft.PowerShell.IoT/Gpio/Get/GetGpioPin.cs
index ad00c4e..ff0b14f 100644
--- a/src/Microsoft.PowerShell.IoT/Gpio/Get/GetGpioPin.cs
+++ b/src/Microsoft.PowerShell.IoT/Gpio/Get/GetGpioPin.cs
@@ -1,81 +1,72 @@
using System;
using System.Collections;
-using System.Management.Automation; // PowerShell namespace.
+using System.Management.Automation;
+using System.Device.Gpio;
- [Cmdlet(VerbsCommon.Get, "GpioPin")]
-public class GetGpioPin : Cmdlet
+[Cmdlet(VerbsCommon.Get, "GpioPin")]
+public class GetGpioPin : GpioCmdletBase
{
- [Parameter(Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0)]
- public int[] Id { get; set; }
+ [Parameter(Mandatory = true, 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, Position = 1)]
+ public PullMode? PullMode { get; set; }
- [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)]
- public SwitchParameter Raw { get; set; }
+ [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)]
+ public SwitchParameter Raw { get; set; }
- protected override void ProcessRecord()
- {
- try
- {
- ArrayList pinList = new ArrayList();
+ protected override void ProcessRecord()
+ {
+ 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);
- }
+ if ((this.Id == null) || (this.Id.Length <= 0))
+ {
+ // TODO: this is "gpio readall" functionality
+ // do not forget to change Id param to Mandatory = false when this is implemented
+ }
+ else
+ {
+ pinList.AddRange(this.Id);
+ }
+
+ PinMode mode = PinMode.Input;
+ if (this.PullMode.HasValue)
+ {
+ switch (this.PullMode.Value)
+ {
+ case global::PullMode.PullDown: mode = PinMode.InputPullDown; break;
+ case global::PullMode.PullUp: mode = PinMode.InputPullUp; break;
+ default: mode = PinMode.Input; break;
+ };
+ };
- 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();
+ foreach (int pinId in pinList)
+ {
+ SignalLevel slResult = SignalLevel.Low;
- 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;
- }
- }
+ this.EnsureOpenPin(pinId, mode);
+
+ if (this.GpioController.Read(pinId) == PinValue.High)
+ {
+ slResult = SignalLevel.High;
+ };
+
+ if (this.Raw)
+ {
+ WriteObject(slResult);
+ }
+ else
+ {
+ GpioPinData pinData = new GpioPinData(pinId, slResult);
+ WriteObject(pinData);
+ }
+ }
+ }
}
public enum PullMode
{
- Off = 0,
- PullDown = 1,
- PullUp = 2
-}
\ No newline at end of file
+ Off = 0,
+ PullDown = 1,
+ PullUp = 2
+}
diff --git a/src/Microsoft.PowerShell.IoT/Gpio/GpioCmdletBase.cs b/src/Microsoft.PowerShell.IoT/Gpio/GpioCmdletBase.cs
new file mode 100644
index 0000000..1feeb0b
--- /dev/null
+++ b/src/Microsoft.PowerShell.IoT/Gpio/GpioCmdletBase.cs
@@ -0,0 +1,51 @@
+using System;
+using System.Management.Automation;
+using System.Device.Gpio;
+
+public class GpioCmdletBase : Cmdlet
+{
+ protected static GpioController StaticGpioController;
+ protected GpioController GpioController
+ {
+ get
+ {
+ if (StaticGpioController == null)
+ {
+ StaticGpioController = new GpioController();
+ }
+ return StaticGpioController;
+ }
+ set
+ {
+ StaticGpioController = value;
+ }
+ }
+
+ protected void EnsureOpenPin(int pinId, PinMode mode)
+ {
+ if (this.GpioController.IsPinOpen(pinId))
+ {
+ if (this.GpioController.GetPinMode(pinId) != mode)
+ {
+ this.GpioController.SetPinMode(pinId, mode);
+ }
+ }
+ else
+ {
+ this.GpioController.OpenPin(pinId, mode);
+ }
+ }
+}
+
+[Cmdlet(VerbsCommon.Clear, "GpioResources")]
+public class ClearGpioResources : GpioCmdletBase
+{
+ protected override void ProcessRecord()
+ {
+ if (GpioCmdletBase.StaticGpioController != null)
+ {
+ GpioCmdletBase.StaticGpioController.Dispose();
+ GpioCmdletBase.StaticGpioController = null;
+ }
+ }
+}
diff --git a/src/Microsoft.PowerShell.IoT/Gpio/GpioPinData.cs b/src/Microsoft.PowerShell.IoT/Gpio/GpioPinData.cs
index 2518516..171d05d 100644
--- a/src/Microsoft.PowerShell.IoT/Gpio/GpioPinData.cs
+++ b/src/Microsoft.PowerShell.IoT/Gpio/GpioPinData.cs
@@ -1,19 +1,17 @@
public class GpioPinData
{
- public int Id;
- public SignalLevel Value;
- //public Unosquare.RaspberryIO.Gpio.GpioPin PinInfo; //not in use
+ public int Id;
+ public SignalLevel Value;
- public GpioPinData(int id, SignalLevel value, Unosquare.RaspberryIO.Gpio.GpioPin pinInfo)
- {
- this.Id = id;
- this.Value = value;
- //this.PinInfo = pinInfo;
- }
+ public GpioPinData(int id, SignalLevel value)
+ {
+ this.Id = id;
+ this.Value = value;
+ }
}
public enum SignalLevel
{
- Low = 0,
- High = 1
-}
\ No newline at end of file
+ Low = 0,
+ High = 1
+}
diff --git a/src/Microsoft.PowerShell.IoT/Gpio/Set/SetGpioPin.cs b/src/Microsoft.PowerShell.IoT/Gpio/Set/SetGpioPin.cs
index 8a640a6..cb126d1 100644
--- a/src/Microsoft.PowerShell.IoT/Gpio/Set/SetGpioPin.cs
+++ b/src/Microsoft.PowerShell.IoT/Gpio/Set/SetGpioPin.cs
@@ -1,44 +1,42 @@
using System;
-using System.Management.Automation; // PowerShell namespace.
+using System.Management.Automation;
+using System.Device.Gpio;
[Cmdlet(VerbsCommon.Set, "GpioPin")]
-public class SetGpioPin : Cmdlet
+public class SetGpioPin : GpioCmdletBase
{
- [Parameter(Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0)]
- public int[] Id { get; set; }
+ [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 = true, ValueFromPipelineByPropertyName = true, Position = 1)]
+ public SignalLevel Value { get; set; }
- [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)]
- public SwitchParameter PassThru { 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
+ protected override void ProcessRecord()
+ {
+ if (this.Id != null)
+ {
+ foreach (int pinId in this.Id)
+ {
+ this.EnsureOpenPin(pinId, PinMode.Output);
+
+ if(this.Value == SignalLevel.Low)
+ {
+ this.GpioController.Write(pinId, PinValue.Low);
+ }
+ else
+ {
+ this.GpioController.Write(pinId, PinValue.High);
+ }
+
+ if (this.PassThru)
+ {
+ GpioPinData pinData = new GpioPinData(pinId, this.Value);
+ WriteObject(pinData);
+ }
+ }
+ }
+ }
+}
diff --git a/src/Microsoft.PowerShell.IoT/I2c/Get/GetI2cDevice.cs b/src/Microsoft.PowerShell.IoT/I2c/Get/GetI2cDevice.cs
index 3a74513..e7c91c4 100644
--- a/src/Microsoft.PowerShell.IoT/I2c/Get/GetI2cDevice.cs
+++ b/src/Microsoft.PowerShell.IoT/I2c/Get/GetI2cDevice.cs
@@ -1,27 +1,29 @@
using System;
-using System.Management.Automation; // PowerShell namespace.
+using System.Management.Automation;
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 = true, ValueFromPipelineByPropertyName = true, Position = 0)]
+ public int Id { get; set; }
- [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 1)]
- public string FriendlyName { get; set; }
+ [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 1)]
+ public string FriendlyName { get; set; }
- public GetI2CDevice()
- {
- this.FriendlyName = string.Empty;
- this.Id = 0;
- }
+ [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 2)]
+ public int BusId { get; set; }
- protected override void ProcessRecord()
- {
- var settings = new I2cConnectionSettings(1, this.Id);
- I2cDevice device = I2cDevice.Create(settings);
- WriteObject(new I2CDevice(device, this.Id, this.FriendlyName));
+ public GetI2CDevice()
+ {
+ this.FriendlyName = string.Empty;
+ this.BusId = 1;
+ }
- }
-}
\ No newline at end of file
+ protected override void ProcessRecord()
+ {
+ var settings = new I2cConnectionSettings(this.BusId, this.Id);
+ I2cDevice device = I2cDevice.Create(settings);
+ WriteObject(new I2CDevice(device, this.Id, this.FriendlyName, this.BusId));
+ }
+}
diff --git a/src/Microsoft.PowerShell.IoT/I2c/Get/GetI2cRegister.cs b/src/Microsoft.PowerShell.IoT/I2c/Get/GetI2cRegister.cs
index b74e68c..4835418 100644
--- a/src/Microsoft.PowerShell.IoT/I2c/Get/GetI2cRegister.cs
+++ b/src/Microsoft.PowerShell.IoT/I2c/Get/GetI2cRegister.cs
@@ -3,60 +3,60 @@
[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, 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 = 1)]
+ public ushort Register { get; set; }
- [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 2)]
- public byte ByteCount { get; set; }
+ [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 2)]
+ public byte ByteCount { get; set; }
- [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)]
- public SwitchParameter Raw { get; set; }
+ [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)]
+ public SwitchParameter Raw { get; set; }
- public GetI2CRegister()
- {
- this.ByteCount = 1;
- this.Raw = false;
- }
+ 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];
+ protected override void ProcessRecord()
+ {
+ if (this.ByteCount > 1)
+ {
+ Span readBuffer = stackalloc byte[ByteCount];
- this.Device.device.Read(readBuffer);
+ this.Device.device.WriteByte((byte)this.Register);
+ 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
+ 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);
+ }
+ }
+ }
+}
diff --git a/src/Microsoft.PowerShell.IoT/I2c/I2cDevice.cs b/src/Microsoft.PowerShell.IoT/I2c/I2cDevice.cs
index 7eb8b20..9d283e7 100644
--- a/src/Microsoft.PowerShell.IoT/I2c/I2cDevice.cs
+++ b/src/Microsoft.PowerShell.IoT/I2c/I2cDevice.cs
@@ -1,26 +1,28 @@
public class I2CDevice
{
- internal System.Device.I2c.I2cDevice device = null;
+ internal System.Device.I2c.I2cDevice device = null;
- public string FriendlyName { get; set; }
- public int Id { get; set; }
+ public string FriendlyName { get; set; }
+ public int Id { get; set; }
+ public int BusId { get; set; }
- public I2CDevice(System.Device.I2c.I2cDevice device, int Id, string FriendlyName)
- {
- this.device = device;
- this.Id = Id;
- this.FriendlyName = FriendlyName;
- }
+ public I2CDevice(System.Device.I2c.I2cDevice device, int Id, string FriendlyName, int BusId)
+ {
+ this.device = device;
+ this.Id = Id;
+ this.FriendlyName = FriendlyName;
+ this.BusId = BusId;
+ }
- public override string ToString()
- {
- if (string.IsNullOrEmpty(this.FriendlyName))
- {
- return this.Id.ToString();
- }
- else
- {
- return this.FriendlyName;
- }
- }
-}
\ No newline at end of file
+ public override string ToString()
+ {
+ if (string.IsNullOrEmpty(this.FriendlyName))
+ {
+ return this.Id.ToString();
+ }
+ else
+ {
+ return this.FriendlyName;
+ }
+ }
+}
diff --git a/src/Microsoft.PowerShell.IoT/I2c/I2cRegisterData.cs b/src/Microsoft.PowerShell.IoT/I2c/I2cRegisterData.cs
index 79b5f64..edee022 100644
--- a/src/Microsoft.PowerShell.IoT/I2c/I2cRegisterData.cs
+++ b/src/Microsoft.PowerShell.IoT/I2c/I2cRegisterData.cs
@@ -1,23 +1,23 @@
public class I2CDeviceRegisterData
{
- public I2CDevice Device { get; set; }
- public ushort Register { get; set; }
- public byte[] Data { get; set; }
+ 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, 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(I2CDevice device, ushort register)
+ : this(device, register, new byte[0])
+ {
+ }
- public I2CDeviceRegisterData()
- : this(null, 0)
- {
- }
-}
\ No newline at end of file
+ public I2CDeviceRegisterData()
+ : this(null, 0)
+ {
+ }
+}
diff --git a/src/Microsoft.PowerShell.IoT/I2c/Set/SetI2cRegister.cs b/src/Microsoft.PowerShell.IoT/I2c/Set/SetI2cRegister.cs
index f88e791..96b8770 100644
--- a/src/Microsoft.PowerShell.IoT/I2c/Set/SetI2cRegister.cs
+++ b/src/Microsoft.PowerShell.IoT/I2c/Set/SetI2cRegister.cs
@@ -4,26 +4,26 @@
[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, 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 = 1)]
+ public ushort Register { get; set; }
- [Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, Position = 2)]
- public byte[] Data { get; set; }
+ [Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, Position = 2)]
+ public byte[] Data { get; set; }
- [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)]
- public SwitchParameter PassThru { 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
+ 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);
+ }
+ }
+}
diff --git a/src/Microsoft.PowerShell.IoT/Microsoft.PowerShell.IoT.csproj b/src/Microsoft.PowerShell.IoT/Microsoft.PowerShell.IoT.csproj
index 1d219b7..bc659f1 100644
--- a/src/Microsoft.PowerShell.IoT/Microsoft.PowerShell.IoT.csproj
+++ b/src/Microsoft.PowerShell.IoT/Microsoft.PowerShell.IoT.csproj
@@ -1,14 +1,14 @@
- netcoreapp2.0
- 0.1.1.0
+ netstandard2.0
+ 0.2.0.0
Microsoft Corporation
Microsoft Corporation
Microsoft.PowerShell.IoT
Microsoft.PowerShell.IoT
- 0.1.1.0
- 0.1.1.0
+ 0.2.0.0
+ 0.2.0.0
© Microsoft Corporation. All rights reserved.
https://github.com/PowerShell/PowerShell-IoT/blob/master/LICENSE.txt
https://github.com/PowerShell/PowerShell-IoT
@@ -17,8 +17,7 @@
-
-
+
diff --git a/src/Microsoft.PowerShell.IoT/Microsoft.PowerShell.IoT.psd1 b/src/Microsoft.PowerShell.IoT/Microsoft.PowerShell.IoT.psd1
index b6c25c5..af4817d 100644
--- a/src/Microsoft.PowerShell.IoT/Microsoft.PowerShell.IoT.psd1
+++ b/src/Microsoft.PowerShell.IoT/Microsoft.PowerShell.IoT.psd1
@@ -7,7 +7,7 @@
CompanyName="Microsoft Corporation"
Copyright="© Microsoft Corporation. All rights reserved."
Description='A PowerShell module for interacting with hardware sensors and devices using common protocols: GPIO, I2C & SPI.'
- ModuleVersion="0.1.1"
+ ModuleVersion="0.2.0"
FunctionsToExport = '*'
CmdletsToExport = '*'
AliasesToExport = @()
@@ -29,7 +29,11 @@
# IconUri = ''
# ReleaseNotes of this module
- ReleaseNotes = '## 0.1.1
+ ReleaseNotes = '## 0.2.0
+
+Started using System.Device.Gpio
+
+ ## 0.1.1
Minor bug fixes
diff --git a/src/Microsoft.PowerShell.IoT/SPI/SPIData.cs b/src/Microsoft.PowerShell.IoT/SPI/SPIData.cs
index 9cb4249..27528fb 100644
--- a/src/Microsoft.PowerShell.IoT/SPI/SPIData.cs
+++ b/src/Microsoft.PowerShell.IoT/SPI/SPIData.cs
@@ -1,15 +1,17 @@
- public class SPIData
+public class SPIData
{
- public uint Channel { get; set; }
- public uint Frequency { get; set; }
- public byte[] Data { get; set; }
- public byte[] Response { get; set; }
+ public int BusId { get; set; }
+ public int ChipSelectLine { get; set; }
+ public int 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
+ public SPIData(int busId, int chipSelectLine, int frequency, byte[] data, byte[] response)
+ {
+ this.BusId = busId;
+ this.ChipSelectLine = chipSelectLine;
+ this.Frequency = frequency;
+ this.Data = data;
+ this.Response = response;
+ }
+}
diff --git a/src/Microsoft.PowerShell.IoT/SPI/Send/SendSPIData.cs b/src/Microsoft.PowerShell.IoT/SPI/Send/SendSPIData.cs
index 40690c3..c23d5ab 100644
--- a/src/Microsoft.PowerShell.IoT/SPI/Send/SendSPIData.cs
+++ b/src/Microsoft.PowerShell.IoT/SPI/Send/SendSPIData.cs
@@ -1,59 +1,52 @@
using System;
-using System.Management.Automation; // PowerShell namespace.
+using System.Management.Automation;
+using System.Device.Spi;
+
[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
+ [Parameter(Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0)]
+ public byte[] Data { get; set; }
+
+ [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 1)]
+ public int BusId { get; set; }
+
+ [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 2)]
+ public int ChipSelectLine { get; set; }
+
+ [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 3)]
+ public int Frequency { get; set; }
+
+ [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)]
+ public SwitchParameter Raw { get; set; }
+
+ public SendSPIData()
+ {
+ this.BusId = 0;
+ this.ChipSelectLine = 0;
+ this.Frequency = 500_000; // 500 KHz default speed
+ }
+
+ protected override void ProcessRecord()
+ {
+ var settings = new SpiConnectionSettings(this.BusId, this.ChipSelectLine);
+ settings.ClockFrequency = this.Frequency;
+
+ using(var spiDevice = SpiDevice.Create(settings))
+ {
+ var response = new byte[this.Data.Length];
+
+ spiDevice.TransferFullDuplex(this.Data, response);
+
+ if (this.Raw)
+ {
+ WriteObject(response);
+ }
+ else
+ {
+ SPIData spiData = new SPIData(this.BusId, this.ChipSelectLine, this.Frequency, this.Data, response);
+ WriteObject(spiData);
+ }
+ }
+ }
+}