autd3_core/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![warn(missing_docs)]
3#![warn(rustdoc::missing_crate_level_docs)]
4#![warn(rustdoc::unescaped_backticks)]
5
6//! Core traits and types for AUTD3.
7
8#[cfg_attr(docsrs, doc(cfg(feature = "acoustics")))]
9#[cfg(feature = "acoustics")]
10/// Utilities for acoustics.
11pub mod acoustics;
12#[cfg_attr(docsrs, doc(cfg(feature = "common")))]
13#[cfg(feature = "common")]
14/// Common constants and types.
15pub mod common;
16#[cfg_attr(docsrs, doc(cfg(feature = "datagram")))]
17#[cfg(feature = "datagram")]
18/// Core traits for Datagram.
19pub mod datagram;
20#[cfg_attr(docsrs, doc(cfg(feature = "devices")))]
21#[cfg(feature = "devices")]
22/// Definitions for devices.
23pub mod devices;
24#[cfg_attr(docsrs, doc(cfg(feature = "environment")))]
25#[cfg(feature = "environment")]
26#[doc(hidden)]
27pub mod environment;
28#[cfg_attr(docsrs, doc(cfg(feature = "ethercat")))]
29#[cfg(feature = "ethercat")]
30/// Definitions for EtherCAT.
31pub mod ethercat;
32#[cfg_attr(docsrs, doc(cfg(feature = "firmware")))]
33#[cfg(feature = "firmware")]
34/// Firmware related modules.
35pub mod firmware;
36#[cfg_attr(docsrs, doc(cfg(feature = "gain")))]
37#[cfg(feature = "gain")]
38/// Core traits for Gain.
39pub mod gain;
40#[cfg_attr(docsrs, doc(cfg(feature = "geometry")))]
41#[cfg(feature = "geometry")]
42/// Geometry related modules.
43pub mod geometry;
44#[cfg_attr(docsrs, doc(cfg(feature = "link")))]
45#[cfg(feature = "link")]
46/// A interface to the device.
47pub mod link;
48#[cfg_attr(docsrs, doc(cfg(feature = "modulation")))]
49#[cfg(feature = "modulation")]
50/// Core traits for Modulation.
51pub mod modulation;
52#[cfg_attr(docsrs, doc(cfg(feature = "sleep")))]
53#[cfg(feature = "sleep")]
54#[doc(hidden)]
55pub mod sleep;
56#[cfg_attr(docsrs, doc(cfg(feature = "utils")))]
57#[cfg(feature = "utils")]
58#[doc(hidden)]
59pub mod utils;
60
61/// Utilities for user-common [`Gain`] and [`Modulation`].
62///
63/// # Example
64///
65/// The following example shows how to define a custom [`Gain`] that generates a single focal point.
66///
67/// ```
68/// use autd3_core::derive::*;
69/// use autd3_core::geometry::Point3;
70/// use autd3_core::common::rad;
71///
72/// #[derive(Gain, Debug)]
73/// pub struct FocalPoint {
74///     pos: Point3,
75/// }
76///
77/// #[derive(Clone, Copy)]
78/// pub struct Impl {
79///     pos: Point3,
80///     wavenumber: f32,
81/// }
82///
83/// impl<'a> GainCalculator<'a> for Impl {
84///     fn calc(&self, tr: &'a Transducer) -> Drive {
85///         Drive {
86///             phase: Phase::from(-(self.pos - tr.position()).norm() * self.wavenumber * rad),
87///             intensity: Intensity::MAX,
88///         }
89///     }
90/// }
91///
92/// impl<'a> GainCalculatorGenerator<'a> for Impl {
93///     type Calculator = Self;
94///
95///     fn generate(&mut self, _: &'a Device) -> Self::Calculator {
96///        *self
97///     }
98/// }
99///
100/// impl<'a> Gain<'a> for FocalPoint {
101///     type G = Impl;
102///
103///     fn init(
104///         self,
105///         _geometry: &'a Geometry,
106///         env: &Environment,
107///         _filter: &TransducerMask ,
108///     ) -> Result<Self::G, GainError> {
109///         Ok(Impl {
110///             pos: self.pos,
111///             wavenumber: env.wavenumber(),
112///         })
113///     }
114/// }
115/// ```
116///
117/// The following example shows how to define a modulation that outputs the maximum value only for a moment.
118///
119/// ```
120/// use autd3_core::common::kHz;
121/// use autd3_core::derive::*;
122///
123/// #[derive(Modulation, Debug)]
124/// pub struct Burst {
125/// }
126///
127/// impl Burst {
128///     pub fn new() -> Self {
129///         Self {}
130///     }
131/// }
132///
133/// impl Modulation for Burst {
134///     fn calc(self) -> Result<Vec<u8>, ModulationError>  {
135///         Ok((0..4000)
136///             .map(|i| if i == 3999 { u8::MAX } else { u8::MIN })
137///             .collect())
138///     }
139///
140///     fn sampling_config(&self) -> SamplingConfig {
141///         SamplingConfig::new(4. * kHz)
142///     }
143/// }
144/// ```
145///
146/// [`Gain`]: crate::gain::Gain
147/// [`Modulation`]: crate::modulation::Modulation
148#[cfg_attr(docsrs, doc(cfg(feature = "derive")))]
149#[cfg(feature = "derive")]
150pub mod derive {
151    #[cfg(any(feature = "gain", feature = "modulation"))]
152    mod common {
153        pub use crate::{
154            datagram::{DatagramOption, DeviceMask, Inspectable, InspectionResult, internal},
155            environment::Environment,
156            firmware::{Segment, transition_mode},
157            geometry::Geometry,
158        };
159    }
160    #[cfg(any(feature = "gain", feature = "modulation"))]
161    pub use common::*;
162
163    #[cfg(feature = "gain")]
164    mod gain {
165        pub use crate::{
166            datagram::DatagramS,
167            firmware::{Drive, Intensity, Phase},
168            gain::{
169                DeviceTransducerMask, Gain, GainCalculator, GainCalculatorGenerator, GainError,
170                GainInspectionResult, GainOperationGenerator, TransducerMask,
171            },
172            geometry::{Device, Transducer},
173        };
174        pub use autd3_derive::Gain;
175    }
176    #[cfg(feature = "gain")]
177    pub use gain::*;
178
179    #[cfg(feature = "modulation")]
180    mod modulation {
181        pub use crate::datagram::{Datagram, DatagramL};
182        pub use crate::firmware::{SamplingConfig, SamplingConfigError};
183        pub use crate::modulation::{
184            Modulation, ModulationError, ModulationInspectionResult, ModulationOperationGenerator,
185        };
186        pub use autd3_derive::Modulation;
187    }
188    #[cfg(feature = "modulation")]
189    pub use modulation::*;
190}