This repository was archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Updates for v2 of the module #51
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
Off = 0, | ||
PullDown = 1, | ||
PullUp = 2 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System; | ||
using System.Management.Automation; | ||
using System.Device.Gpio; | ||
|
||
public class GpioCmdletBase : Cmdlet | ||
{ | ||
protected static GpioController StaticGpioController; | ||
anmenaga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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")] | ||
anmenaga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public class ClearGpioResources : GpioCmdletBase | ||
{ | ||
protected override void ProcessRecord() | ||
{ | ||
if (GpioCmdletBase.StaticGpioController != null) | ||
{ | ||
GpioCmdletBase.StaticGpioController.Dispose(); | ||
GpioCmdletBase.StaticGpioController = null; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
Low = 0, | ||
High = 1 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.