@@ -47,8 +47,8 @@ public class Ads1115 : IDisposable
47
47
/// <param name="measuringRange">Programmable Gain Amplifier</param>
48
48
/// <param name="dataRate">Data Rate</param>
49
49
/// <param name="deviceMode">Initial device mode</param>
50
- public Ads1115 ( I2cDevice i2cDevice , InputMultiplexer inputMultiplexer = InputMultiplexer . AIN0 , MeasuringRange measuringRange = MeasuringRange . FS4096 , DataRate dataRate = DataRate . SPS128 ,
51
- DeviceMode deviceMode = DeviceMode . Continuous )
50
+ public Ads1115 ( I2cDevice i2cDevice , InputMultiplexer inputMultiplexer = InputMultiplexer . AIN0 , MeasuringRange measuringRange = MeasuringRange . FS4096 ,
51
+ DataRate dataRate = DataRate . SPS128 , DeviceMode deviceMode = DeviceMode . Continuous )
52
52
{
53
53
_i2cDevice = i2cDevice ?? throw new ArgumentNullException ( nameof ( i2cDevice ) ) ;
54
54
_inputMultiplexer = inputMultiplexer ;
@@ -61,7 +61,6 @@ public Ads1115(I2cDevice i2cDevice, InputMultiplexer inputMultiplexer = InputMul
61
61
_comparatorPolarity = ComparatorPolarity . Low ;
62
62
_comparatorLatching = ComparatorLatching . NonLatching ;
63
63
_comparatorQueue = ComparatorQueue . Disable ;
64
- _shouldDispose = false ;
65
64
66
65
SetConfig ( ) ;
67
66
DisableAlertReadyPin ( ) ;
@@ -79,18 +78,18 @@ public Ads1115(I2cDevice i2cDevice, InputMultiplexer inputMultiplexer = InputMul
79
78
/// <param name="dataRate">Data Rate</param>
80
79
/// <param name="deviceMode">Initial device mode</param>
81
80
public Ads1115 ( I2cDevice i2cDevice ,
82
- GpioController gpioController , int gpioInterruptPin , bool shouldDispose = true , InputMultiplexer inputMultiplexer = InputMultiplexer . AIN0 , MeasuringRange measuringRange = MeasuringRange . FS4096 , DataRate dataRate = DataRate . SPS128 ,
83
- DeviceMode deviceMode = DeviceMode . Continuous )
81
+ GpioController ? gpioController , int gpioInterruptPin , bool shouldDispose = true , InputMultiplexer inputMultiplexer = InputMultiplexer . AIN0 , MeasuringRange measuringRange = MeasuringRange . FS4096 ,
82
+ DataRate dataRate = DataRate . SPS128 , DeviceMode deviceMode = DeviceMode . Continuous )
84
83
: this ( i2cDevice , inputMultiplexer , measuringRange , dataRate , deviceMode )
85
84
{
86
- _gpioController = gpioController ?? throw new ArgumentNullException ( nameof ( gpioController ) ) ;
87
- if ( gpioInterruptPin < 0 || gpioInterruptPin >= gpioController . PinCount )
85
+ _gpioController = gpioController ?? new GpioController ( ) ;
86
+ if ( gpioInterruptPin < 0 || gpioInterruptPin >= _gpioController . PinCount )
88
87
{
89
88
throw new ArgumentOutOfRangeException ( nameof ( gpioInterruptPin ) , $ "The given GPIO Controller has no pin number { gpioInterruptPin } ") ;
90
89
}
91
90
92
91
_gpioInterruptPin = gpioInterruptPin ;
93
- _shouldDispose = shouldDispose ;
92
+ _shouldDispose = shouldDispose || gpioController is null ;
94
93
}
95
94
96
95
/// <summary>
@@ -164,11 +163,7 @@ public DeviceMode DeviceMode
164
163
/// Comparator mode.
165
164
/// Only relevant if the comparator trigger event is set up and is changed by <see cref="EnableComparator(short, short, ComparatorMode, ComparatorQueue)"/>.
166
165
/// </summary>
167
- public ComparatorMode ComparatorMode
168
- {
169
- get ;
170
- private set ;
171
- }
166
+ public ComparatorMode ComparatorMode { get ; private set ; }
172
167
173
168
/// <summary>
174
169
/// Comparator polarity. Indicates whether the rising or the falling edge of the ALRT/RDY Pin is relevant.
@@ -208,13 +203,7 @@ public ComparatorLatching ComparatorLatching
208
203
/// Minimum number of samples exceeding the lower/upper threshold before the ALRT pin is asserted.
209
204
/// This can only be set with <see cref="EnableComparator(short, short, ComparatorMode, ComparatorQueue)"/>.
210
205
/// </summary>
211
- public ComparatorQueue ComparatorQueue
212
- {
213
- get
214
- {
215
- return _comparatorQueue ;
216
- }
217
- }
206
+ public ComparatorQueue ComparatorQueue => _comparatorQueue ;
218
207
219
208
/// <summary>
220
209
/// This event fires when a new value is available (in conversion ready mode) or the comparator threshold is exceeded.
@@ -285,7 +274,7 @@ private void DisableAlertReadyPin()
285
274
( byte ) Register . ADC_CONFIG_REG_HI_THRESH , 0x7F , 0xFF
286
275
} ;
287
276
_i2cDevice . Write ( writeBuff ) ;
288
- if ( _gpioController != null )
277
+ if ( _gpioController is object )
289
278
{
290
279
_gpioController . UnregisterCallbackForPinValueChangedEvent ( _gpioInterruptPin , ConversionReadyCallback ) ;
291
280
_gpioController . ClosePin ( _gpioInterruptPin ) ;
@@ -319,7 +308,7 @@ private void WriteComparatorRegisters(short loThreshold, short hiThreshold)
319
308
/// for interrupt handling.</exception>
320
309
public void EnableConversionReady ( )
321
310
{
322
- if ( _gpioController == null )
311
+ if ( _gpioController is null )
323
312
{
324
313
throw new InvalidOperationException ( "Must have provided a GPIO Controller for interrupt handling." ) ;
325
314
}
@@ -349,7 +338,7 @@ public void EnableConversionReady()
349
338
350
339
private void ConversionReadyCallback ( object sender , PinValueChangedEventArgs pinValueChangedEventArgs )
351
340
{
352
- if ( AlertReadyAsserted != null )
341
+ if ( AlertReadyAsserted is object )
353
342
{
354
343
AlertReadyAsserted ( ) ;
355
344
}
@@ -368,10 +357,7 @@ private void ConversionReadyCallback(object sender, PinValueChangedEventArgs pin
368
357
/// <param name="queueLength">Minimum number of samples that must exceed the threshold to trigger the event</param>
369
358
/// <exception cref="InvalidOperationException">The GPIO Controller for the interrupt handler has not been set up</exception>
370
359
public void EnableComparator ( ElectricPotential lowerValue , ElectricPotential upperValue , ComparatorMode mode ,
371
- ComparatorQueue queueLength )
372
- {
373
- EnableComparator ( VoltageToRaw ( lowerValue ) , VoltageToRaw ( upperValue ) , mode , queueLength ) ;
374
- }
360
+ ComparatorQueue queueLength ) => EnableComparator ( VoltageToRaw ( lowerValue ) , VoltageToRaw ( upperValue ) , mode , queueLength ) ;
375
361
376
362
/// <summary>
377
363
/// Enable comparator callback mode.
@@ -388,7 +374,7 @@ public void EnableComparator(ElectricPotential lowerValue, ElectricPotential upp
388
374
public void EnableComparator ( short lowerValue , short upperValue , ComparatorMode mode ,
389
375
ComparatorQueue queueLength )
390
376
{
391
- if ( _gpioController == null )
377
+ if ( _gpioController is null )
392
378
{
393
379
throw new InvalidOperationException ( "GPIO Controller must have been provided in constructor for this operation to work" ) ;
394
380
}
@@ -444,7 +430,7 @@ private ushort ReadConfigRegister()
444
430
/// This method must only be called in powerdown mode, otherwise it would timeout, since the busy bit never changes.
445
431
/// Due to that, we always write the configuration first in power down mode and then enable the continuous bit.
446
432
/// </summary>
447
- /// <exception cref="TimeoutException">A timeout occured waiting for the ADC to finish the conversion</exception>
433
+ /// <exception cref="TimeoutException">A timeout occurred waiting for the ADC to finish the conversion</exception>
448
434
private void WaitWhileBusy ( )
449
435
{
450
436
// In powerdown-mode, wait until the busy bit goes high
@@ -502,10 +488,7 @@ private short ReadRawInternal()
502
488
/// For performance reasons, it is advised to use this method if quick readings with different input channels are required,
503
489
/// instead of setting all the properties first and then calling <see cref="ReadRaw()"/>.
504
490
/// </remarks>
505
- public short ReadRaw ( InputMultiplexer inputMultiplexer )
506
- {
507
- return ReadRaw ( inputMultiplexer , MeasuringRange , DataRate ) ;
508
- }
491
+ public short ReadRaw ( InputMultiplexer inputMultiplexer ) => ReadRaw ( inputMultiplexer , MeasuringRange , DataRate ) ;
509
492
510
493
/// <summary>
511
494
/// Reads the next raw value, first switching to the given input and ranges.
@@ -590,30 +573,16 @@ public short VoltageToRaw(ElectricPotential voltage)
590
573
/// <returns>An electric potential (voltage).</returns>
591
574
public ElectricPotential MaxVoltageFromMeasuringRange ( MeasuringRange measuringRange )
592
575
{
593
- double voltage ;
594
- switch ( measuringRange )
595
- {
596
- case MeasuringRange . FS6144 :
597
- voltage = 6.144 ;
598
- break ;
599
- case MeasuringRange . FS4096 :
600
- voltage = 4.096 ;
601
- break ;
602
- case MeasuringRange . FS2048 :
603
- voltage = 2.048 ;
604
- break ;
605
- case MeasuringRange . FS1024 :
606
- voltage = 1.024 ;
607
- break ;
608
- case MeasuringRange . FS0512 :
609
- voltage = 0.512 ;
610
- break ;
611
- case MeasuringRange . FS0256 :
612
- voltage = 0.256 ;
613
- break ;
614
- default :
615
- throw new ArgumentOutOfRangeException ( nameof ( measuringRange ) , "Unknown measuring range used" ) ;
616
- }
576
+ double voltage = measuringRange switch
577
+ {
578
+ MeasuringRange . FS6144 => 6.144 ,
579
+ MeasuringRange . FS4096 => 4.096 ,
580
+ MeasuringRange . FS2048 => 2.048 ,
581
+ MeasuringRange . FS1024 => 1.024 ,
582
+ MeasuringRange . FS0512 => 0.512 ,
583
+ MeasuringRange . FS0256 => 0.256 ,
584
+ _ => throw new ArgumentOutOfRangeException ( nameof ( measuringRange ) , "Unknown measuring range used" )
585
+ } ;
617
586
618
587
return ElectricPotential . FromVolts ( voltage ) ;
619
588
}
@@ -623,50 +592,37 @@ public ElectricPotential MaxVoltageFromMeasuringRange(MeasuringRange measuringRa
623
592
/// </summary>
624
593
/// <param name="dataRate">One of the <see cref="DataRate"/> enumeration members.</param>
625
594
/// <returns>A frequency, in Hertz</returns>
626
- public double FrequencyFromDataRate ( DataRate dataRate )
595
+ public double FrequencyFromDataRate ( DataRate dataRate ) => dataRate switch
627
596
{
628
- switch ( dataRate )
629
- {
630
- case DataRate . SPS008 :
631
- return 8.0 ;
632
- case DataRate . SPS016 :
633
- return 16.0 ;
634
- case DataRate . SPS032 :
635
- return 32.0 ;
636
- case DataRate . SPS064 :
637
- return 64.0 ;
638
- case DataRate . SPS128 :
639
- return 128.0 ;
640
- case DataRate . SPS250 :
641
- return 250.0 ;
642
- case DataRate . SPS475 :
643
- return 475.0 ;
644
- case DataRate . SPS860 :
645
- return 860.0 ;
646
- default :
647
- throw new ArgumentOutOfRangeException ( nameof ( dataRate ) , "Unknown data rate used" ) ;
648
- }
649
- }
597
+ DataRate . SPS008 => 8.0 ,
598
+ DataRate . SPS016 => 16.0 ,
599
+ DataRate . SPS032 => 32.0 ,
600
+ DataRate . SPS064 => 64.0 ,
601
+ DataRate . SPS128 => 128.0 ,
602
+ DataRate . SPS250 => 250.0 ,
603
+ DataRate . SPS475 => 475.0 ,
604
+ DataRate . SPS860 => 860.0 ,
605
+ _ => throw new ArgumentOutOfRangeException ( nameof ( dataRate ) , "Unknown data rate used" )
606
+ } ;
650
607
651
608
/// <summary>
652
609
/// Cleanup.
653
610
/// Failing to dispose this class, especially when callbacks are active, may lead to undefined behavior.
654
611
/// </summary>
655
612
public void Dispose ( )
656
613
{
657
- if ( _i2cDevice != null )
614
+ if ( _i2cDevice is object )
658
615
{
659
616
DisableAlertReadyPin ( ) ;
660
- _i2cDevice ? . Dispose ( ) ;
617
+ _i2cDevice . Dispose ( ) ;
661
618
_i2cDevice = null ! ;
662
619
}
663
620
664
- if ( _shouldDispose && _gpioController != null )
621
+ if ( _shouldDispose )
665
622
{
666
- _gpioController . Dispose ( ) ;
623
+ _gpioController ? . Dispose ( ) ;
624
+ _gpioController = null ;
667
625
}
668
-
669
- _gpioController = null ;
670
626
}
671
627
}
672
628
}
0 commit comments