1
+ using System ;
2
+ using System . Collections ;
3
+ using System . Management . Automation ; // PowerShell namespace.
4
+
5
+ [ Cmdlet ( VerbsCommon . Get , "GpioPin" ) ]
6
+ public class GetGpioPin : Cmdlet
7
+ {
8
+ [ Parameter ( Mandatory = false , ValueFromPipeline = true , ValueFromPipelineByPropertyName = true , Position = 0 ) ]
9
+ public int [ ] Id { get ; set ; }
10
+
11
+ [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = true , Position = 1 ) ]
12
+ public PullMode ? PullMode { get ; set ; }
13
+
14
+ [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = true ) ]
15
+ public SwitchParameter Raw { get ; set ; }
16
+
17
+ protected override void ProcessRecord ( )
18
+ {
19
+ try
20
+ {
21
+ ArrayList pinList = new ArrayList ( ) ;
22
+
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
+ }
34
+
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 ( ) ;
53
+
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
+ }
74
+ }
75
+
76
+ public enum PullMode
77
+ {
78
+ Off = 0 ,
79
+ PullDown = 1 ,
80
+ PullUp = 2
81
+ }
0 commit comments