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 = "environment")))]
21#[cfg(feature = "environment")]
22#[doc(hidden)]
23pub mod environment;
24#[cfg_attr(docsrs, doc(cfg(feature = "ethercat")))]
25#[cfg(feature = "ethercat")]
26/// Definitions for EtherCAT.
27pub mod ethercat;
28#[cfg_attr(docsrs, doc(cfg(feature = "gain")))]
29#[cfg(feature = "gain")]
30/// Core traits for Gain.
31pub mod gain;
32#[cfg_attr(docsrs, doc(cfg(feature = "geometry")))]
33#[cfg(feature = "geometry")]
34/// Geometry related modules.
35pub mod geometry;
36#[cfg_attr(docsrs, doc(cfg(feature = "link")))]
37#[cfg(feature = "link")]
38/// A interface to the device.
39pub mod link;
40#[cfg_attr(docsrs, doc(cfg(feature = "modulation")))]
41#[cfg(feature = "modulation")]
42/// Core traits for Modulation.
43pub mod modulation;
44#[cfg_attr(docsrs, doc(cfg(feature = "sampling_config")))]
45#[cfg(feature = "sampling_config")]
46#[doc(hidden)]
47pub mod sampling_config;
48#[cfg_attr(docsrs, doc(cfg(feature = "sleep")))]
49#[cfg(feature = "sleep")]
50#[doc(hidden)]
51pub mod sleep;
52#[cfg_attr(docsrs, doc(cfg(feature = "utils")))]
53#[cfg(feature = "utils")]
54#[doc(hidden)]
55pub mod utils;
56
57#[cfg_attr(docsrs, doc(cfg(feature = "async-trait")))]
58#[cfg(feature = "async-trait")]
59pub use async_trait::async_trait;
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 GainCalculator for Impl {
84///     fn calc(&self, tr: &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 GainCalculatorGenerator for Impl {
93///     type Calculator = Self;
94///
95///     fn generate(&mut self, _: &Device) -> Self::Calculator {
96///        *self
97///     }
98/// }
99///
100/// impl Gain for FocalPoint {
101///     type G = Impl;
102///
103///     fn init(
104///         self,
105///         _geometry: &Geometry,
106///         env: &Environment,
107///         _filter: &TransducerFilter,
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, _limits: &FirmwareLimits) -> 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::{
155                DatagramOption, DeviceFilter, FirmwareLimits, Inspectable, InspectionResult,
156                Segment, TransitionMode,
157            },
158            environment::Environment,
159            geometry::Geometry,
160        };
161        pub use num_cpus;
162        pub use tynm;
163    }
164    #[cfg(any(feature = "gain", feature = "modulation"))]
165    pub use common::*;
166
167    #[cfg(feature = "gain")]
168    mod gain {
169        pub use crate::{
170            datagram::DatagramS,
171            gain::{
172                Drive, Gain, GainCalculator, GainCalculatorGenerator, GainError,
173                GainInspectionResult, GainOperationGenerator, Intensity, Phase, TransducerFilter,
174            },
175            geometry::{Device, Transducer},
176        };
177        pub use autd3_derive::Gain;
178    }
179    #[cfg(feature = "gain")]
180    pub use gain::*;
181
182    #[cfg(feature = "modulation")]
183    mod modulation {
184        pub use crate::datagram::{Datagram, DatagramL, LoopBehavior};
185        pub use crate::modulation::{
186            Modulation, ModulationError, ModulationInspectionResult, ModulationOperationGenerator,
187        };
188        pub use crate::sampling_config::{SamplingConfig, SamplingConfigError};
189        pub use autd3_derive::Modulation;
190        pub use std::{collections::HashMap, sync::Arc};
191    }
192    #[cfg(feature = "modulation")]
193    pub use modulation::*;
194}