@@ -118,7 +118,17 @@ public GetI2CDevice()
118
118
119
119
protected override void ProcessRecord ( )
120
120
{
121
- WriteObject ( new I2CDevice ( Unosquare . RaspberryIO . Pi . I2C . AddDevice ( this . Id ) , this . Id , this . FriendlyName ) ) ;
121
+ try
122
+ {
123
+ WriteObject ( new I2CDevice ( Unosquare . RaspberryIO . Pi . I2C . AddDevice ( this . Id ) , this . Id , this . FriendlyName ) ) ;
124
+ }
125
+ catch ( System . TypeInitializationException e ) // Unosquare.RaspberryIO.Gpio.GpioController.Initialize throws this TypeInitializationException
126
+ {
127
+ if ( ! Unosquare . RaspberryIO . Computer . SystemInfo . Instance . IsRunningAsRoot )
128
+ {
129
+ throw new PlatformNotSupportedException ( Resources . ErrNeedRootPrivileges , e ) ;
130
+ }
131
+ }
122
132
}
123
133
}
124
134
@@ -145,32 +155,42 @@ public GetI2CRegister()
145
155
146
156
protected override void ProcessRecord ( )
147
157
{
148
- if ( this . ByteCount > 1 )
158
+ try
149
159
{
150
- this . Device . device . Write ( ( byte ) this . Register ) ;
151
- byte [ ] value = this . Device . device . Read ( this . ByteCount ) ;
152
- if ( this . Raw )
160
+ if ( this . ByteCount > 1 )
153
161
{
154
- WriteObject ( value ) ;
162
+ this . Device . device . Write ( ( byte ) this . Register ) ;
163
+ byte [ ] value = this . Device . device . Read ( this . ByteCount ) ;
164
+ if ( this . Raw )
165
+ {
166
+ WriteObject ( value ) ;
167
+ }
168
+ else
169
+ {
170
+ I2CDeviceRegisterData result = new I2CDeviceRegisterData ( this . Device , this . Register ) ;
171
+ result . Data = value ;
172
+ WriteObject ( result ) ;
173
+ }
155
174
}
156
175
else
157
176
{
158
- I2CDeviceRegisterData result = new I2CDeviceRegisterData ( this . Device , this . Register ) ;
159
- result . Data = value ;
160
- WriteObject ( result ) ;
177
+ byte value = this . Device . device . ReadAddressByte ( this . Register ) ;
178
+ if ( this . Raw )
179
+ {
180
+ WriteObject ( value ) ;
181
+ }
182
+ else
183
+ {
184
+ I2CDeviceRegisterData result = new I2CDeviceRegisterData ( this . Device , this . Register , new byte [ 1 ] { value } ) ;
185
+ WriteObject ( result ) ;
186
+ }
161
187
}
162
188
}
163
- else
189
+ catch ( System . TypeInitializationException e ) // Unosquare.RaspberryIO.Gpio.GpioController.Initialize throws this TypeInitializationException
164
190
{
165
- byte value = this . Device . device . ReadAddressByte ( this . Register ) ;
166
- if ( this . Raw )
191
+ if ( ! Unosquare . RaspberryIO . Computer . SystemInfo . Instance . IsRunningAsRoot )
167
192
{
168
- WriteObject ( value ) ;
169
- }
170
- else
171
- {
172
- I2CDeviceRegisterData result = new I2CDeviceRegisterData ( this . Device , this . Register , new byte [ 1 ] { value } ) ;
173
- WriteObject ( result ) ;
193
+ throw new PlatformNotSupportedException ( Resources . ErrNeedRootPrivileges , e ) ;
174
194
}
175
195
}
176
196
}
@@ -193,11 +213,21 @@ public class SetI2CRegister : Cmdlet
193
213
194
214
protected override void ProcessRecord ( )
195
215
{
196
- this . Device . device . WriteAddressByte ( this . Register , this . Data [ 0 ] ) ;
197
- if ( this . PassThru )
216
+ try
217
+ {
218
+ this . Device . device . WriteAddressByte ( this . Register , this . Data [ 0 ] ) ;
219
+ if ( this . PassThru )
220
+ {
221
+ I2CDeviceRegisterData result = new I2CDeviceRegisterData ( this . Device , this . Register , this . Data ) ;
222
+ WriteObject ( result ) ;
223
+ }
224
+ }
225
+ catch ( System . TypeInitializationException e ) // Unosquare.RaspberryIO.Gpio.GpioController.Initialize throws this TypeInitializationException
198
226
{
199
- I2CDeviceRegisterData result = new I2CDeviceRegisterData ( this . Device , this . Register , this . Data ) ;
200
- WriteObject ( result ) ;
227
+ if ( ! Unosquare . RaspberryIO . Computer . SystemInfo . Instance . IsRunningAsRoot )
228
+ {
229
+ throw new PlatformNotSupportedException ( Resources . ErrNeedRootPrivileges , e ) ;
230
+ }
201
231
}
202
232
}
203
233
}
@@ -216,20 +246,30 @@ public class SetGpioPin : Cmdlet
216
246
217
247
protected override void ProcessRecord ( )
218
248
{
219
- if ( this . Id != null )
249
+ try
220
250
{
221
- foreach ( int pinId in this . Id )
251
+ if ( this . Id != null )
222
252
{
223
- var pin = Unosquare . RaspberryIO . Pi . Gpio [ pinId ] ;
224
- pin . PinMode = Unosquare . RaspberryIO . Gpio . GpioPinDriveMode . Output ;
225
- pin . Write ( ( Unosquare . RaspberryIO . Gpio . GpioPinValue ) this . Value ) ;
226
- if ( this . PassThru )
253
+ foreach ( int pinId in this . Id )
227
254
{
228
- GpioPinData pinData = new GpioPinData ( pinId , this . Value , pin ) ;
229
- WriteObject ( pinData ) ;
255
+ var pin = Unosquare . RaspberryIO . Pi . Gpio [ pinId ] ;
256
+ pin . PinMode = Unosquare . RaspberryIO . Gpio . GpioPinDriveMode . Output ;
257
+ pin . Write ( ( Unosquare . RaspberryIO . Gpio . GpioPinValue ) this . Value ) ;
258
+ if ( this . PassThru )
259
+ {
260
+ GpioPinData pinData = new GpioPinData ( pinId , this . Value , pin ) ;
261
+ WriteObject ( pinData ) ;
262
+ }
230
263
}
231
264
}
232
265
}
266
+ catch ( System . TypeInitializationException e ) // Unosquare.RaspberryIO.Gpio.GpioController.Initialize throws this TypeInitializationException
267
+ {
268
+ if ( ! Unosquare . RaspberryIO . Computer . SystemInfo . Instance . IsRunningAsRoot )
269
+ {
270
+ throw new PlatformNotSupportedException ( Resources . ErrNeedRootPrivileges , e ) ;
271
+ }
272
+ }
233
273
}
234
274
}
235
275
@@ -247,47 +287,57 @@ public class GetGpioPin : Cmdlet
247
287
248
288
protected override void ProcessRecord ( )
249
289
{
250
- ArrayList pinList = new ArrayList ( ) ;
251
-
252
- if ( ( this . Id == null ) || ( this . Id . Length <= 0 ) )
290
+ try
253
291
{
254
- foreach ( var pin in Unosquare . RaspberryIO . Pi . Gpio . Pins )
255
- {
256
- pinList . Add ( pin . PinNumber ) ;
257
- }
258
- }
259
- else
260
- {
261
- pinList . AddRange ( this . Id ) ;
262
- }
292
+ ArrayList pinList = new ArrayList ( ) ;
263
293
264
- foreach ( int pinId in pinList )
265
- {
266
- var pin = Unosquare . RaspberryIO . Pi . Gpio [ pinId ] ;
267
- try
294
+ if ( ( this . Id == null ) || ( this . Id . Length <= 0 ) )
268
295
{
269
- pin . PinMode = Unosquare . RaspberryIO . Gpio . GpioPinDriveMode . Input ;
270
- if ( this . PullMode . HasValue )
296
+ foreach ( var pin in Unosquare . RaspberryIO . Pi . Gpio . Pins )
271
297
{
272
- pin . InputPullMode = ( Unosquare . RaspberryIO . Gpio . GpioPinResistorPullMode ) this . PullMode . Value ;
273
- } ;
298
+ pinList . Add ( pin . PinNumber ) ;
299
+ }
274
300
}
275
- catch ( System . NotSupportedException )
301
+ else
276
302
{
277
- // We want to avoid errors like
278
- // System.NotSupportedException : Get - GpioPin : Pin Pin15 'BCM 14 (UART Transmit)' does not support mode 'Input'.Pin capabilities are limited to: UARTTXD
279
- // at the same time we need to return PinInfo for such pins, so we need to continue processing
303
+ pinList . AddRange ( this . Id ) ;
280
304
}
281
- bool pinBoolValue = pin . Read ( ) ;
282
305
283
- if ( this . Raw )
306
+ foreach ( int pinId in pinList )
284
307
{
285
- WriteObject ( pinBoolValue ? SignalLevel . High : SignalLevel . Low ) ;
308
+ var pin = Unosquare . RaspberryIO . Pi . Gpio [ pinId ] ;
309
+ try
310
+ {
311
+ pin . PinMode = Unosquare . RaspberryIO . Gpio . GpioPinDriveMode . Input ;
312
+ if ( this . PullMode . HasValue )
313
+ {
314
+ pin . InputPullMode = ( Unosquare . RaspberryIO . Gpio . GpioPinResistorPullMode ) this . PullMode . Value ;
315
+ } ;
316
+ }
317
+ catch ( System . NotSupportedException )
318
+ {
319
+ // We want to avoid errors like
320
+ // System.NotSupportedException : Get - GpioPin : Pin Pin15 'BCM 14 (UART Transmit)' does not support mode 'Input'.Pin capabilities are limited to: UARTTXD
321
+ // at the same time we need to return PinInfo for such pins, so we need to continue processing
322
+ }
323
+ bool pinBoolValue = pin . Read ( ) ;
324
+
325
+ if ( this . Raw )
326
+ {
327
+ WriteObject ( pinBoolValue ? SignalLevel . High : SignalLevel . Low ) ;
328
+ }
329
+ else
330
+ {
331
+ GpioPinData pinData = new GpioPinData ( pinId , pinBoolValue ? SignalLevel . High : SignalLevel . Low , pin ) ;
332
+ WriteObject ( pinData ) ;
333
+ }
286
334
}
287
- else
335
+ }
336
+ catch ( System . TypeInitializationException e ) // Unosquare.RaspberryIO.Gpio.GpioController.Initialize throws this TypeInitializationException
337
+ {
338
+ if ( ! Unosquare . RaspberryIO . Computer . SystemInfo . Instance . IsRunningAsRoot )
288
339
{
289
- GpioPinData pinData = new GpioPinData ( pinId , pinBoolValue ? SignalLevel . High : SignalLevel . Low , pin ) ;
290
- WriteObject ( pinData ) ;
340
+ throw new PlatformNotSupportedException ( Resources . ErrNeedRootPrivileges , e ) ;
291
341
}
292
342
}
293
343
}
@@ -316,26 +366,36 @@ public SendSPIData()
316
366
317
367
protected override void ProcessRecord ( )
318
368
{
319
- var spiChannel = Unosquare . RaspberryIO . Pi . Spi . Channel0 ;
320
- if ( this . Channel == 1 )
321
- {
322
- spiChannel = Unosquare . RaspberryIO . Pi . Spi . Channel1 ;
323
- Unosquare . RaspberryIO . Pi . Spi . Channel1Frequency = ( int ) this . Frequency ;
324
- }
325
- else
369
+ try
326
370
{
327
- Unosquare . RaspberryIO . Pi . Spi . Channel0Frequency = ( int ) this . Frequency ;
328
- } ;
371
+ var spiChannel = Unosquare . RaspberryIO . Pi . Spi . Channel0 ;
372
+ if ( this . Channel == 1 )
373
+ {
374
+ spiChannel = Unosquare . RaspberryIO . Pi . Spi . Channel1 ;
375
+ Unosquare . RaspberryIO . Pi . Spi . Channel1Frequency = ( int ) this . Frequency ;
376
+ }
377
+ else
378
+ {
379
+ Unosquare . RaspberryIO . Pi . Spi . Channel0Frequency = ( int ) this . Frequency ;
380
+ } ;
329
381
330
- var response = spiChannel . SendReceive ( this . Data ) ;
331
- if ( this . Raw )
332
- {
333
- WriteObject ( response ) ;
382
+ var response = spiChannel . SendReceive ( this . Data ) ;
383
+ if ( this . Raw )
384
+ {
385
+ WriteObject ( response ) ;
386
+ }
387
+ else
388
+ {
389
+ SPIData spiData = new SPIData ( this . Channel , this . Frequency , this . Data , response ) ;
390
+ WriteObject ( spiData ) ;
391
+ }
334
392
}
335
- else
393
+ catch ( System . TypeInitializationException e ) // Unosquare.RaspberryIO.Gpio.GpioController.Initialize throws this TypeInitializationException
336
394
{
337
- SPIData spiData = new SPIData ( this . Channel , this . Frequency , this . Data , response ) ;
338
- WriteObject ( spiData ) ;
395
+ if ( ! Unosquare . RaspberryIO . Computer . SystemInfo . Instance . IsRunningAsRoot )
396
+ {
397
+ throw new PlatformNotSupportedException ( Resources . ErrNeedRootPrivileges , e ) ;
398
+ }
339
399
}
340
400
}
341
401
}
0 commit comments