From 617aab43d7ee92e384842a6fedb0c5f12ce0256b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Fri, 28 Jul 2023 21:23:55 +0200 Subject: [PATCH] feat(src): add CountDown abstraction --- src/count_down.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/count_down.rs diff --git a/src/count_down.rs b/src/count_down.rs new file mode 100644 index 0000000..e858ca0 --- /dev/null +++ b/src/count_down.rs @@ -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 { + counter: CounterUs, +} + +impl CountDowner +where + T: Instance, +{ + pub fn new(counter: CounterUs) -> Self { + Self { counter } + } +} + +impl CountDown for CountDowner +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() + } +} -- 2.48.1