use core::mem::ManuallyDrop;
use core::task::Waker;
use super::low_level::Timer;
use super::{Channel, GeneralInstance4Channel};
use crate::dma::WritableRingBuffer;
use crate::dma::ringbuffer::Error;
use crate::dma::word::Word;
pub struct RingBufferedPwmChannel<'d, T: GeneralInstance4Channel, W: Word + Into<T::Word>> {
timer: ManuallyDrop<Timer<'d, T>>,
ring_buf: WritableRingBuffer<'d, W>,
channel: Channel,
}
impl<'d, T: GeneralInstance4Channel, W: Word + Into<T::Word>> RingBufferedPwmChannel<'d, T, W> {
pub(crate) fn new(
timer: ManuallyDrop<Timer<'d, T>>,
channel: Channel,
ring_buf: WritableRingBuffer<'d, W>,
) -> Self {
Self {
timer,
ring_buf,
channel,
}
}
pub fn start(&mut self) {
self.ring_buf.start()
}
pub fn clear(&mut self) {
self.ring_buf.clear()
}
pub fn write_immediate(&mut self, buf: &[W]) -> Result<(usize, usize), Error> {
self.ring_buf.write_immediate(buf)
}
pub fn write(&mut self, buf: &[W]) -> Result<(usize, usize), Error> {
self.ring_buf.write(buf)
}
pub async fn write_exact(&mut self, buffer: &[W]) -> Result<usize, Error> {
self.ring_buf.write_exact(buffer).await
}
pub async fn wait_write_error(&mut self) -> Result<usize, Error> {
self.ring_buf.wait_write_error().await
}
pub fn len(&mut self) -> Result<usize, Error> {
self.ring_buf.len()
}
pub const fn capacity(&self) -> usize {
self.ring_buf.capacity()
}
pub fn set_waker(&mut self, waker: &Waker) {
self.ring_buf.set_waker(waker)
}
pub fn request_reset(&mut self) {
self.ring_buf.request_reset()
}
pub fn request_pause(&mut self) {
self.ring_buf.request_pause()
}
pub fn is_running(&mut self) -> bool {
self.ring_buf.is_running()
}
pub async fn stop(&mut self) {
self.ring_buf.stop().await
}
pub fn enable(&mut self) {
self.timer.enable_channel(self.channel, true);
}
pub fn disable(&mut self) {
self.timer.enable_channel(self.channel, false);
}
pub fn is_enabled(&self) -> bool {
self.timer.get_channel_enable_state(self.channel)
}
pub fn max_duty_cycle(&self) -> u16 {
let max: u32 = self.timer.get_max_compare_value().into();
assert!(max < u16::MAX as u32);
max as u16 + 1
}
pub fn set_polarity(&mut self, polarity: super::low_level::OutputPolarity) {
self.timer.set_output_polarity(self.channel, polarity);
}
pub fn set_output_compare_mode(&mut self, mode: super::low_level::OutputCompareMode) {
self.timer.set_output_compare_mode(self.channel, mode);
}
}