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