use super::low_level::{CountingMode, OutputCompareMode};
use super::{Channel, Polarity, Timer};
pub struct OutputCompare {
timer: Timer,
}
impl OutputCompare {
pub fn new(mut timer: Timer, prescaler: u16, period: u32, mode: CountingMode) -> Self {
timer.stop();
timer.set_counting_mode(mode);
timer.set_prescaler(prescaler);
timer.set_auto_reload(period);
timer.generate_update();
Self { timer }
}
pub fn channel(
&mut self,
channel: Channel,
mode: OutputCompareMode,
compare: u32,
) -> OutputCompareChannel<'_> {
self.timer.configure_output(channel, mode, false, false);
self.timer.set_compare(channel, compare);
OutputCompareChannel {
driver: self,
channel,
}
}
pub fn start(&mut self) {
self.timer.start();
}
pub fn stop(&mut self) {
self.timer.stop();
}
pub fn free(self) -> Timer {
self.timer
}
}
pub struct OutputCompareChannel<'a> {
driver: &'a mut OutputCompare,
channel: Channel,
}
impl OutputCompareChannel<'_> {
pub fn enable(&mut self) {
self.driver.timer.set_channel_enabled(self.channel, true);
}
pub fn disable(&mut self) {
self.driver.timer.set_channel_enabled(self.channel, false);
}
pub fn set_compare(&mut self, compare: u32) {
self.driver.timer.set_compare(self.channel, compare);
}
pub fn set_mode(&mut self, mode: OutputCompareMode) {
self.driver
.timer
.configure_output(self.channel, mode, false, false);
}
pub fn set_polarity(&mut self, polarity: Polarity) {
self.driver
.timer
.set_channel_polarity(self.channel, polarity);
}
}