1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use crate::sender::{PulseSender, State};
use crate::Command;
use core::convert::Infallible;

pub struct HalSender<PWMPIN, DUTY>
where
    PWMPIN: embedded_hal::PwmPin<Duty = DUTY>,
{
    pts: PulseSender,
    pin: PWMPIN,
    pub counter: u32,
}

impl<'a, PWMPIN, DUTY> HalSender<PWMPIN, DUTY>
where
    PWMPIN: embedded_hal::PwmPin<Duty = DUTY>,
{
    pub fn new(samplerate: u32, pin: PWMPIN) -> Self {
        Self {
            pts: PulseSender::new(samplerate),
            pin,
            counter: 0,
        }
    }

    pub fn load<C: Command>(&mut self, cmd: &C) -> nb::Result<(), Infallible> {
        if self.pts.state == State::Idle {
            self.pts.load_command(cmd);
            self.counter = 0;
            Ok(())
        } else {
            Err(nb::Error::WouldBlock)
        }
    }

    /// Get a reference to the data
    pub fn buffer(&self) -> &[u16] {
        &self.pts.buffer()
    }

    /// Method to be called periodically to update the pwm output
    pub fn tick(&mut self) {
        let state = self.pts.tick(self.counter);
        self.counter = self.counter.wrapping_add(1);

        match state {
            State::Transmit(true) => self.pin.enable(),
            State::Transmit(false) => self.pin.disable(),
            State::Idle => self.pin.disable(),
            State::Error => self.pin.disable(),
        };
    }
}