~ruther/simple-clock

617aab43d7ee92e384842a6fedb0c5f12ce0256b — František Boháček 1 year, 9 months ago 7532a6f
feat(src): add CountDown abstraction
1 files changed, 38 insertions(+), 0 deletions(-)

A src/count_down.rs
A src/count_down.rs => src/count_down.rs +38 -0
@@ 0,0 1,38 @@
use fugit::MicrosDurationU32;
use nb::Result;
use stm32f1xx_hal::timer::{self, CounterUs, Instance};

pub trait CountDown {
    type Time;

    fn start(&mut self, count: Self::Time);
    fn wait(&mut self) -> Result<(), timer::Error>;
}

pub struct CountDowner<T> {
    counter: CounterUs<T>,
}

impl<T> CountDowner<T>
where
    T: Instance,
{
    pub fn new(counter: CounterUs<T>) -> Self {
        Self { counter }
    }
}

impl<T> CountDown for CountDowner<T>
where
    T: Instance,
{
    type Time = MicrosDurationU32;

    fn start(&mut self, count: Self::Time) {
        self.counter.start(count.into()).unwrap();
    }

    fn wait(&mut self) -> nb::Result<(), timer::Error> {
        self.counter.wait()
    }
}

Do not follow this link