From c70e4aa652af81c89b710e511fc20452c6d7c607 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Thu, 22 Sep 2022 17:14:20 +0100 Subject: [PATCH] Rework StartContinuousSampling and StartAveragedContinuousSampling (#7) --- nanoFramework.GiantGecko.Adc/AdcController.cs | 74 +++++++++++++++---- 1 file changed, 59 insertions(+), 15 deletions(-) diff --git a/nanoFramework.GiantGecko.Adc/AdcController.cs b/nanoFramework.GiantGecko.Adc/AdcController.cs index d79340f..19c4ce9 100644 --- a/nanoFramework.GiantGecko.Adc/AdcController.cs +++ b/nanoFramework.GiantGecko.Adc/AdcController.cs @@ -22,12 +22,10 @@ public class AdcController : AdcControllerBase // a lock is required because multiple threads can access the AdcController [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)] private readonly object _syncLock; -#pragma warning disable IDE0052 // required in native driver - private AdcChannel[] _scanChannels; -#pragma warning restore IDE0052 // Remove unread private members private bool _continuousSamplingStarted; private readonly AdcConfiguration _adcConfiguration; + private AdcChannelConfiguration _adcChannelConfiguration; /// public override int ChannelCount @@ -134,22 +132,59 @@ public override AdcChannel OpenChannel( } /// - /// Starts continuous sampling on the specified channels. + /// Starts continuous sampling on the specified channels using the default . /// - /// Array of channels to scan performing continuous sampling. - /// if the operation was successful. otherwise. + /// Array of channels indexes to scan performing continuous sampling. /// If a previous continuous sampling operation has been started previously without being stopped. - public bool StartContinuousSampling(AdcChannel[] channels) + /// If the specified channel index does not exist. + public void StartContinuousSampling(int[] channels) + { + StartContinuousSampling( + channels, + new AdcChannelConfiguration()); + } + + /// + /// Starts continuous sampling on the specified channels using the specified . + /// + /// Array of channels indexes to scan performing continuous sampling. + /// Initial configuration for the various ADC channels. + /// If a previous continuous sampling operation has been started previously without being stopped. + /// If the specified channel index does not exist. + public void StartContinuousSampling( + int[] channels, + AdcChannelConfiguration configuration) { CheckIfContinuousSamplingIsStarted(); - _scanChannels = channels; + _adcChannelConfiguration = configuration; // set average count to 1 for single sample - // update flag upon successful start - _continuousSamplingStarted = NativeStartContinuousConversion(1); + // flag is updated in native code upon successful start + NativeStartContinuousConversion( + channels, + 1); + } - return _continuousSamplingStarted; + /// + /// Starts continuous sampling and average the digital representation of analog values read from the ADC. + /// + /// + /// In this mode, the last count samples are averaged and made available in LastScanConversion[0]. + /// + /// Array of channels to scan performing continuous sampling. + /// Number of samples to take for averaging. + /// if the continuous sampling was successfully started. otherwise. + /// + /// If the specified channel index does not exist. + public void StartAveragedContinuousSampling( + int[] channels, + int count) + { + StartAveragedContinuousSampling( + channels, + new AdcChannelConfiguration(), + count); } /// @@ -159,18 +194,25 @@ public bool StartContinuousSampling(AdcChannel[] channels) /// In this mode, the last count samples are averaged and made available in LastScanConversion[0]. /// /// Array of channels to scan performing continuous sampling. + /// Initial configuration for the various ADC channels. /// Number of samples to take for averaging. /// if the continuous sampling was successfully started. otherwise. /// - public bool StartAveragedContinuousSampling(AdcChannel[] channels, int count) + /// If the specified channel index does not exist. + public void StartAveragedContinuousSampling( + int[] channels, + AdcChannelConfiguration configuration, + int count) { CheckIfContinuousSamplingIsStarted(); - _scanChannels = channels; + _adcChannelConfiguration = configuration; // set average count to 1 for single sample // flag is updated in native code upon successful start - return NativeStartContinuousConversion(count); + NativeStartContinuousConversion( + channels, + count); } /// @@ -211,7 +253,9 @@ private void CheckIfContinuousSamplingIsStarted(bool invertCheck = false) private extern SampleResolution[] NativeGetSupportedResolutionsInBits(); [MethodImpl(MethodImplOptions.InternalCall)] - private extern bool NativeStartContinuousConversion(int count); + private extern void NativeStartContinuousConversion( + int[] channels, + int count); [MethodImpl(MethodImplOptions.InternalCall)] private extern void NativeStopContinuousConversion();