Skip to content

Commit a0b59a0

Browse files
author
Andrew
authored
Merge pull request PowerShell#51 from anmenaga/v2updates
2 parents c1f04d7 + cda7aaf commit a0b59a0

16 files changed

+364
-433
lines changed

README.md

+4-12
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,8 @@ Please see our [docs folder here](/docs) for an API reference, pin layout and ot
4848

4949
### Dependencies
5050

51-
This project relies on [RaspberryIO](https://github.com/unosquare/raspberryio).
52-
It's an easy-to-use .NET library for interacting with Raspberry Pi's IO functionality.
53-
RaspberryIO is built on [Wiring Pi](http://wiringpi.com/) -
54-
a pin based GPIO access library written in C.
51+
This project relies on [.NET Core IoT Libraries](https://github.com/dotnet/iot).
52+
It is a .NET library for interacting with Raspberry Pi's IO functionality.
5553

5654
## Installation
5755

@@ -60,7 +58,6 @@ a pin based GPIO access library written in C.
6058
You can grab the latest version of PowerShell IoT by running:
6159

6260
```powershell
63-
sudo WIRINGPI_CODES=1 pwsh
6461
Install-Module Microsoft.PowerShell.IoT
6562
```
6663

@@ -130,17 +127,12 @@ _NOTE: If you'd rather not use the script, simply copy the `out/Microsoft.PowerS
130127

131128
#### Running
132129

133-
First, you must run pwsh with sudo:
130+
Start PowerShell:
134131

135132
```powershell
136-
sudo WIRINGPI_CODES=1 pwsh
133+
pwsh
137134
```
138135

139-
##### About `WIRINGPI_CODES` environment variable
140-
141-
`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.
142-
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`
143-
144136
If you have the `Microsoft.PowerShell.IoT` module in your PSModulePath:
145137

146138
```powershell

ThirdPartyNotices.txt

-101
This file was deleted.

docs/rpi3_pin_layout.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Pin layout
22

3-
PowerShell IoT uses Wiring Pi under the hood.
4-
As such, it follow the Wiring Pi scheme for pin identification.
5-
For all of the `Pin` parameters in this module, use the WiringPi/wPi number listed in the following diagram:
3+
PowerShell IoT uses logical representation of the microcontroller's GPIOs.
4+
Refer to the microcontroller's datasheet to find this information.
5+
In case of Raspberry Pi use the BCM number listed in the following diagram:
66

77
## Image reference
88

Original file line numberDiff line numberDiff line change
@@ -1,81 +1,72 @@
11
using System;
22
using System.Collections;
3-
using System.Management.Automation; // PowerShell namespace.
3+
using System.Management.Automation;
4+
using System.Device.Gpio;
45

5-
[Cmdlet(VerbsCommon.Get, "GpioPin")]
6-
public class GetGpioPin : Cmdlet
6+
[Cmdlet(VerbsCommon.Get, "GpioPin")]
7+
public class GetGpioPin : GpioCmdletBase
78
{
8-
[Parameter(Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0)]
9-
public int[] Id { get; set; }
9+
[Parameter(Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0)]
10+
public int[] Id { get; set; }
1011

11-
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 1)]
12-
public PullMode? PullMode { get; set; }
12+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 1)]
13+
public PullMode? PullMode { get; set; }
1314

14-
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)]
15-
public SwitchParameter Raw { get; set; }
15+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)]
16+
public SwitchParameter Raw { get; set; }
1617

17-
protected override void ProcessRecord()
18-
{
19-
try
20-
{
21-
ArrayList pinList = new ArrayList();
18+
protected override void ProcessRecord()
19+
{
20+
ArrayList pinList = new ArrayList();
2221

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-
}
22+
if ((this.Id == null) || (this.Id.Length <= 0))
23+
{
24+
// TODO: this is "gpio readall" functionality
25+
// do not forget to change Id param to Mandatory = false when this is implemented
26+
}
27+
else
28+
{
29+
pinList.AddRange(this.Id);
30+
}
31+
32+
PinMode mode = PinMode.Input;
33+
if (this.PullMode.HasValue)
34+
{
35+
switch (this.PullMode.Value)
36+
{
37+
case global::PullMode.PullDown: mode = PinMode.InputPullDown; break;
38+
case global::PullMode.PullUp: mode = PinMode.InputPullUp; break;
39+
default: mode = PinMode.Input; break;
40+
};
41+
};
3442

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();
43+
foreach (int pinId in pinList)
44+
{
45+
SignalLevel slResult = SignalLevel.Low;
5346

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-
}
47+
this.EnsureOpenPin(pinId, mode);
48+
49+
if (this.GpioController.Read(pinId) == PinValue.High)
50+
{
51+
slResult = SignalLevel.High;
52+
};
53+
54+
if (this.Raw)
55+
{
56+
WriteObject(slResult);
57+
}
58+
else
59+
{
60+
GpioPinData pinData = new GpioPinData(pinId, slResult);
61+
WriteObject(pinData);
62+
}
63+
}
64+
}
7465
}
7566

7667
public enum PullMode
7768
{
78-
Off = 0,
79-
PullDown = 1,
80-
PullUp = 2
81-
}
69+
Off = 0,
70+
PullDown = 1,
71+
PullUp = 2
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Management.Automation;
3+
using System.Device.Gpio;
4+
5+
public class GpioCmdletBase : Cmdlet
6+
{
7+
protected static GpioController StaticGpioController;
8+
protected GpioController GpioController
9+
{
10+
get
11+
{
12+
if (StaticGpioController == null)
13+
{
14+
StaticGpioController = new GpioController();
15+
}
16+
return StaticGpioController;
17+
}
18+
set
19+
{
20+
StaticGpioController = value;
21+
}
22+
}
23+
24+
protected void EnsureOpenPin(int pinId, PinMode mode)
25+
{
26+
if (this.GpioController.IsPinOpen(pinId))
27+
{
28+
if (this.GpioController.GetPinMode(pinId) != mode)
29+
{
30+
this.GpioController.SetPinMode(pinId, mode);
31+
}
32+
}
33+
else
34+
{
35+
this.GpioController.OpenPin(pinId, mode);
36+
}
37+
}
38+
}
39+
40+
[Cmdlet(VerbsCommon.Clear, "GpioResources")]
41+
public class ClearGpioResources : GpioCmdletBase
42+
{
43+
protected override void ProcessRecord()
44+
{
45+
if (GpioCmdletBase.StaticGpioController != null)
46+
{
47+
GpioCmdletBase.StaticGpioController.Dispose();
48+
GpioCmdletBase.StaticGpioController = null;
49+
}
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
public class GpioPinData
22
{
3-
public int Id;
4-
public SignalLevel Value;
5-
//public Unosquare.RaspberryIO.Gpio.GpioPin PinInfo; //not in use
3+
public int Id;
4+
public SignalLevel Value;
65

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-
}
6+
public GpioPinData(int id, SignalLevel value)
7+
{
8+
this.Id = id;
9+
this.Value = value;
10+
}
1311
}
1412

1513
public enum SignalLevel
1614
{
17-
Low = 0,
18-
High = 1
19-
}
15+
Low = 0,
16+
High = 1
17+
}

0 commit comments

Comments
 (0)