1
+ // Copyright (c) Microsoft Corporation. All rights reserved.
2
+ // Licensed under the MIT License.
3
+
4
+ using System ;
5
+ using System . Collections ;
6
+ using System . Management . Automation ; // PowerShell namespace.
7
+
8
+ namespace Microsoft . PowerShell . IoT
9
+ {
10
+ public class I2CDevice
11
+ {
12
+ internal Unosquare . RaspberryIO . Gpio . I2CDevice device = null ;
13
+
14
+ public string FriendlyName { get ; set ; }
15
+ public int Id { get ; set ; }
16
+
17
+ public I2CDevice ( Unosquare . RaspberryIO . Gpio . I2CDevice device , int Id , string FriendlyName )
18
+ {
19
+ this . device = device ;
20
+ this . Id = Id ;
21
+ this . FriendlyName = FriendlyName ;
22
+ }
23
+
24
+ public override string ToString ( )
25
+ {
26
+ if ( string . IsNullOrEmpty ( this . FriendlyName ) )
27
+ {
28
+ return this . Id . ToString ( ) ;
29
+ }
30
+ else
31
+ {
32
+ return this . FriendlyName ;
33
+ }
34
+ }
35
+ }
36
+
37
+ public class I2CDeviceRegisterData
38
+ {
39
+ public I2CDevice Device { get ; set ; }
40
+ public ushort Register { get ; set ; }
41
+ public byte [ ] Data { get ; set ; }
42
+
43
+ public I2CDeviceRegisterData ( I2CDevice device , ushort register , byte [ ] data )
44
+ {
45
+ this . Device = device ;
46
+ this . Register = register ;
47
+ this . Data = data ;
48
+ }
49
+
50
+ public I2CDeviceRegisterData ( I2CDevice device , ushort register )
51
+ : this ( device , register , new byte [ 0 ] )
52
+ {
53
+ }
54
+
55
+ public I2CDeviceRegisterData ( )
56
+ : this ( null , 0 )
57
+ {
58
+ }
59
+ }
60
+
61
+ public enum SignalLevel
62
+ {
63
+ Low = 0 ,
64
+ High = 1
65
+ }
66
+
67
+ public enum PullMode
68
+ {
69
+ Off = 0 ,
70
+ PullDown = 1 ,
71
+ PullUp = 2
72
+ }
73
+
74
+ public class GpioPinData
75
+ {
76
+ public int Id ;
77
+ public SignalLevel Value ;
78
+ public Unosquare . RaspberryIO . Gpio . GpioPin PinInfo ;
79
+
80
+ public GpioPinData ( int id , SignalLevel value , Unosquare . RaspberryIO . Gpio . GpioPin pinInfo )
81
+ {
82
+ this . Id = id ;
83
+ this . Value = value ;
84
+ this . PinInfo = pinInfo ;
85
+ }
86
+ }
87
+
88
+ [ Cmdlet ( VerbsCommon . Get , "I2CDevice" ) ]
89
+ public class GetI2CDevice : Cmdlet
90
+ {
91
+ [ Parameter ( Mandatory = true , ValueFromPipelineByPropertyName = true , Position = 0 ) ]
92
+ public int Id { get ; set ; }
93
+
94
+ [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = true , Position = 1 ) ]
95
+ public string FriendlyName { get ; set ; }
96
+
97
+ public GetI2CDevice ( )
98
+ {
99
+ this . FriendlyName = string . Empty ;
100
+ this . Id = 0 ;
101
+ }
102
+
103
+ protected override void ProcessRecord ( )
104
+ {
105
+ WriteObject ( new I2CDevice ( Unosquare . RaspberryIO . Pi . I2C . AddDevice ( this . Id ) , this . Id , this . FriendlyName ) ) ;
106
+ }
107
+ }
108
+
109
+ [ Cmdlet ( VerbsCommon . Get , "I2CRegister" ) ]
110
+ public class GetI2CRegister : Cmdlet
111
+ {
112
+ [ Parameter ( Mandatory = true , ValueFromPipeline = true , ValueFromPipelineByPropertyName = true , Position = 0 ) ]
113
+ public I2CDevice Device { get ; set ; }
114
+
115
+ [ Parameter ( Mandatory = true , ValueFromPipelineByPropertyName = true , Position = 1 ) ]
116
+ public ushort Register { get ; set ; }
117
+
118
+ [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = true , Position = 2 ) ]
119
+ public byte ByteCount { get ; set ; }
120
+
121
+ [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = true ) ]
122
+ public SwitchParameter Raw { get ; set ; }
123
+
124
+ public GetI2CRegister ( )
125
+ {
126
+ this . ByteCount = 1 ;
127
+ this . Raw = false ;
128
+ }
129
+
130
+ protected override void ProcessRecord ( )
131
+ {
132
+ if ( this . ByteCount > 1 )
133
+ {
134
+ this . Device . device . Write ( ( byte ) this . Register ) ;
135
+ byte [ ] value = this . Device . device . Read ( this . ByteCount ) ;
136
+ if ( this . Raw )
137
+ {
138
+ WriteObject ( value ) ;
139
+ }
140
+ else
141
+ {
142
+ I2CDeviceRegisterData result = new I2CDeviceRegisterData ( this . Device , this . Register ) ;
143
+ result . Data = value ;
144
+ WriteObject ( result ) ;
145
+ }
146
+ }
147
+ else
148
+ {
149
+ byte value = this . Device . device . ReadAddressByte ( this . Register ) ;
150
+ if ( this . Raw )
151
+ {
152
+ WriteObject ( value ) ;
153
+ }
154
+ else
155
+ {
156
+ I2CDeviceRegisterData result = new I2CDeviceRegisterData ( this . Device , this . Register , new byte [ 1 ] { value } ) ;
157
+ WriteObject ( result ) ;
158
+ }
159
+ }
160
+ }
161
+ }
162
+
163
+ [ Cmdlet ( VerbsCommon . Set , "I2CRegister" ) ]
164
+ public class SetI2CRegister : Cmdlet
165
+ {
166
+ [ Parameter ( Mandatory = true , ValueFromPipeline = true , ValueFromPipelineByPropertyName = true , Position = 0 ) ]
167
+ public I2CDevice Device { get ; set ; }
168
+
169
+ [ Parameter ( Mandatory = true , ValueFromPipelineByPropertyName = true , Position = 1 ) ]
170
+ public ushort Register { get ; set ; }
171
+
172
+ [ Parameter ( Mandatory = true , ValueFromPipelineByPropertyName = true , Position = 2 ) ]
173
+ public byte [ ] Data { get ; set ; }
174
+
175
+ [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = true ) ]
176
+ public SwitchParameter PassThru { get ; set ; }
177
+
178
+ protected override void ProcessRecord ( )
179
+ {
180
+ this . Device . device . WriteAddressByte ( this . Register , this . Data [ 0 ] ) ;
181
+ if ( this . PassThru )
182
+ {
183
+ I2CDeviceRegisterData result = new I2CDeviceRegisterData ( this . Device , this . Register , this . Data ) ;
184
+ WriteObject ( result ) ;
185
+ }
186
+ }
187
+ }
188
+
189
+ [ Cmdlet ( VerbsCommon . Set , "GpioPin" ) ]
190
+ public class SetGpioPin : Cmdlet
191
+ {
192
+ [ Parameter ( Mandatory = true , ValueFromPipeline = true , ValueFromPipelineByPropertyName = true , Position = 0 ) ]
193
+ public int [ ] Id { get ; set ; }
194
+
195
+ [ Parameter ( Mandatory = true , ValueFromPipelineByPropertyName = true , Position = 1 ) ]
196
+ public SignalLevel Value { get ; set ; }
197
+
198
+ [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = true ) ]
199
+ public SwitchParameter PassThru { get ; set ; }
200
+
201
+ protected override void ProcessRecord ( )
202
+ {
203
+ if ( this . Id != null )
204
+ {
205
+ foreach ( int pinId in this . Id )
206
+ {
207
+ var pin = Unosquare . RaspberryIO . Pi . Gpio [ pinId ] ;
208
+ pin . PinMode = Unosquare . RaspberryIO . Gpio . GpioPinDriveMode . Output ;
209
+ pin . Write ( ( Unosquare . RaspberryIO . Gpio . GpioPinValue ) this . Value ) ;
210
+ if ( this . PassThru )
211
+ {
212
+ GpioPinData pinData = new GpioPinData ( pinId , this . Value , pin ) ;
213
+ WriteObject ( pinData ) ;
214
+ }
215
+ }
216
+ }
217
+ }
218
+ }
219
+
220
+ [ Cmdlet ( VerbsCommon . Get , "GpioPin" ) ]
221
+ public class GetGpioPin : Cmdlet
222
+ {
223
+ [ Parameter ( Mandatory = false , ValueFromPipeline = true , ValueFromPipelineByPropertyName = true , Position = 0 ) ]
224
+ public int [ ] Id { get ; set ; }
225
+
226
+ [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = true , Position = 1 ) ]
227
+ public PullMode ? PullMode { get ; set ; }
228
+
229
+ [ Parameter ( Mandatory = false , ValueFromPipelineByPropertyName = true ) ]
230
+ public SwitchParameter Raw { get ; set ; }
231
+
232
+ protected override void ProcessRecord ( )
233
+ {
234
+ ArrayList pinList = new ArrayList ( ) ;
235
+
236
+ if ( ( this . Id == null ) || ( this . Id . Length <= 0 ) )
237
+ {
238
+ foreach ( var pin in Unosquare . RaspberryIO . Pi . Gpio . Pins )
239
+ {
240
+ pinList . Add ( pin . PinNumber ) ;
241
+ }
242
+ }
243
+ else
244
+ {
245
+ pinList . AddRange ( this . Id ) ;
246
+ }
247
+
248
+ foreach ( int pinId in pinList )
249
+ {
250
+ var pin = Unosquare . RaspberryIO . Pi . Gpio [ pinId ] ;
251
+ try
252
+ {
253
+ pin . PinMode = Unosquare . RaspberryIO . Gpio . GpioPinDriveMode . Input ;
254
+ if ( this . PullMode . HasValue )
255
+ {
256
+ pin . InputPullMode = ( Unosquare . RaspberryIO . Gpio . GpioPinResistorPullMode ) this . PullMode . Value ;
257
+ } ;
258
+ }
259
+ catch ( System . NotSupportedException )
260
+ {
261
+ // We want to avoid errors like
262
+ // System.NotSupportedException : Get - GpioPin : Pin Pin15 'BCM 14 (UART Transmit)' does not support mode 'Input'.Pin capabilities are limited to: UARTTXD
263
+ // at the same time we need to return PinInfo for such pins, so we need to continue processing
264
+ }
265
+ bool pinBoolValue = pin . Read ( ) ;
266
+
267
+ if ( this . Raw )
268
+ {
269
+ WriteObject ( pinBoolValue ? SignalLevel . High : SignalLevel . Low ) ;
270
+ }
271
+ else
272
+ {
273
+ GpioPinData pinData = new GpioPinData ( pinId , pinBoolValue ? SignalLevel . High : SignalLevel . Low , pin ) ;
274
+ WriteObject ( pinData ) ;
275
+ }
276
+ }
277
+ }
278
+ }
279
+ }
0 commit comments