From 7ceabdb7a4f675397109091956852bc24283e4e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Tue, 13 Sep 2022 11:49:34 +0100 Subject: [PATCH] Changes from API design (#1) --- README.md | 3 +- nanoFramework.GiantGecko.Adc/AdcChannel.cs | 20 +-- .../AdcChannelConfiguration.cs | 10 +- .../AdcChannelMode.cs | 1 + .../AdcConfiguration.cs | 5 +- nanoFramework.GiantGecko.Adc/AdcController.cs | 136 +++++++++--------- .../AdcControllerBase.cs | 23 +-- .../Properties/AssemblyInfo.cs | 36 ++--- .../SampleResolution.cs | 1 - .../nanoFramework.GiantGecko.Adc.nfproj | 8 ++ nanoFramework.GiantGecko.Adc/packages.config | 1 + .../packages.lock.json | 19 +++ 12 files changed, 123 insertions(+), 140 deletions(-) create mode 100644 nanoFramework.GiantGecko.Adc/packages.lock.json diff --git a/README.md b/README.md index c32fe86..8d9d252 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ ----- -# Welcome to the .NET **nanoFramework** nanoFramework.GiantGecko.Adc repository +# Welcome to the .NET **nanoFramework** GiantGecko.Adc repository ## Build status @@ -14,7 +14,6 @@ ## Usage - ## Feedback and documentation For documentation, providing feedback, issues and finding out how to contribute please refer to the [Home repo](https://github.com/nanoframework/Home). diff --git a/nanoFramework.GiantGecko.Adc/AdcChannel.cs b/nanoFramework.GiantGecko.Adc/AdcChannel.cs index f65b44b..c6da97e 100644 --- a/nanoFramework.GiantGecko.Adc/AdcChannel.cs +++ b/nanoFramework.GiantGecko.Adc/AdcChannel.cs @@ -30,19 +30,21 @@ public class AdcChannel : AdcChannelBase, IDisposable #pragma warning restore IDE0052 // Remove unread private members /// - /// Initialization configuration for . + /// The initialized configuration for this . Any changes made + /// to properties in this object will be ignored: + /// properties must be defined before calling . /// public AdcChannelConfiguration AdcChannelConfiguration => _adcChannelConfiguration; - internal AdcChannel( - AdcController controller, - int channelNumber) + /// + /// The last value read in continuous sample mode started via () or, if averaged sampling continuous mode is currently in use via (), the latest averaged value. + /// It is an error to read this property if the channel is not currently in continuous mode. + /// + /// When reading from this property when the is not currently in continuous mode. + public extern int LastContinuousValue { - _adcController = controller; - _channelNumber = channelNumber; - _adcChannelConfiguration = default; - - _syncLock = new object(); + [MethodImpl(MethodImplOptions.InternalCall)] + get; } internal AdcChannel( diff --git a/nanoFramework.GiantGecko.Adc/AdcChannelConfiguration.cs b/nanoFramework.GiantGecko.Adc/AdcChannelConfiguration.cs index 9314659..b9459c5 100644 --- a/nanoFramework.GiantGecko.Adc/AdcChannelConfiguration.cs +++ b/nanoFramework.GiantGecko.Adc/AdcChannelConfiguration.cs @@ -21,9 +21,6 @@ public class AdcChannelConfiguration /// /// Peripheral reflex system trigger selection. /// - /// - /// Only applicable if prsEnable is enabled. - /// public PrsSampleTrigger PrsSampleTrigger { get => _prsSampleTrigger; set => _prsSampleTrigger = value; } /// @@ -45,15 +42,12 @@ public class AdcChannelConfiguration public SampleResolution SampleResolution { get => _sampleResolution; set => _sampleResolution = value; } /// - /// Gets or sets the channel mode for the . + /// Channel mode: single ended or differential. /// - /// - /// The mode for the . - /// public AdcChannelMode ChannelMode { get => _channelMode; set => _channelMode = value; } /// - /// Peripheral reflex system trigger enable. + /// Returns whether the peripheral reflex system trigger is enable, based on the setting. /// public bool IsPrsEnabled => _prsSampleTrigger > PrsSampleTrigger.Disabled; } diff --git a/nanoFramework.GiantGecko.Adc/AdcChannelMode.cs b/nanoFramework.GiantGecko.Adc/AdcChannelMode.cs index 4c22033..91ba8ad 100644 --- a/nanoFramework.GiantGecko.Adc/AdcChannelMode.cs +++ b/nanoFramework.GiantGecko.Adc/AdcChannelMode.cs @@ -14,6 +14,7 @@ public enum AdcChannelMode /// Simple value of a particular pin. /// SingleEnded = 0, + /// /// Difference between two pins. /// diff --git a/nanoFramework.GiantGecko.Adc/AdcConfiguration.cs b/nanoFramework.GiantGecko.Adc/AdcConfiguration.cs index c5cb23b..fd9f932 100644 --- a/nanoFramework.GiantGecko.Adc/AdcConfiguration.cs +++ b/nanoFramework.GiantGecko.Adc/AdcConfiguration.cs @@ -6,7 +6,7 @@ namespace nanoFramework.GiantGecko.Adc { /// - /// ADC configuration, common for single conversion and scan sequence. + /// ADC global configuration, common for single conversion and scan sequence. /// public class AdcConfiguration { @@ -21,7 +21,8 @@ public class AdcConfiguration /// Oversampling rate select. /// /// - /// To have any effect, oversampling must be enabled for single/scan mode. + /// To have any effect, oversampling must be enabled for single/scan mode when the + /// channel was opened/configred via . /// public OversampleRate OversampleRate { get => _oversampleRate; set => _oversampleRate = value; } diff --git a/nanoFramework.GiantGecko.Adc/AdcController.cs b/nanoFramework.GiantGecko.Adc/AdcController.cs index 3c95f1c..35651d0 100644 --- a/nanoFramework.GiantGecko.Adc/AdcController.cs +++ b/nanoFramework.GiantGecko.Adc/AdcController.cs @@ -13,7 +13,7 @@ namespace nanoFramework.GiantGecko.Adc /// Represents an on the system. /// /// - /// This class implements specifics of the Silabs Giant Gecko ADC. + /// This class implements specifics of the Silabs Giant Gecko EFM32 ADC. /// It's meant to be used instead of the standard System.Adc. /// public class AdcController : AdcControllerBase @@ -24,7 +24,7 @@ public class AdcController : AdcControllerBase private readonly object _syncLock; private AdcChannel[] _scanChannels; private int _averageCount; - private bool _continuousConvertionstarted; + private bool _continuousSamplingStarted; private readonly AdcConfiguration _acdConfiguration; @@ -38,14 +38,7 @@ public override int ChannelCount } /// - public override AdcChannelMode ChannelMode - { - get => throw new PlatformNotSupportedException(); - set => throw new PlatformNotSupportedException(); - } - - /// - public override SampleResolution SupportedResolutionsInBits + public override SampleResolution[] SupportedResolutionsInBits { get { @@ -59,48 +52,43 @@ public override SampleResolution SupportedResolutionsInBits public AdcConfiguration AcdConfiguration => _acdConfiguration; /// - /// Gets an array with the last conversions from an ongoing scan operation. + /// Gets an array with the last samples from an ongoing scan operation. /// - /// The ADC is not performing a scan operation. This as to be started with a call to or . - /// The values are either the last conversion or the average of the last conversion count, if the averaged continuous scan was started with . - public int[] LastConversion + /// The ADC is not performing a scan operation. This has to be started with a call to or . + /// + /// + /// The values are either the last sample value (if started with ) or the average of the last samples count (if the averaged continuous scan was started with ). + /// The array is indexed by the position of the channel in the array passed to or . For example, if ([channel_idda, channel_idd3_3]) was called, then [0] would have value for channel_idda and [1] would have value for channel_idd3_3. The last continuous sample for a channel may also be retrieved from . + /// + /// + /// Please see remarks on and for more information on continuous sampling. + /// + /// + public int[] LastContinuousSamples { - [MethodImpl(MethodImplOptions.InternalCall)] - get; - } + get + { + CheckIfContinuousSamplingIsStarted(); - /// - public override SampleResolution ResolutionInBits - { - get => throw new PlatformNotSupportedException(); - set => throw new PlatformNotSupportedException(); + return NativeGetLastContinuousSamples(); + } } + /// + /// Returns if the ADC is currently running in scan mode, started via or . otherwise. + /// + public bool IsContinuousSamplingRunning => _continuousSamplingStarted; + /// /// Initializes a new instance of the class. + /// A default is used. /// /// /// The for the system. /// /// If the has already been instantiated. - public AdcController() + public AdcController() : this(new AdcConfiguration()) { - // check if this device is already opened - if (_syncLock == null) - { - _acdConfiguration = new AdcConfiguration(); - - // call native init to allow HAL/PAL inits related with ADC hardware - // this is also used to check if the requested ADC actually exists - NativeInit(); - - _syncLock = new object(); - } - else - { - // this controller already exists: throw an exception - throw new InvalidOperationException(); - } } /// @@ -131,9 +119,7 @@ public AdcController(AdcConfiguration acdInitialization) /// public override AdcChannel OpenChannel(int channelNumber) { - NativeOpenChannel(channelNumber); - - return new AdcChannel(this, channelNumber); + return OpenChannel(channelNumber, new AdcChannelConfiguration()); } /// @@ -147,67 +133,69 @@ public override AdcChannel OpenChannel( } /// - /// Starts continuous conversions on the specified channels. + /// Starts continuous sampling on the specified channels. /// - /// Array of channels to scan performing continuous conversions. + /// Array of channels to scan performing continuous sampling. /// if the operation was successful. otherwise. - /// If a previous continuous conversion operation has been started previously without being stopped. - public bool StartContinuousConversion(AdcChannel[] channels) + /// If a previous continuous sampling operation has been started previously without being stopped. + public bool StartContinuousSampling(AdcChannel[] channels) { - if (_continuousConvertionstarted) - { - throw new InvalidOperationException(); - } + CheckIfContinuousSamplingIsStarted(); _scanChannels = channels; // set average count to 1 for single sample _averageCount = 1; // update flag - _continuousConvertionstarted = NativeStartContinuousConversion(); + _continuousSamplingStarted = NativeStartContinuousConversion(); - return _continuousConvertionstarted; + return _continuousSamplingStarted; } /// - /// Starts continuous conversions and average the digital representation of analog values read from the ADC. + /// Starts continuous sampling and average the digital representation of analog values read from the ADC. /// - /// Array of channels to scan performing continuous conversions. + /// + /// 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. /// - public bool StartAveragedContinuousConversion(AdcChannel[] channels, int count) + public bool StartAveragedContinuousSampling(AdcChannel[] channels, int count) { - if (_continuousConvertionstarted) - { - throw new InvalidOperationException(); - } + CheckIfContinuousSamplingIsStarted(); _scanChannels = channels; _averageCount = count; // update flag - _continuousConvertionstarted = NativeStartContinuousConversion(); + _continuousSamplingStarted = NativeStartContinuousConversion(); - return _continuousConvertionstarted; + return _continuousSamplingStarted; } /// - /// Stops an ongoing continuous conversion scan operation. + /// Stops an ongoing continuous sampling operation. /// - /// If there is no ongoing continuous conversion operation. - public void StoptContinuousConversion() + /// If there is no ongoing continuous sampling operation. + public void StopContinuousSampling() { - if (!_continuousConvertionstarted) - { - throw new InvalidOperationException(); - } + CheckIfContinuousSamplingIsStarted(true); NativeStoptContinuousConversion(); // update flag - _continuousConvertionstarted = false; + _continuousSamplingStarted = false; + + } + private void CheckIfContinuousSamplingIsStarted(bool invertCheck = false) + { + if (invertCheck ? !_continuousSamplingStarted : _continuousSamplingStarted) + { + throw new InvalidOperationException(); + } } #region Native Calls @@ -225,7 +213,7 @@ public void StoptContinuousConversion() private extern bool NativeIsChannelModeSupported(int mode); [MethodImpl(MethodImplOptions.InternalCall)] - private extern SampleResolution NativeGetSupportedResolutionsInBits(); + private extern SampleResolution[] NativeGetSupportedResolutionsInBits(); [MethodImpl(MethodImplOptions.InternalCall)] private extern bool NativeStartContinuousConversion(); @@ -233,6 +221,12 @@ public void StoptContinuousConversion() [MethodImpl(MethodImplOptions.InternalCall)] private extern bool NativeStoptContinuousConversion(); + [MethodImpl(MethodImplOptions.InternalCall)] + private extern int[] NativeGetLastContinuousSamples(); + + [MethodImpl(MethodImplOptions.InternalCall)] + private extern int NativeGetLastScanSampleForChannel(int channel); + #endregion } } diff --git a/nanoFramework.GiantGecko.Adc/AdcControllerBase.cs b/nanoFramework.GiantGecko.Adc/AdcControllerBase.cs index 6a15057..6c71eb4 100644 --- a/nanoFramework.GiantGecko.Adc/AdcControllerBase.cs +++ b/nanoFramework.GiantGecko.Adc/AdcControllerBase.cs @@ -29,6 +29,7 @@ public abstract class AdcControllerBase /// /// The channel to connect to. /// + /// Initial configuration for ADC channel. /// /// The ADC channel. /// @@ -42,30 +43,12 @@ public abstract class AdcControllerBase /// public abstract int ChannelCount { get; } - /// - /// Gets or sets the channel mode for the . - /// - /// - /// The mode for the . - /// - /// If the platform doesn't have this configuration available. - public abstract AdcChannelMode ChannelMode { get; set; } - /// /// Gets the resolution(s) of the controller as number of bits it has. For example, if we have a 10-bit ADC, that means it can detect 1024 (2^10) discrete levels. /// /// - /// The number of bits the has support for. - /// - public abstract SampleResolution SupportedResolutionsInBits { get; } - - /// - /// Gets or sets the resolution of the controller as number of bits it has. - /// - /// - /// The number of bits the will use to perform conversions. + /// Array with the resolution(s) that the ADC has support for. /// - /// If trying to change the resolution when a continuous conversion operation has been started. - public abstract SampleResolution ResolutionInBits { get; set; } + public abstract SampleResolution[] SupportedResolutionsInBits { get; } } } diff --git a/nanoFramework.GiantGecko.Adc/Properties/AssemblyInfo.cs b/nanoFramework.GiantGecko.Adc/Properties/AssemblyInfo.cs index 7801a8a..867add4 100644 --- a/nanoFramework.GiantGecko.Adc/Properties/AssemblyInfo.cs +++ b/nanoFramework.GiantGecko.Adc/Properties/AssemblyInfo.cs @@ -5,35 +5,17 @@ // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. -[assembly: AssemblyTitle("CSharp.TestApplication")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("CSharp.TestApplication")] -[assembly: AssemblyCopyright("Copyright © 2022")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] +[assembly: AssemblyTitle("nanoFramework.GiantGecko.Adc")] +[assembly: AssemblyCompany("nanoFramework Contributors")] +[assembly: AssemblyProduct("nanoFramework.GiantGecko.Adc")] +[assembly: AssemblyCopyright("Copyright (c) .NET Foundation and Contributors")] + +//////////////////////////////////////////////////////////////// +// update this whenever the native assembly signature changes // +[assembly: AssemblyNativeVersion("100.1.0.0")] +//////////////////////////////////////////////////////////////// // Setting ComVisible to false makes the types in this assembly not visible // to COM components. If you need to access a type in this assembly from // COM, set the ComVisible attribute to true on that type. [assembly: ComVisible(false)] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] - -///////////////////////////////////////////////////////////////// -// This attribute is mandatory when building Interop libraries // -// update this whenever the native assembly signature changes // -[assembly: AssemblyNativeVersion("1.0.0.0")] -///////////////////////////////////////////////////////////////// diff --git a/nanoFramework.GiantGecko.Adc/SampleResolution.cs b/nanoFramework.GiantGecko.Adc/SampleResolution.cs index 70e720d..89224b0 100644 --- a/nanoFramework.GiantGecko.Adc/SampleResolution.cs +++ b/nanoFramework.GiantGecko.Adc/SampleResolution.cs @@ -8,7 +8,6 @@ namespace nanoFramework.GiantGecko.Adc /// /// Sample resolution. /// - [System.Flags] public enum SampleResolution { /// diff --git a/nanoFramework.GiantGecko.Adc/nanoFramework.GiantGecko.Adc.nfproj b/nanoFramework.GiantGecko.Adc/nanoFramework.GiantGecko.Adc.nfproj index 7c7599f..c190be4 100644 --- a/nanoFramework.GiantGecko.Adc/nanoFramework.GiantGecko.Adc.nfproj +++ b/nanoFramework.GiantGecko.Adc/nanoFramework.GiantGecko.Adc.nfproj @@ -18,6 +18,7 @@ True bin\$(Configuration)\nanoFramework.GiantGecko.Adc.xml true + true @@ -72,4 +73,11 @@ + + + + This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}. + + + \ No newline at end of file diff --git a/nanoFramework.GiantGecko.Adc/packages.config b/nanoFramework.GiantGecko.Adc/packages.config index ac18722..fa0f444 100644 --- a/nanoFramework.GiantGecko.Adc/packages.config +++ b/nanoFramework.GiantGecko.Adc/packages.config @@ -1,4 +1,5 @@  + \ No newline at end of file diff --git a/nanoFramework.GiantGecko.Adc/packages.lock.json b/nanoFramework.GiantGecko.Adc/packages.lock.json new file mode 100644 index 0000000..e0a83c9 --- /dev/null +++ b/nanoFramework.GiantGecko.Adc/packages.lock.json @@ -0,0 +1,19 @@ +{ + "version": 1, + "dependencies": { + ".NETnanoFramework,Version=v1.0": { + "nanoFramework.CoreLibrary": { + "type": "Direct", + "requested": "[1.12.0, 1.12.0]", + "resolved": "1.12.0", + "contentHash": "qQrFNXmJiStMC4VXk5cVMOJp23/qlT9FW5i9i+igwQVwraQTtvpkam8yK1hj992jqrbjoCIFZP4Hw9E8H0pB7w==" + }, + "Nerdbank.GitVersioning": { + "type": "Direct", + "requested": "[3.5.109, 3.5.109]", + "resolved": "3.5.109", + "contentHash": "tKA1XJ5U2Ytk+u2/5sGNa04Pw6z9noYmqbpmHpjvbwJTtIalqsQjnwT4RQKSDTTIznHBbKxzv3HVh8QD28Qk+g==" + } + } + } +} \ No newline at end of file