Skip to content

Commit 9ca76b8

Browse files
author
Andrew
committed
Initial updates for v2
1 parent c1f04d7 commit 9ca76b8

File tree

8 files changed

+77
-74
lines changed

8 files changed

+77
-74
lines changed

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

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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

56
[Cmdlet(VerbsCommon.Get, "GpioPin")]
67
public class GetGpioPin : Cmdlet
@@ -16,60 +17,55 @@ public class GetGpioPin : Cmdlet
1617

1718
protected override void ProcessRecord()
1819
{
19-
try
20+
ArrayList pinList = new ArrayList();
21+
22+
if ((this.Id == null) || (this.Id.Length <= 0))
2023
{
21-
ArrayList pinList = new ArrayList();
24+
// this is "gpio readall" functionality
2225

23-
if ((this.Id == null) || (this.Id.Length <= 0))
26+
/*foreach (var pin in Unosquare.RaspberryIO.Pi.Gpio.Pins)
2427
{
25-
foreach (var pin in Unosquare.RaspberryIO.Pi.Gpio.Pins)
26-
{
27-
pinList.Add(pin.PinNumber);
28-
}
29-
}
30-
else
28+
pinList.Add(pin.PinNumber);
29+
}*/
30+
}
31+
else
32+
{
33+
pinList.AddRange(this.Id);
34+
}
35+
36+
using (GpioController controller = new GpioController())
37+
{
38+
PinMode mode = PinMode.Input;
39+
if (this.PullMode.HasValue)
3140
{
32-
pinList.AddRange(this.Id);
33-
}
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+
};
3448

3549
foreach (int pinId in pinList)
3650
{
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)
51+
SignalLevel slResult = SignalLevel.Low;
52+
controller.OpenPin(pinId, mode); // pin will be closed in GpioController.Dispose()
53+
if (controller.Read(pinId) == PinValue.High)
4754
{
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();
53-
55+
slResult = SignalLevel.High;
56+
};
57+
5458
if (this.Raw)
5559
{
56-
WriteObject(pinBoolValue ? SignalLevel.High : SignalLevel.Low);
60+
WriteObject(slResult);
5761
}
5862
else
5963
{
60-
GpioPinData pinData = new GpioPinData(pinId, pinBoolValue ? SignalLevel.High : SignalLevel.Low, pin);
64+
GpioPinData pinData = new GpioPinData(pinId, slResult);
6165
WriteObject(pinData);
6266
}
6367
}
6468
}
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-
}
7369
}
7470
}
7571

src/Microsoft.PowerShell.IoT/Gpio/GpioPinData.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ public class GpioPinData
22
{
33
public int Id;
44
public SignalLevel Value;
5-
//public Unosquare.RaspberryIO.Gpio.GpioPin PinInfo; //not in use
65

7-
public GpioPinData(int id, SignalLevel value, Unosquare.RaspberryIO.Gpio.GpioPin pinInfo)
6+
public GpioPinData(int id, SignalLevel value)
87
{
98
this.Id = id;
109
this.Value = value;
11-
//this.PinInfo = pinInfo;
1210
}
1311
}
1412

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
2-
using System.Management.Automation; // PowerShell namespace.
2+
using System.Management.Automation;
3+
using System.Device.Gpio;
34

45
[Cmdlet(VerbsCommon.Set, "GpioPin")]
56
public class SetGpioPin : Cmdlet
@@ -15,30 +16,30 @@ public class SetGpioPin : Cmdlet
1516

1617
protected override void ProcessRecord()
1718
{
18-
try
19+
if (this.Id != null)
1920
{
20-
if (this.Id != null)
21-
{
21+
GpioController controller = new GpioController();
22+
//using (GpioController controller = new GpioController())
23+
//{
2224
foreach (int pinId in this.Id)
2325
{
24-
var pin = Unosquare.RaspberryIO.Pi.Gpio[pinId];
25-
pin.PinMode = Unosquare.RaspberryIO.Gpio.GpioPinDriveMode.Output;
26-
pin.Write((Unosquare.RaspberryIO.Gpio.GpioPinValue)this.Value);
26+
controller.OpenPin(pinId, PinMode.Output); // pin will be closed in GpioController.Dispose()
27+
if(this.Value == SignalLevel.Low)
28+
{
29+
controller.Write(pinId, PinValue.Low);
30+
}
31+
else
32+
{
33+
controller.Write(pinId, PinValue.High);
34+
}
35+
2736
if (this.PassThru)
2837
{
29-
GpioPinData pinData = new GpioPinData(pinId, this.Value, pin);
38+
GpioPinData pinData = new GpioPinData(pinId, this.Value);
3039
WriteObject(pinData);
3140
}
3241
}
33-
}
34-
}
35-
catch (System.TypeInitializationException e) // Unosquare.RaspberryIO.Gpio.GpioController.Initialize throws this TypeInitializationException
36-
{
37-
if (!Unosquare.RaspberryIO.Computer.SystemInfo.Instance.IsRunningAsRoot)
38-
{
39-
throw new PlatformNotSupportedException(Resources.ErrNeedRootPrivileges, e);
40-
}
41-
throw;
42+
//}
4243
}
4344
}
4445
}
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System;
2-
using System.Management.Automation; // PowerShell namespace.
2+
using System.Management.Automation;
33
using System.Device.I2c;
44

