diff options
author | Brian Cully <bjc@kublai.com> | 2022-08-06 13:06:59 -0400 |
---|---|---|
committer | Brian Cully <bjc@kublai.com> | 2022-08-06 13:06:59 -0400 |
commit | 340eb6eb0d3fb47f5d968c323c92943662457fda (patch) | |
tree | 3421390df55457fdfe1d3bbe7f97e325162f7f4b /src/blink.rs | |
parent | 35248b205cd56633e52b9e634a942e7fd987db0b (diff) | |
download | luchie-340eb6eb0d3fb47f5d968c323c92943662457fda.tar.gz luchie-340eb6eb0d3fb47f5d968c323c92943662457fda.zip |
Put blink in separate ‘Task’ struct.
Diffstat (limited to 'src/blink.rs')
-rw-r--r-- | src/blink.rs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/blink.rs b/src/blink.rs new file mode 100644 index 0000000..7c992d7 --- /dev/null +++ b/src/blink.rs @@ -0,0 +1,54 @@ +use crate::{PollResult, led::LED}; + +use gd32vf103xx_hal::prelude::_embedded_hal_timer_CountDown; + +use gd32vf103xx_hal::{ + eclic::{self, EclicExt}, + pac::{self, Interrupt}, + time::Hertz, + timer, +}; + +enum State { + WaitForTimer, + ToggleLED, +} + +pub struct Task { + timer: timer::Timer<pac::TIMER6>, + frequency: Hertz, + led: LED, + state: State, +} + +impl Task { + pub fn new(mut timer: timer::Timer<pac::TIMER6>, frequency: Hertz, led: LED) -> Self { + pac::ECLIC::setup(Interrupt::TIMER6, eclic::TriggerType::RisingEdge, eclic::Level::L0, eclic::Priority::P3); + unsafe { pac::ECLIC::unmask(Interrupt::TIMER6); } + if !pac::ECLIC::is_enabled(Interrupt::TIMER6) { + panic!("timer6 interrupt not enabled"); + } + timer.listen(timer::Event::Update); + + Self { timer, frequency, led, state: State::ToggleLED } + } + + pub fn poll(&mut self) -> PollResult { + match self.state { + State::WaitForTimer => { + if let Ok(_) = self.timer.wait() { + self.state = State::ToggleLED; + PollResult::Ok + } else { + PollResult::WouldBlock + } + }, + State::ToggleLED => { + self.led.toggle(); + self.timer.start(self.frequency); + self.state = State::WaitForTimer; + PollResult::WouldBlock + } + } + } +} |