1
1
using System ;
2
+ using System . Collections ;
2
3
using System . Management . Automation ; // PowerShell namespace.
3
4
4
5
namespace Microsoft . PowerShell . IoT
@@ -7,127 +8,269 @@ public class I2CDevice
7
8
{
8
9
internal Unosquare . RaspberryIO . Gpio . I2CDevice device = null ;
9
10
10
- public string Name { get ; set ; }
11
- public int Id { get ; set ; }
11
+ public string FriendlyName { get ; set ; }
12
+ public int Id { get ; set ; }
12
13
13
- public I2CDevice ( Unosquare . RaspberryIO . Gpio . I2CDevice device , int Id , string Name )
14
+ public I2CDevice ( Unosquare . RaspberryIO . Gpio . I2CDevice device , int Id , string FriendlyName )
14
15
{
15
16
this . device = device ;
16
17
this . Id = Id ;
17
- this . Name = Name ;
18
+ this . FriendlyName = FriendlyName ;
18
19
}
19
- }
20
20
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
23
35
{
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
+ }
26
99
27
- [ Parameter ( Mandatory = true ) ]
28
- public int Id { get ; set ; }
29
-
30
100
protected override void ProcessRecord ( )
31
101
{
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 ) ) ;
33
103
}
34
104
}
35
105
36
- [ Cmdlet ( VerbsCommunications . Read , "I2CRegister" ) ]
37
- public class ReadI2CRegister : Cmdlet
106
+ [ Cmdlet ( VerbsCommon . Get , "I2CRegister" ) ]
107
+ public class GetI2CRegister : Cmdlet
38
108
{
39
- [ Parameter ( Mandatory = true , ValueFromPipeline = true ) ]
109
+ [ Parameter ( Mandatory = true , ValueFromPipeline = true , ValueFromPipelineByPropertyName = true , Position = 0 ) ]
40
110
public I2CDevice Device { get ; set ; }
41
111
42
- [ Parameter ( Mandatory = true ) ]
43
- public byte Register { get ; set ; }
112
+ [ Parameter ( Mandatory = true , ValueFromPipelineByPropertyName = true , Position = 1 ) ]
113
+ public ushort Register { get ; set ; }
44
114
45
- [ Parameter ( Mandatory = false ) ]
46
- public byte Length { get ; set ; }
115
+ [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = true , Position = 2 ) ]
116
+ public byte ByteCount { get ; set ; }
47
117
48
- public ReadI2CRegister ( )
118
+ [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = true ) ]
119
+ public SwitchParameter Raw { get ; set ; }
120
+
121
+ public GetI2CRegister ( )
49
122
{
50
- this . Length = 1 ;
123
+ this . ByteCount = 1 ;
124
+ this . Raw = false ;
51
125
}
52
126
53
127
protected override void ProcessRecord ( )
54
128
{
55
- if ( this . Length > 1 )
129
+ if ( this . ByteCount > 1 )
56
130
{
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
+ }
59
143
}
60
144
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
+ }
63
156
}
64
157
}
65
158
}
66
159
67
- [ Cmdlet ( VerbsCommunications . Write , "I2CRegister" ) ]
68
- public class WriteI2CRegister : Cmdlet
160
+ [ Cmdlet ( VerbsCommon . Set , "I2CRegister" ) ]
161
+ public class SetI2CRegister : Cmdlet
69
162
{
70
- [ Parameter ( Mandatory = true , ValueFromPipeline = true ) ]
163
+ [ Parameter ( Mandatory = true , ValueFromPipeline = true , ValueFromPipelineByPropertyName = true , Position = 0 ) ]
71
164
public I2CDevice Device { get ; set ; }
72
165
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 ; }
75
171
76
- [ Parameter ( Mandatory = true ) ]
77
- public byte Value { get ; set ; }
172
+ [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = true ) ]
173
+ public SwitchParameter PassThru { get ; set ; }
78
174
79
175
protected override void ProcessRecord ( )
80
176
{
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
+ }
82
183
}
83
184
}
84
185
85
186
[ Cmdlet ( VerbsCommon . Set , "GpioPin" ) ]
86
187
public class SetGpioPin : Cmdlet
87
188
{
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 ; }
90
194
91
- [ Parameter ( Mandatory = true ) ]
92
- public bool Value { get ; set ; }
195
+ [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = true ) ]
196
+ public SwitchParameter PassThru { get ; set ; }
93
197
94
198
protected override void ProcessRecord ( )
95
199
{
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
+ }
99
214
}
100
215
}
101
216
102
217
[ Cmdlet ( VerbsCommon . Get , "GpioPin" ) ]
103
218
public class GetGpioPin : Cmdlet
104
219
{
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 ; }
107
225
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 ; }
110
228
111
229
protected override void ProcessRecord ( )
112
230
{
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 )
118
236
{
119
- pin . InputPullMode = this . PullMode . Value ;
120
- } ;
121
- bool value = pin . Read ( ) ;
122
- WriteObject ( value ) ;
237
+ pinList . Add ( pin . PinNumber ) ;
238
+ }
123
239
}
124
240
else
125
- {
126
- foreach ( var pin in Unosquare . RaspberryIO . Pi . Gpio . Pins )
127
- {
128
- WriteObject ( pin ) ;
129
- }
241
+ {
242
+ pinList . AddRange ( this . Id ) ;
130
243
}
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
+ }
131
274
}
132
275
}
133
276
}
0 commit comments