1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * File: lib.rs
 * Project: src
 * Created Date: 28/04/2022
 * Author: Shun Suzuki
 * -----
 * Last Modified: 27/07/2023
 * Modified By: Shun Suzuki (suzuki@hapis.k.u-tokyo.ac.jp)
 * -----
 * Copyright (c) 2022-2023 Shun Suzuki. All rights reserved.
 *
 */

use proc_macro::TokenStream;
use quote::quote;

#[proc_macro_derive(Modulation)]
pub fn modulation_derive(input: TokenStream) -> TokenStream {
    let ast = syn::parse(input).unwrap();
    impl_modulation_macro(&ast)
}

fn impl_modulation_macro(ast: &syn::DeriveInput) -> TokenStream {
    let name = &ast.ident;
    let generics = &ast.generics;
    let linetimes_prop = generics.lifetimes();
    let linetimes_impl = generics.lifetimes();
    let linetimes_datagram = generics.lifetimes();
    let type_params_prop = generics.type_params();
    let type_params_impl = generics.type_params();
    let type_params_datagram = generics.type_params();
    let (_, ty_generics, where_clause) = generics.split_for_impl();
    let gen = quote! {
        impl <#(#linetimes_prop,)* #(#type_params_prop,)*> autd3_core::modulation::ModulationProperty for #name #ty_generics #where_clause {
            fn sampling_frequency_division(&self) -> u32 {
                self.freq_div
            }

            fn sampling_frequency(&self) -> autd3_core::float {
                autd3_core::FPGA_SUB_CLK_FREQ as autd3_core::float / self.freq_div as autd3_core::float
            }
        }

        impl <#(#linetimes_impl,)* #(#type_params_impl,)*> #name #ty_generics #where_clause {
            /// Set sampling frequency division
            ///
            /// # Arguments
            ///
            /// * `freq_div` - Sampling frequency division. The sampling frequency will be [autd3_core::FPGA_SUB_CLK_FREQ] / `freq_div`. The value must be and must be at least [autd3_core::SAMPLING_FREQ_DIV_MIN]
            ///
            #[allow(clippy::needless_update)]
            pub fn with_sampling_frequency_division(self, freq_div: u32) -> Self {
                Self {freq_div, ..self}
            }

            /// Set sampling frequency
            ///
            /// # Arguments
            ///
            /// * `freq` - Sampling frequency. The sampling frequency closest to `freq` from the possible sampling frequencies is set.
            ///
            #[allow(clippy::needless_update)]
            pub fn with_sampling_frequency(self, freq: autd3_core::float) -> Self {
                let freq_div = autd3_core::FPGA_SUB_CLK_FREQ as autd3_core::float / freq;
                self.with_sampling_frequency_division(freq_div as _)
            }

            /// Set sampling period
            ///
            /// # Arguments
            ///
            /// * `period` - Sampling period. The sampling period closest to `period` from the possible sampling periods is set.
            ///
            #[allow(clippy::needless_update)]
            pub fn with_sampling_period(self, period: std::time::Duration) -> Self {
                let freq_div = autd3_core::FPGA_SUB_CLK_FREQ as autd3_core::float / 1000000000. * period.as_nanos() as autd3_core::float;
                self.with_sampling_frequency_division(freq_div as _)
            }
        }

        impl <#(#linetimes_datagram,)* #(#type_params_datagram,)* T: autd3_core::geometry::Transducer> autd3_core::datagram::Datagram<T> for #name #ty_generics #where_clause {
            type H = autd3_core::Modulation;
            type B = autd3_core::NullBody;

            fn operation(
                &self,
                _geometry: &autd3_core::geometry::Geometry<T>,
            ) -> Result<(Self::H, Self::B), autd3_core::error::AUTDInternalError> {
                let freq_div = self.freq_div;
                Ok((Self::H::new(self.calc()?, freq_div), Self::B::default()))
            }
        }
    };
    gen.into()
}

#[proc_macro_derive(Gain)]
pub fn gain_derive(input: TokenStream) -> TokenStream {
    let ast = syn::parse(input).unwrap();
    impl_gain_macro(ast)
}

fn impl_gain_macro(ast: syn::DeriveInput) -> TokenStream {
    let name = &ast.ident;
    let generics = &ast.generics;
    let linetimes_for_any = generics.lifetimes();
    let linetimes = generics.lifetimes();
    let type_params_for_any = generics.type_params();
    let type_params = generics.type_params();
    let (_, ty_generics, where_clause) = generics.split_for_impl();
    if generics.type_params().any(|ty| ty.ident == "T") {
        let gen = quote! {
            impl <#(#linetimes_for_any,)* #(#type_params_for_any,)*> autd3_core::gain::GainAsAny for #name #ty_generics #where_clause {
                fn as_any(&self) -> &dyn std::any::Any {
                    self
                }
            }

            impl <#(#linetimes,)* #(#type_params,)*> autd3_core::datagram::Datagram<T> for #name #ty_generics #where_clause {
                type H = autd3_core::NullHeader;
                type B = T::Gain;

                fn operation(
                    &self,
                    geometry: &Geometry<T>,
                ) -> Result<(Self::H, Self::B), autd3_core::error::AUTDInternalError> {
                    Ok((Self::H::default(), <Self::B as autd3_core::GainOp>::new(self.calc(geometry)?, || {
                        geometry.transducers().map(|tr| tr.cycle()).collect()
                    })))
                }
            }
        };
        gen.into()
    } else {
        let gen = quote! {
            impl <#(#linetimes_for_any,)* #(#type_params_for_any,)*> autd3_core::gain::GainAsAny for #name #ty_generics #where_clause {
                fn as_any(&self) -> &dyn std::any::Any {
                    self
                }
            }

            impl <#(#linetimes,)* #(#type_params,)* T: autd3_core::geometry::Transducer> autd3_core::datagram::Datagram<T> for #name #ty_generics #where_clause {
                type H = autd3_core::NullHeader;
                type B = T::Gain;

                fn operation(
                    &self,
                    geometry: &Geometry<T>,
                ) -> Result<(Self::H, Self::B), autd3_core::error::AUTDInternalError> {
                    Ok((Self::H::default(), <Self::B as autd3_core::GainOp>::new(self.calc(geometry)?, || {
                        geometry.transducers().map(|tr| tr.cycle()).collect()
                    })))
                }
            }
        };
        gen.into()
    }
}