-
Notifications
You must be signed in to change notification settings - Fork 273
Add ADC trait definitions #743
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
i509VCB
wants to merge
1
commit into
rust-embedded:master
Choose a base branch
from
i509VCB:adc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+171
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| //! Asynchronous Analog to Digital conversion. | ||
| //! | ||
| //! See the [blocking](embedded_hal::adc) types documentation for more information. | ||
|
|
||
| use core::future::Future; | ||
|
|
||
| use embedded_hal::adc::{Channel, ErrorType}; | ||
|
|
||
| /// Asynchronous ADC channel capable of a one shot measurement. | ||
| pub trait OneShotChannel: Channel + ErrorType { | ||
| /// The maximum value that may be produced by the ADC. | ||
| /// | ||
| /// This returns [`Ready`](core::task::Poll::Ready) when the max value is obtained. Some implementations such | ||
| /// as a channel connected over I2C may return [`Pending`](core::task::Poll::Pending) while the max value is | ||
| /// being obtained. | ||
| /// | ||
| /// This is guaranteed to not change once a channel is constructed. | ||
| /// | ||
| /// For example, a 12-bit ADC would return 4095. | ||
| async fn max_value(&self) -> Result<Self::Word, Self::Error>; | ||
|
|
||
| /// Sample from the ADC. | ||
| /// | ||
| /// This returns [`Ready`](core::task::Poll::Ready) when the sample is complete. | ||
| async fn sample(&mut self) -> Result<Self::Word, Self::Error>; | ||
| } | ||
|
|
||
| impl<T: OneShotChannel + ?Sized> OneShotChannel for &mut T { | ||
| #[inline] | ||
| fn max_value(&self) -> impl Future<Output = Result<Self::Word, Self::Error>> { | ||
| T::max_value(self) | ||
| } | ||
|
|
||
| #[inline] | ||
| fn sample(&mut self) -> impl Future<Output = Result<Self::Word, Self::Error>> { | ||
| T::sample(self) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| #![no_std] | ||
| #![allow(async_fn_in_trait)] | ||
|
|
||
| pub mod adc; | ||
| pub mod delay; | ||
| pub mod digital; | ||
| pub mod i2c; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| //! Blocking Analog to Digital conversion. | ||
| //! | ||
| //! # Multi channel ADCs | ||
| //! | ||
| //! One of the challenges of writing portable drivers is that the device may support | ||
| //! multiple ADC channels but driver may only be capable of sampling one channel at a | ||
| //! time.. | ||
| //! | ||
| //! To solve this, the implementor of [`OneShotChannel`] is expected to multiplex access to | ||
| //! the ADC. This means a driver may not directly implement the ADC traits for you. | ||
|
|
||
| #[cfg(feature = "defmt-03")] | ||
| use crate::defmt; | ||
|
|
||
| /// Error. | ||
| pub trait Error: core::fmt::Debug { | ||
| /// Convert error to a generic error kind | ||
| /// | ||
| /// By using this method, errors freely defined by HAL implementations | ||
| /// can be converted to a set of generic errors upon which generic | ||
| /// code can act. | ||
| fn kind(&self) -> ErrorKind; | ||
| } | ||
|
|
||
| impl Error for core::convert::Infallible { | ||
| fn kind(&self) -> ErrorKind { | ||
| match *self {} | ||
| } | ||
| } | ||
|
|
||
| /// Error kind. | ||
| /// | ||
| /// This represents a common set of operation errors. HAL implementations are | ||
| /// free to define more specific or additional error types. However, by providing | ||
| /// a mapping to these common errors, generic code can still react to them. | ||
| #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] | ||
| #[cfg_attr(feature = "defmt-03", derive(defmt::Format))] | ||
| #[non_exhaustive] | ||
| pub enum ErrorKind { | ||
| /// A different error occurred. The original error may contain more information. | ||
| Other, | ||
| } | ||
|
|
||
| impl Error for ErrorKind { | ||
| #[inline] | ||
| fn kind(&self) -> ErrorKind { | ||
| *self | ||
| } | ||
| } | ||
|
|
||
| impl core::error::Error for ErrorKind {} | ||
|
|
||
| impl core::fmt::Display for ErrorKind { | ||
| #[inline] | ||
| fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | ||
| match self { | ||
| Self::Other => write!( | ||
| f, | ||
| "A different error occurred. The original error may contain more information" | ||
| ), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Error type trait. | ||
| /// | ||
| /// This just defines the error type, to be used by the other traits. | ||
| pub trait ErrorType { | ||
| /// Error type | ||
| type Error: Error; | ||
| } | ||
|
|
||
| impl<T: ErrorType + ?Sized> ErrorType for &T { | ||
| type Error = T::Error; | ||
| } | ||
|
|
||
| impl<T: ErrorType + ?Sized> ErrorType for &mut T { | ||
| type Error = T::Error; | ||
| } | ||
|
|
||
| /// ADC channel. | ||
| /// | ||
| /// This does not contain any logic. It only defines the type of a sample read by the ADC. | ||
| pub trait Channel { | ||
| /// The size of a sample value from the ADC. | ||
| type Word: Sized + Copy; | ||
| } | ||
|
|
||
| /// ADC channel capable of a one shot measurement. | ||
| pub trait OneShotChannel: Channel + ErrorType { | ||
| /// The maximum value that may be produced by the ADC. | ||
| /// | ||
| /// This is guaranteed to not change once a channel is constructed. | ||
| /// | ||
| /// For example, a 12-bit ADC would return 4095. | ||
| fn max_value(&self) -> Result<Self::Word, Self::Error>; | ||
|
|
||
| /// Sample from the ADC. | ||
| fn sample(&mut self) -> Result<Self::Word, Self::Error>; | ||
| } | ||
|
|
||
| impl<T: Channel + ?Sized> Channel for &T { | ||
| type Word = T::Word; | ||
| } | ||
|
|
||
| impl<T: Channel + ?Sized> Channel for &mut T { | ||
| type Word = T::Word; | ||
| } | ||
|
|
||
| impl<T: OneShotChannel + ?Sized> OneShotChannel for &mut T { | ||
| #[inline] | ||
| fn max_value(&self) -> Result<Self::Word, Self::Error> { | ||
| T::max_value(self) | ||
| } | ||
|
|
||
| #[inline] | ||
| fn sample(&mut self) -> Result<Self::Word, Self::Error> { | ||
| T::sample(self) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use core::convert::Infallible; | ||
|
|
||
| use crate::adc::OneShotChannel; | ||
|
|
||
| /// Verify the blocking trait is dyn compatible. | ||
| #[allow(unused)] | ||
| fn dyn_compatible(channel: &mut dyn OneShotChannel<Word = u16, Error = Infallible>) {} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| #![warn(missing_docs)] | ||
| #![no_std] | ||
|
|
||
| pub mod adc; | ||
| pub mod delay; | ||
| pub mod digital; | ||
| pub mod i2c; | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in that case, would it make sense to have as a constant? Or are you considering a case for ADCs with selectable range (presumably trading performance vs resolution)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes this is to consider ADCs which a selectable resolution when configured. I can see making a query of max value async as a potential issue. I could see some less than ideal code literally asking over SPI/I2C/remote MCU interface every time for the resolution which would not be so free.
I could make this a non-async function of course which would make access effectively free and allow moving it to the common
Channeltrait.