11using System ;
2+ using System . Collections ;
23using System . Management . Automation ; // PowerShell namespace.
34
45namespace Microsoft . PowerShell . IoT
@@ -7,127 +8,269 @@ public class I2CDevice
78 {
89 internal Unosquare . RaspberryIO . Gpio . I2CDevice device = null ;
910
10- public string Name { get ; set ; }
11- public int Id { get ; set ; }
11+ public string FriendlyName { get ; set ; }
12+ public int Id { get ; set ; }
1213
13- public I2CDevice ( Unosquare . RaspberryIO . Gpio . I2CDevice device , int Id , string Name )
14+ public I2CDevice ( Unosquare . RaspberryIO . Gpio . I2CDevice device , int Id , string FriendlyName )
1415 {
1516 this . device = device ;
1617 this . Id = Id ;
17- this . Name = Name ;
18+ this . FriendlyName = FriendlyName ;
1819 }
19- }
2020
21- [ Cmdlet ( VerbsCommon . New , "I2CDevice" ) ]
22- public class NewI2CDevice : Cmdlet
21+ public override string ToString ( )
22+ {
23+ if ( string . IsNullOrEmpty ( this . FriendlyName ) )
24+ {
25+ return this . Id . ToString ( ) ;
26+ }
27+ else
28+ {
29+ return this . FriendlyName ;
30+ }
31+ }
32+ }
33+
34+ public class I2CDeviceRegisterData
2335 {
24- [ Parameter ( Mandatory = false ) ]
25- public string Name { get ; set ; }
36+ public I2CDevice Device { get ; set ; }
37+ public ushort Register { get ; set ; }
38+ public byte [ ] Data { get ; set ; }
39+
40+ public I2CDeviceRegisterData ( I2CDevice device , ushort register , byte [ ] data )
41+ {
42+ this . Device = device ;
43+ this . Register = register ;
44+ this . Data = data ;
45+ }
46+
47+ public I2CDeviceRegisterData ( I2CDevice device , ushort register )
48+ : this ( device , register , new byte [ 0 ] )
49+ {
50+ }
51+
52+ public I2CDeviceRegisterData ( )
53+ : this ( null , 0 )
54+ {
55+ }
56+ }
57+
58+ public enum SignalLevel
59+ {
60+ Low = 0 ,
61+ High = 1
62+ }
63+
64+ public enum PullMode
65+ {
66+ Off = 0 ,
67+ PullDown = 1 ,
68+ PullUp = 2
69+ }
70+
71+ public class GpioPinData
72+ {
73+ public int Id ;
74+ public SignalLevel Value ;
75+ public Unosquare . RaspberryIO . Gpio . GpioPin PinInfo ;
76+
77+ public GpioPinData ( int id , SignalLevel value , Unosquare . RaspberryIO . Gpio . GpioPin pinInfo )
78+ {
79+ this . Id = id ;
80+ this . Value = value ;
81+ this . PinInfo = pinInfo ;
82+ }
83+ }
84+
85+ [ Cmdlet ( VerbsCommon . Get , "I2CDevice" ) ]
86+ public class GetI2CDevice : Cmdlet
87+ {
88+ [ Parameter ( Mandatory = true , ValueFromPipelineByPropertyName = true , Position = 0 ) ]
89+ public int Id { get ; set ; }
90+
91+ [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = true , Position = 1 ) ]
92+ public string FriendlyName { get ; set ; }
93+
94+ public GetI2CDevice ( )
95+ {
96+ this . FriendlyName = string . Empty ;
97+ this . Id = 0 ;
98+ }
2699
27- [ Parameter ( Mandatory = true ) ]
28- public int Id { get ; set ; }
29-
30100 protected override void ProcessRecord ( )
31101 {
32- WriteObject ( new I2CDevice ( Unosquare . RaspberryIO . Pi . I2C . AddDevice ( this . Id ) , this . Id , this . Name ) ) ;
102+ WriteObject ( new I2CDevice ( Unosquare . RaspberryIO . Pi . I2C . AddDevice ( this . Id ) , this . Id , this . FriendlyName ) ) ;
33103 }
34104 }
35105
36- [ Cmdlet ( VerbsCommunications . Read , "I2CRegister" ) ]
37- public class ReadI2CRegister : Cmdlet
106+ [ Cmdlet ( VerbsCommon . Get , "I2CRegister" ) ]
107+ public class GetI2CRegister : Cmdlet
38108 {
39- [ Parameter ( Mandatory = true , ValueFromPipeline = true ) ]
109+ [ Parameter ( Mandatory = true , ValueFromPipeline = true , ValueFromPipelineByPropertyName = true , Position = 0 ) ]
40110 public I2CDevice Device { get ; set ; }
41111
42- [ Parameter ( Mandatory = true ) ]
43- public byte Register { get ; set ; }
112+ [ Parameter ( Mandatory = true , ValueFromPipelineByPropertyName = true , Position = 1 ) ]
113+ public ushort Register { get ; set ; }
44114
45- [ Parameter ( Mandatory = false ) ]
46- public byte Length { get ; set ; }
115+ [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = true , Position = 2 ) ]
116+ public byte ByteCount { get ; set ; }
47117
48- public ReadI2CRegister ( )
118+ [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = true ) ]
119+ public SwitchParameter Raw { get ; set ; }
120+
121+ public GetI2CRegister ( )
49122 {
50- this . Length = 1 ;
123+ this . ByteCount = 1 ;
124+ this . Raw = false ;
51125 }
52126
53127 protected override void ProcessRecord ( )
54128 {
55- if ( this . Length > 1 )
129+ if ( this . ByteCount > 1 )
56130 {
57- this . Device . device . Write ( this . Register ) ;
58- WriteObject ( this . Device . device . Read ( this . Length ) ) ;
131+ this . Device . device . Write ( ( byte ) this . Register ) ;
132+ byte [ ] value = this . Device . device . Read ( this . ByteCount ) ;
133+ if ( this . Raw )
134+ {
135+ WriteObject ( value ) ;
136+ }
137+ else
138+ {
139+ I2CDeviceRegisterData result = new I2CDeviceRegisterData ( this . Device , this . Register ) ;
140+ result . Data = value ;
141+ WriteObject ( result ) ;
142+ }
59143 }
60144 else
61- {
62- WriteObject ( this . Device . device . ReadAddressByte ( this . Register ) ) ;
145+ {
146+ byte value = this . Device . device . ReadAddressByte ( this . Register ) ;
147+ if ( this . Raw )
148+ {
149+ WriteObject ( value ) ;
150+ }
151+ else
152+ {
153+ I2CDeviceRegisterData result = new I2CDeviceRegisterData ( this . Device , this . Register , new byte [ 1 ] { value } ) ;
154+ WriteObject ( result ) ;
155+ }
63156 }
64157 }
65158 }
66159
67- [ Cmdlet ( VerbsCommunications . Write , "I2CRegister" ) ]
68- public class WriteI2CRegister : Cmdlet
160+ [ Cmdlet ( VerbsCommon . Set , "I2CRegister" ) ]
161+ public class SetI2CRegister : Cmdlet
69162 {
70- [ Parameter ( Mandatory = true , ValueFromPipeline = true ) ]
163+ [ Parameter ( Mandatory = true , ValueFromPipeline = true , ValueFromPipelineByPropertyName = true , Position = 0 ) ]
71164 public I2CDevice Device { get ; set ; }
72165
73- [ Parameter ( Mandatory = true ) ]
74- public byte Register { get ; set ; }
166+ [ Parameter ( Mandatory = true , ValueFromPipelineByPropertyName = true , Position = 1 ) ]
167+ public ushort Register { get ; set ; }
168+
169+ [ Parameter ( Mandatory = true , ValueFromPipelineByPropertyName = true , Position = 2 ) ]
170+ public byte [ ] Data { get ; set ; }
75171
76- [ Parameter ( Mandatory = true ) ]
77- public byte Value { get ; set ; }
172+ [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = true ) ]
173+ public SwitchParameter PassThru { get ; set ; }
78174
79175 protected override void ProcessRecord ( )
80176 {
81- this . Device . device . WriteAddressByte ( this . Register , this . Value ) ;
177+ this . Device . device . WriteAddressByte ( this . Register , this . Data [ 0 ] ) ;
178+ if ( this . PassThru )
179+ {
180+ I2CDeviceRegisterData result = new I2CDeviceRegisterData ( this . Device , this . Register , this . Data ) ;
181+ WriteObject ( result ) ;
182+ }
82183 }
83184 }
84185
85186 [ Cmdlet ( VerbsCommon . Set , "GpioPin" ) ]
86187 public class SetGpioPin : Cmdlet
87188 {
88- [ Parameter ( Mandatory = true ) ]
89- public int Pin { get ; set ; }
189+ [ Parameter ( Mandatory = true , ValueFromPipeline = true , ValueFromPipelineByPropertyName = true , Position = 0 ) ]
190+ public int [ ] Id { get ; set ; }
191+
192+ [ Parameter ( Mandatory = true , ValueFromPipelineByPropertyName = true , Position = 1 ) ]
193+ public SignalLevel Value { get ; set ; }
90194
91- [ Parameter ( Mandatory = true ) ]
92- public bool Value { get ; set ; }
195+ [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = true ) ]
196+ public SwitchParameter PassThru { get ; set ; }
93197
94198 protected override void ProcessRecord ( )
95199 {
96- var pin = Unosquare . RaspberryIO . Pi . Gpio [ this . Pin ] ;
97- pin . PinMode = Unosquare . RaspberryIO . Gpio . GpioPinDriveMode . Output ;
98- pin . Write ( this . Value ) ;
200+ if ( this . Id != null )
201+ {
202+ foreach ( int pinId in this . Id )
203+ {
204+ var pin = Unosquare . RaspberryIO . Pi . Gpio [ pinId ] ;
205+ pin . PinMode = Unosquare . RaspberryIO . Gpio . GpioPinDriveMode . Output ;
206+ pin . Write ( ( Unosquare . RaspberryIO . Gpio . GpioPinValue ) this . Value ) ;
207+ if ( this . PassThru )
208+ {
209+ GpioPinData pinData = new GpioPinData ( pinId , this . Value , pin ) ;
210+ WriteObject ( pinData ) ;
211+ }
212+ }
213+ }
99214 }
100215 }
101216
102217 [ Cmdlet ( VerbsCommon . Get , "GpioPin" ) ]
103218 public class GetGpioPin : Cmdlet
104219 {
105- [ Parameter ( Mandatory = false ) ]
106- public int ? Pin { get ; set ; }
220+ [ Parameter ( Mandatory = false , ValueFromPipeline = true , ValueFromPipelineByPropertyName = true , Position = 0 ) ]
221+ public int [ ] Id { get ; set ; }
222+
223+ [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = true , Position = 1 ) ]
224+ public PullMode ? PullMode { get ; set ; }
107225
108- [ Parameter ( Mandatory = false ) ]
109- public Unosquare . RaspberryIO . Gpio . GpioPinResistorPullMode ? PullMode { get ; set ; }
226+ [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = true ) ]
227+ public SwitchParameter Raw { get ; set ; }
110228
111229 protected override void ProcessRecord ( )
112230 {
113- if ( this . Pin . HasValue )
114- {
115- var pin = Unosquare . RaspberryIO . Pi . Gpio [ this . Pin . Value ] ;
116- pin . PinMode = Unosquare . RaspberryIO . Gpio . GpioPinDriveMode . Input ;
117- if ( this . PullMode . HasValue )
231+ ArrayList pinList = new ArrayList ( ) ;
232+
233+ if ( ( this . Id == null ) || ( this . Id . Length <= 0 ) )
234+ {
235+ foreach ( var pin in Unosquare . RaspberryIO . Pi . Gpio . Pins )
118236 {
119- pin . InputPullMode = this . PullMode . Value ;
120- } ;
121- bool value = pin . Read ( ) ;
122- WriteObject ( value ) ;
237+ pinList . Add ( pin . PinNumber ) ;
238+ }
123239 }
124240 else
125- {
126- foreach ( var pin in Unosquare . RaspberryIO . Pi . Gpio . Pins )
127- {
128- WriteObject ( pin ) ;
129- }
241+ {
242+ pinList . AddRange ( this . Id ) ;
130243 }
244+
245+ foreach ( int pinId in pinList )
246+ {
247+ var pin = Unosquare . RaspberryIO . Pi . Gpio [ pinId ] ;
248+ try
249+ {
250+ pin . PinMode = Unosquare . RaspberryIO . Gpio . GpioPinDriveMode . Input ;
251+ if ( this . PullMode . HasValue )
252+ {
253+ pin . InputPullMode = ( Unosquare . RaspberryIO . Gpio . GpioPinResistorPullMode ) this . PullMode . Value ;
254+ } ;
255+ }
256+ catch ( System . NotSupportedException )
257+ {
258+ // We want to avoid errors like
259+ // System.NotSupportedException : Get - GpioPin : Pin Pin15 'BCM 14 (UART Transmit)' does not support mode 'Input'.Pin capabilities are limited to: UARTTXD
260+ // at the same time we need to return PinInfo for such pins, so we need to continue processing
261+ }
262+ bool pinBoolValue = pin . Read ( ) ;
263+
264+ if ( this . Raw )
265+ {
266+ WriteObject ( pinBoolValue ? SignalLevel . High : SignalLevel . Low ) ;
267+ }
268+ else
269+ {
270+ GpioPinData pinData = new GpioPinData ( pinId , pinBoolValue ? SignalLevel . High : SignalLevel . Low , pin ) ;
271+ WriteObject ( pinData ) ;
272+ }
273+ }
131274 }
132275 }
133276}
0 commit comments