ad983x/
ad9833_ad9837.rs

1use embedded_hal::spi::SpiDevice;
2
3use crate::{marker, Ad983x, BitFlags, Error, OutputWaveform};
4
5impl<DEV, E> Ad983x<DEV, marker::Ad9833Ad9837>
6where
7    DEV: SpiDevice<Error = E>,
8{
9    /// Create a new instance of an AD9833 device.
10    ///
11    /// Remember to call `reset()` before using the device after power up.
12    pub fn new_ad9833(spi: DEV) -> Self {
13        Self::create(spi)
14    }
15    /// Create a new instance of an AD9837 device.
16    ///
17    /// Remember to call `reset()` before using the device after power up.
18    pub fn new_ad9837(spi: DEV) -> Self {
19        // Behaves the same as AD9833
20        Self::create(spi)
21    }
22
23    /// Set the output waveform
24    pub fn set_output_waveform(&mut self, waveform: OutputWaveform) -> Result<(), Error<E>> {
25        let control = match waveform {
26            OutputWaveform::Sinusoidal => self
27                .control
28                .with_low(BitFlags::OPBITEN)
29                .with_low(BitFlags::MODE),
30            OutputWaveform::Triangle => self
31                .control
32                .with_low(BitFlags::OPBITEN)
33                .with_high(BitFlags::MODE),
34            OutputWaveform::SquareMsbOfDac => self
35                .control
36                .with_high(BitFlags::OPBITEN)
37                .with_low(BitFlags::MODE)
38                .with_high(BitFlags::DIV2),
39            OutputWaveform::SquareMsbOfDacDiv2 => self
40                .control
41                .with_high(BitFlags::OPBITEN)
42                .with_low(BitFlags::MODE)
43                .with_low(BitFlags::DIV2),
44        };
45        self.write_control(control)
46    }
47}