dac8568/
asynchronous.rs

1use embedded_hal::digital::OutputPin;
2
3use crate::{mode::Async, Channel, Dac, DacError, Message};
4
5impl<SPI, SYNC> Dac<SPI, SYNC, Async> 
6    where SPI: embedded_hal_async::spi::SpiBus, SYNC: OutputPin,
7
8{
9    /// Set the specified value to the given channel. This will update the DAC
10    /// to output the desired voltage
11    pub async fn set_voltage(&mut self, channel: Channel, voltage: u16) -> Result<(), DacError> {
12        let message = Message::get_voltage_message(channel, voltage, self.is_inverted);
13        self.write(message).await
14    }
15
16    /// Configure the DAC to use its internal reference mode of 2.5v rather than using an external
17    /// voltage reference
18    pub async fn use_internal_reference(&mut self) -> Result<(), DacError> {
19        let message = Message::get_internal_reference_message(true);
20        self.write(message).await
21    }
22
23    /// Configure the DAC to use its external reference mode rather than using the internal reference
24    pub async fn use_external_reference(&mut self) -> Result<(), DacError> {
25        let message = Message::get_internal_reference_message(false);
26        self.write(message).await
27    }
28
29    /// Perform a software reset, clearing out all registers
30    /// 8.2.10 Software Reset Function
31    /// The DAC7568, DAC8168, and DAC8568 contain a software reset feature.
32    /// If the software reset feature is executed, all registers inside the device are reset to default settings; that is,
33    /// all DAC channels are reset to the power-on reset code (power on reset to zero scale for grades A and C; power on reset to midscale for grades B and D).
34    pub async fn reset(&mut self) -> Result<(), DacError> {
35        let message = Message::get_software_reset_message();
36        self.write(message).await
37    }
38
39    /// Write to the DAC via a blocking call on the specified SPI interface
40    async fn write(&mut self, message: Message) -> Result<(), DacError> {
41        let command: [u8; 4] = message.get_payload_bytes();
42
43        self.sync.set_low().unwrap_or_default();
44        let result = self.spi.write(&command).await;
45        self.sync.set_high().unwrap_or_default();
46
47        match result {
48            Ok(v) => Ok(v),
49            Err(_e) => Err(DacError::BusWriteError),
50        }
51    }
52}