autd3_driver/datagram/
output_mask.rs

1use std::convert::Infallible;
2
3use autd3_core::{
4    datagram::{Datagram, DatagramOption, DeviceMask},
5    environment::Environment,
6    firmware::Segment,
7    geometry::{Device, Geometry, Transducer},
8};
9
10/// [`Datagram`] to mask output.
11///
12/// The transducers set to `false` in [`OutputMask`] will not output ultrasound regardless of the intensity settings by [`Gain`], [`FociSTM`], and [`GainSTM`].
13///
14/// # Example
15///
16/// ```
17/// # use autd3_driver::datagram::OutputMask;
18/// OutputMask::new(|_dev| |_tr| true);
19/// ```
20///
21/// [`Datagram`]: autd3_core::datagram::Datagram
22/// [`Gain`]: autd3_core::gain::Gain
23/// [`FociSTM`]: crate::datagram::FociSTM
24/// [`GainSTM`]: crate::datagram::GainSTM
25#[derive(Debug)]
26pub struct OutputMask<F, FT> {
27    #[doc(hidden)]
28    pub f: F,
29    /// The segment to which this [`OutputMask`] applies.
30    pub segment: Segment,
31    _phantom: std::marker::PhantomData<FT>,
32}
33
34impl<'a, FT: Fn(&'a Transducer) -> bool, F: Fn(&'a Device) -> FT> OutputMask<F, FT> {
35    /// Creates a new [`OutputMask`].
36    #[must_use]
37    pub const fn new(f: F) -> Self {
38        Self::with_segment(f, Segment::S0)
39    }
40
41    /// Creates a new [`OutputMask`] for a segment.
42    pub const fn with_segment(f: F, segment: Segment) -> Self {
43        Self {
44            f,
45            segment,
46            _phantom: std::marker::PhantomData,
47        }
48    }
49}
50
51pub struct OutputMaskOperationGenerator<F> {
52    pub(crate) f: F,
53    pub(crate) segment: Segment,
54}
55
56impl<'a, FT: Fn(&'a Transducer) -> bool, F: Fn(&'a Device) -> FT> Datagram<'a>
57    for OutputMask<F, FT>
58{
59    type G = OutputMaskOperationGenerator<F>;
60    type Error = Infallible;
61
62    fn operation_generator(
63        self,
64        _: &'a Geometry,
65        _: &Environment,
66        _: &DeviceMask,
67    ) -> Result<Self::G, Self::Error> {
68        Ok(OutputMaskOperationGenerator {
69            f: self.f,
70            segment: self.segment,
71        })
72    }
73
74    fn option(&self) -> DatagramOption {
75        DatagramOption::default()
76    }
77}
78
79#[cfg(test)]
80mod tests {
81    use super::*;
82
83    #[test]
84    fn new() {
85        let datagram = OutputMask::new(|_dev| |_tr| true);
86        assert_eq!(datagram.segment, Segment::S0);
87    }
88}