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
/*
 * File: gain.rs
 * Project: src
 * Created Date: 22/05/2020
 * Author: Shun Suzuki
 * -----
 * Last Modified: 22/05/2020
 * Modified By: Shun Suzuki (suzuki@hapis.k.u-tokyo.ac.jp)
 * -----
 * Copyright (c) 2020 Hapis Lab. All rights reserved.
 *
 */

use crate::geometry::Geometry;
use std::f64::consts::PI;

/// Gain contains the amplitude and phase of each transducer in the AUTD.
pub trait Gain: Send {
    fn build(&mut self, geometry: &dyn Geometry);
    fn get_data(&self) -> &Vec<u8>;
}

/// Convert amplitude and phase of sound to duty ratio and shift duration of Pulse Width Modulation.
pub fn convert_to_pwm_params(amp: u8, phase: u8) -> (u8, u8) {
    let d = (amp as f64 / 255.0).asin() / PI; // duty (0 ~ 0.5)
    let s = ((phase as i32 + 64 - (127.5 * d) as i32) % 256) as u8;
    let d = (510.0 * d) as u8;
    (d, s)
}