Skip to content

Commit 96a27bb

Browse files
author
Andrew
committed
Changed to using System.Device APIs
1 parent 9ca76b8 commit 96a27bb

14 files changed

+340
-407
lines changed

README.md

Lines changed: 3 additions & 11 deletions
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

@@ -133,14 +130,9 @@ _NOTE: If you'd rather not use the script, simply copy the `out/Microsoft.PowerS
133130
First, you must run pwsh with sudo:
134131

135132
```powershell
136-
sudo WIRINGPI_CODES=1 pwsh
133+
sudo 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

Lines changed: 0 additions & 101 deletions
This file was deleted.

docs/rpi3_pin_layout.md

Lines changed: 3 additions & 3 deletions
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

src/Microsoft.PowerShell.IoT/Gpio/Get/GetGpioPin.cs

Lines changed: 63 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,75 +3,79 @@
33
using System.Management.Automation;
44
using System.Device.Gpio;
55

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

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

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

18-
protected override void ProcessRecord()
19-
{
20-
ArrayList pinList = new ArrayList();
18+
protected override void ProcessRecord()
19+
{
20+
ArrayList pinList = new ArrayList();
2121

22-
if ((this.Id == null) || (this.Id.Length <= 0))
23-
{
24-
// this is "gpio readall" functionality
25-
26-
/*foreach (var pin in Unosquare.RaspberryIO.Pi.Gpio.Pins)
27-
{
28-
pinList.Add(pin.PinNumber);
29-
}*/
30-
}
31-
else
32-
{
33-
pinList.AddRange(this.Id);
34-
}
22+
if ((this.Id == null) || (this.Id.Length <= 0))
23+
{
24+
// TODO: this is "gpio readall" functionality
25+
}
26+
else
27+
{
28+
pinList.AddRange(this.Id);
29+
}
30+
31+
PinMode mode = PinMode.Input;
32+
if (this.PullMode.HasValue)
33+
{
34+
switch (this.PullMode.Value)
35+
{
36+
case global::PullMode.PullDown: mode = PinMode.InputPullDown; break;
37+
case global::PullMode.PullUp: mode = PinMode.InputPullUp; break;
38+
default: mode = PinMode.Input; break;
39+
};
40+
};
3541

36-
using (GpioController controller = new GpioController())
42+
foreach (int pinId in pinList)
3743
{
38-
PinMode mode = PinMode.Input;
39-
if (this.PullMode.HasValue)
40-
{
41-
switch (this.PullMode.Value)
42-
{
43-
case global::PullMode.PullDown: mode = PinMode.InputPullDown; break;
44-
case global::PullMode.PullUp: mode = PinMode.InputPullUp; break;
45-
default: mode = PinMode.Input; break;
46-
};
47-
};
44+
SignalLevel slResult = SignalLevel.Low;
45+
46+
if (this.GpioController.IsPinOpen(pinId))
47+
{
48+
if (this.GpioController.GetPinMode(pinId) != mode)
49+
{
50+
this.GpioController.SetPinMode(pinId, mode);
51+
}
52+
}
53+
else
54+
{
55+
this.GpioController.OpenPin(pinId, mode);
56+
}
4857

49-
foreach (int pinId in pinList)
50-
{
51-
SignalLevel slResult = SignalLevel.Low;
52-
controller.OpenPin(pinId, mode); // pin will be closed in GpioController.Dispose()
53-
if (controller.Read(pinId) == PinValue.High)
54-
{
55-
slResult = SignalLevel.High;
56-
};
57-
58-
if (this.Raw)
59-
{
60-
WriteObject(slResult);
61-
}
62-
else
63-
{
64-
GpioPinData pinData = new GpioPinData(pinId, slResult);
65-
WriteObject(pinData);
66-
}
67-
}
68-
}
69-
}
58+
if (this.GpioController.Read(pinId) == PinValue.High)
59+
{
60+
slResult = SignalLevel.High;
61+
};
62+
63+
if (this.Raw)
64+
{
65+
WriteObject(slResult);
66+
}
67+
else
68+
{
69+
GpioPinData pinData = new GpioPinData(pinId, slResult);
70+
WriteObject(pinData);
71+
}
72+
}
73+
}
7074
}
7175

7276
public enum PullMode
7377
{
74-
Off = 0,
75-
PullDown = 1,
76-
PullUp = 2
78+
Off = 0,
79+
PullDown = 1,
80+
PullUp = 2
7781
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
25+
[Cmdlet(VerbsCommon.Clear, "GpioResources")]
26+
public class ClearGpioResources : GpioCmdletBase
27+
{
28+
protected override void ProcessRecord()
29+
{
30+
if (GpioCmdletBase.StaticGpioController != null)
31+
{
32+
GpioCmdletBase.StaticGpioController.Dispose();
33+
GpioCmdletBase.StaticGpioController = null;
34+
}
35+
}
36+
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
public class GpioPinData
22
{
3-
public int Id;
4-
public SignalLevel Value;
3+
public int Id;
4+
public SignalLevel Value;
55

6-
public GpioPinData(int id, SignalLevel value)
7-
{
8-
this.Id = id;
9-
this.Value = value;
10-
}
6+
public GpioPinData(int id, SignalLevel value)
7+
{
8+
this.Id = id;
9+
this.Value = value;
10+
}
1111
}
1212

1313
public enum SignalLevel
1414
{
15-
Low = 0,
16-
High = 1
15+
Low = 0,
16+
High = 1
1717
}

0 commit comments

Comments
 (0)