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