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