55
[Cmdlet(VerbsCommon.Get, "I2CDevice")]
@@ -11,17 +11,20 @@ public class GetI2CDevice : Cmdlet
1111
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 1)]
1212
public string FriendlyName { get; set; }
1313

14+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 2)]
15+
public int BusId { get; set; }
16+
1417
public GetI2CDevice()
1518
{
1619
this.FriendlyName = string.Empty;
1720
this.Id = 0;
21+
this.BusId = 1;
1822
}
1923

2024
protected override void ProcessRecord()
2125
{
22-
var settings = new I2cConnectionSettings(1, this.Id);
26+
var settings = new I2cConnectionSettings(this.BusId, this.Id);
2327
I2cDevice device = I2cDevice.Create(settings);
24-
WriteObject(new I2CDevice(device, this.Id, this.FriendlyName));
25-
28+
WriteObject(new I2CDevice(device, this.Id, this.FriendlyName, this.BusId));
2629
}
2730
}

src/Microsoft.PowerShell.IoT/I2c/I2cDevice.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ public class I2CDevice
44

55
public string FriendlyName { get; set; }
66
public int Id { get; set; }
7+
public int BusId { get; set; }
78

8-
public I2CDevice(System.Device.I2c.I2cDevice device, int Id, string FriendlyName)
9+
public I2CDevice(System.Device.I2c.I2cDevice device, int Id, string FriendlyName, int BusId)
910
{
1011
this.device = device;
1112
this.Id = Id;
1213
this.FriendlyName = FriendlyName;
14+
this.BusId = BusId;
1315
}
1416

1517
public override string ToString()
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp2.0</TargetFramework>
5-
<Version>0.1.1.0</Version>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
<Version>0.2.0.0</Version>
66
<Authors>Microsoft Corporation</Authors>
77
<Company>Microsoft Corporation</Company>
88
<Product>Microsoft.PowerShell.IoT</Product>
99
<PackageId>Microsoft.PowerShell.IoT</PackageId>
10-
<AssemblyVersion>0.1.1.0</AssemblyVersion>
11-
<FileVersion>0.1.1.0</FileVersion>
10+
<AssemblyVersion>0.2.0.0</AssemblyVersion>
11+
<FileVersion>0.2.0.0</FileVersion>
1212
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
1313
<PackageLicenseUrl>https://github.com/PowerShell/PowerShell-IoT/blob/master/LICENSE.txt</PackageLicenseUrl>
1414
<PackageProjectUrl>https://github.com/PowerShell/PowerShell-IoT</PackageProjectUrl>
@@ -17,8 +17,7 @@
1717

1818
<ItemGroup>
1919
<PackageReference Include="System.Device.Gpio" Version="1.0.0" />
20-
<PackageReference Include="System.Management.Automation" Version="6.0.0-rc.2" />
21-
<PackageReference Include="Unosquare.Raspberry.IO" Version="0.14.0" />
20+
<PackageReference Include="System.Management.Automation" Version="7.0.0" />
2221
</ItemGroup>
2322

2423
</Project>

src/Microsoft.PowerShell.IoT/Microsoft.PowerShell.IoT.psd1

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
CompanyName="Microsoft Corporation"
88
Copyright="© Microsoft Corporation. All rights reserved."
99
Description='A PowerShell module for interacting with hardware sensors and devices using common protocols: GPIO, I2C & SPI.'
10-
ModuleVersion="0.1.1"
10+
ModuleVersion="0.2.0"
1111
FunctionsToExport = '*'
1212
CmdletsToExport = '*'
1313
AliasesToExport = @()
@@ -29,7 +29,11 @@
2929
# IconUri = ''
3030

3131
# ReleaseNotes of this module
32-
ReleaseNotes = '## 0.1.1
32+
ReleaseNotes = '## 0.2.0
33+
34+
Started using System.Device.Gpio
35+
36+
## 0.1.1
3337
3438
Minor bug fixes
3539

src/Microsoft.PowerShell.IoT/SPI/Send/SendSPIData.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ public class SendSPIData : Cmdlet
1818
public SendSPIData()
1919
{
2020
this.Channel = 0;
21-
this.Frequency = Unosquare.RaspberryIO.Gpio.SpiChannel.MinFrequency;
21+
//this.Frequency = Unosquare.RaspberryIO.Gpio.SpiChannel.MinFrequency;
2222
}
2323

2424
protected override void ProcessRecord()
25-
{
25+
{/*
2626
try
2727
{
2828
var spiChannel = Unosquare.RaspberryIO.Pi.Spi.Channel0;
@@ -54,6 +54,6 @@ protected override void ProcessRecord()
5454
throw new PlatformNotSupportedException(Resources.ErrNeedRootPrivileges, e);
5555
}
5656
throw;
57-
}
57+
}*/
5858
}
5959
}

0 commit comments

Comments
 (0)