Skip to content
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

[WIP] Initial proposal for CancellableOneShot #124

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/adc.rs
Expand Up @@ -96,3 +96,37 @@ pub trait OneShot<ADC, Word, Pin: Channel<ADC>> {
/// whatever channel underlies the pin.
fn read(&mut self, pin: &mut Pin) -> nb::Result<Word, Self::Error>;
}

/// ADCs that allow cancellation of pending reads
///
/// This trait is useful when reads are not always executed to completion:
///
/// ```
/// fn sample_while_pin_low(pin: ..., adc: ..., channel: ...) -> Result<Option<u16>, adc::Error> {
/// while pin.is_high() {};
///
/// adc.cancel(); // Ensure that no old results from the previous invocation show up
/// loop {
/// match adc.read(channel) {
/// Err(nb::WouldBlock) => continue,
/// Err(x) => Err(x),
/// Ok(n) => return Ok(Some(n)),
/// }
/// if pin.is_high() {
/// return None;
/// }
/// }
/// }
/// ```
#[cfg(feature = "unproven")]
pub trait CancellableOneShot<ADC, Word, Pin: Channel<ADC>>: OneShot<ADC, Word, Pin, Channel<ADC>> {
/// Cancel any pending ADC reading
///
/// If a previous reading was requested on this ADC but has not been picked up, cancel the
/// conversion and discard any result that might have been obtained.
///
/// This is a no-op if no conversion was pending. Implementations must ensure that when `read`
/// is called after a `cancel`, the `read` does not return any value sampled before the
/// `cancel`.
fn cancel(&mut self);
}