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

Use Timer instead of Delay #3

Open
Arkaleks opened this issue Jun 19, 2019 · 1 comment
Open

Use Timer instead of Delay #3

Arkaleks opened this issue Jun 19, 2019 · 1 comment
Labels
enhancement New feature or request

Comments

@Arkaleks
Copy link
Owner

Arkaleks commented Jun 19, 2019

Current implementation of this driver takes ownership of a Delay in order to block get_compensated_sample() and alike operations. While this is fine as a very simple solution, not all implementations of embedded_hal provide multiple Delay object, e.g. atsamd implementation only provides one Delay which is using Systick as delay provider.

Instead of replacing the API to borrow a Delay whenever an operation need it, the API could be changed to be non-blocking by using a Timer:

// Old
pub fn get_sample(&mut self, osr: Oversampling) -> Result<Sample, E> {}

// New
pub fn get_sample(&mut self, osr: Oversampling) -> nb::Result<Sample, Error<SPI>>{}

Where get_sample``returns nb::WouldBlock ` as long as a conversion is ongoing, allowing user's code to do other operation in the mean time.

@Arkaleks Arkaleks added the enhancement New feature or request label Jun 19, 2019
@Arkaleks
Copy link
Owner Author

It's not clear how to use human time (milliseconds) with the trait CountDown. Most embedded-hal implementations use clock as a time reference, hence Time = Herz. Because external trait cannot be implemented for external types, something like the following is not possible:

mod my_device {
pub struct MyDevice<T>(T,)

impl<T, U> MyDevice<T>
where
    T: hal::timer::CountDown<Time=U>
    U: From<Milliseconds>
{
    pub fn new(timer: T) -> Self {
        let mut dev = Self(timer);
        dev.start(Milliseconds(3));
        (dev)
   }

    pub fn wait(&mut self) -> Result<(), Void> {
        (self.timer.wait())
    }
}

pub struct Milliseconds(u8);
}

// ...
fn main() {}

// Not possible
impl From<my_device::Milliseconds> for hal::Herz {
    fn from(duration: my_device::Milliseconds) -> Self {
        Herz(duration.0 * 1000)
    }
}

Adding a conversion trait could be a solution however that doesn't sound really like a good one.

Ref: rust-embedded/embedded-hal#129

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant