Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
s5suzuki committed Dec 30, 2023
1 parent 3180181 commit 581672c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 104 deletions.
37 changes: 34 additions & 3 deletions autd3/src/controller/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Created Date: 05/10/2023
* Author: Shun Suzuki
* -----
* Last Modified: 06/11/2023
* Last Modified: 30/12/2023
* Modified By: Shun Suzuki (suzuki@hapis.k.u-tokyo.ac.jp)
* -----
* Copyright (c) 2023 Shun Suzuki. All rights reserved.
Expand Down Expand Up @@ -44,9 +44,27 @@ impl ControllerBuilder {
self,
link_builder: B,
) -> Result<Controller<B::L>, AUTDError> {
use autd3_driver::{
cpu::{RxMessage, TxDatagram},
datagram::{Clear, Synchronize},
};

let geometry = Geometry::new(self.devices);
let link = link_builder.open(&geometry).await?;
Controller::open_impl(geometry, link).await

let num_devices = geometry.num_devices();
let tx_buf = TxDatagram::new(num_devices);
let mut cnt = Controller {
link,
geometry,
tx_buf,
rx_buf: vec![RxMessage { data: 0, ack: 0 }; num_devices],
ignore_ack: true,
};
cnt.send(Clear::new()).await?;
cnt.send(Synchronize::new()).await?;
cnt.ignore_ack = false;
Ok(cnt)
}

/// Open controller
Expand All @@ -57,6 +75,19 @@ impl ControllerBuilder {
) -> Result<Controller<B::L>, AUTDError> {
let geometry = Geometry::new(self.devices);
let link = link_builder.open(&geometry)?;
Controller::open_impl(geometry, link)

let num_devices = geometry.num_devices();
let tx_buf = TxDatagram::new(num_devices);
let mut cnt = Controller {
link,
geometry,
tx_buf,
rx_buf: vec![RxMessage { data: 0, ack: 0 }; num_devices],
ignore_ack: true,
};
cnt.send(Clear::new())?;
cnt.send(Synchronize::new())?;
cnt.ignore_ack = false;
Ok(cnt)
}
}
77 changes: 26 additions & 51 deletions autd3/src/controller/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Created Date: 05/10/2023
* Author: Shun Suzuki
* -----
* Last Modified: 28/12/2023
* Last Modified: 30/12/2023
* Modified By: Shun Suzuki (suzuki@hapis.k.u-tokyo.ac.jp)
* -----
* Copyright (c) 2023 Shun Suzuki. All rights reserved.
Expand Down Expand Up @@ -68,17 +68,24 @@ impl<'a, K: Hash + Eq + Clone, L: Link, F: Fn(&Device) -> Option<K>> GroupGuard<
Ok(self)
}

#[cfg(not(feature = "sync"))]
pub async fn send(mut self) -> Result<bool, AUTDInternalError> {
let enable_flags_store = self
.cnt
fn push_enable_flags(&self) -> Vec<bool> {
self.cnt
.geometry
.iter()
.map(|dev| dev.enable)
.collect::<Vec<_>>();
.collect::<Vec<_>>()
}

let enable_flags_map: HashMap<K, Vec<bool>> = self
.op
fn pop_enable_flags(&mut self, enable_flags_store: Vec<bool>) {
self.cnt
.geometry
.iter_mut()
.zip(enable_flags_store.iter())
.for_each(|(dev, &enable)| dev.enable = enable);
}

fn get_enable_flags_map(&self) -> HashMap<K, Vec<bool>> {
self.op
.keys()
.map(|k| {
(
Expand All @@ -99,15 +106,20 @@ impl<'a, K: Hash + Eq + Clone, L: Link, F: Fn(&Device) -> Option<K>> GroupGuard<
.collect(),
)
})
.collect();
.collect()
}

#[cfg(not(feature = "sync"))]
pub async fn send(mut self) -> Result<bool, AUTDInternalError> {
let enable_flags_store = self.push_enable_flags();
let enable_flags_map = self.get_enable_flags_map();

self.op.iter_mut().try_for_each(|(k, (op1, op2))| {
self.cnt.geometry.iter_mut().for_each(|dev| {
dev.enable = enable_flags_map[k][dev.idx()];
});
OperationHandler::init(op1, op2, &self.cnt.geometry)
})?;

let r = loop {
let start = std::time::Instant::now();
self.op.iter_mut().try_for_each(|(k, (op1, op2))| {
Expand Down Expand Up @@ -143,55 +155,22 @@ impl<'a, K: Hash + Eq + Clone, L: Link, F: Fn(&Device) -> Option<K>> GroupGuard<
}
};

self.cnt
.geometry
.iter_mut()
.zip(enable_flags_store.iter())
.for_each(|(dev, &enable)| dev.enable = enable);
self.pop_enable_flags(enable_flags_store);

Ok(r)
}

#[cfg(feature = "sync")]
pub fn send(mut self) -> Result<bool, AUTDInternalError> {
let enable_flags_store = self
.cnt
.geometry
.iter()
.map(|dev| dev.enable)
.collect::<Vec<_>>();

let enable_flags_map: HashMap<K, Vec<bool>> = self
.op
.keys()
.map(|k| {
(
k.clone(),
self.cnt
.geometry
.iter()
.map(|dev| {
if !dev.enable {
return false;
}
if let Some(kk) = (self.f)(dev) {
kk == *k
} else {
false
}
})
.collect(),
)
})
.collect();
let enable_flags_store = self.push_enable_flags();
let enable_flags_map = self.get_enable_flags_map();

self.op.iter_mut().try_for_each(|(k, (op1, op2))| {
self.cnt.geometry.iter_mut().for_each(|dev| {
dev.enable = enable_flags_map[k][dev.idx()];
});
OperationHandler::init(op1, op2, &self.cnt.geometry)
})?;

let r = loop {
let start = std::time::Instant::now();
self.op.iter_mut().try_for_each(|(k, (op1, op2))| {
Expand Down Expand Up @@ -221,11 +200,7 @@ impl<'a, K: Hash + Eq + Clone, L: Link, F: Fn(&Device) -> Option<K>> GroupGuard<
}
};

self.cnt
.geometry
.iter_mut()
.zip(enable_flags_store.iter())
.for_each(|(dev, &enable)| dev.enable = enable);
self.pop_enable_flags(enable_flags_store);

Ok(r)
}
Expand Down
44 changes: 2 additions & 42 deletions autd3/src/controller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Created Date: 05/10/2023
* Author: Shun Suzuki
* -----
* Last Modified: 29/12/2023
* Last Modified: 30/12/2023
* Modified By: Shun Suzuki (suzuki@hapis.k.u-tokyo.ac.jp)
* -----
* Copyright (c) 2023 Shun Suzuki. All rights reserved.
Expand All @@ -18,7 +18,7 @@ use std::{collections::HashMap, hash::Hash, time::Duration};

use autd3_driver::{
cpu::{RxMessage, TxDatagram},
datagram::{Clear, Datagram, Synchronize},
datagram::{Clear, Datagram},
firmware_version::FirmwareInfo,
fpga::FPGAInfo,
geometry::{Device, Geometry},
Expand Down Expand Up @@ -73,46 +73,6 @@ impl<L: Link> Controller<L> {
}
}

#[cfg(not(feature = "sync"))]
impl<L: Link> Controller<L> {
#[doc(hidden)]
pub async fn open_impl(geometry: Geometry, link: L) -> Result<Controller<L>, AUTDError> {
let num_devices = geometry.num_devices();
let tx_buf = TxDatagram::new(num_devices);
let mut cnt = Controller {
link,
geometry,
tx_buf,
rx_buf: vec![RxMessage { data: 0, ack: 0 }; num_devices],
ignore_ack: true,
};
cnt.send(Clear::new()).await?;
cnt.send(Synchronize::new()).await?;
cnt.ignore_ack = false;
Ok(cnt)
}
}

#[cfg(feature = "sync")]
impl<L: Link> Controller<L> {
#[doc(hidden)]
pub fn open_impl(geometry: Geometry, link: L) -> Result<Controller<L>, AUTDError> {
let num_devices = geometry.num_devices();
let tx_buf = TxDatagram::new(num_devices);
let mut cnt = Controller {
link,
geometry,
tx_buf,
rx_buf: vec![RxMessage { data: 0, ack: 0 }; num_devices],
ignore_ack: true,
};
cnt.send(Clear::new())?;
cnt.send(Synchronize::new())?;
cnt.ignore_ack = false;
Ok(cnt)
}
}

#[cfg(not(feature = "sync"))]
impl<L: Link> Controller<L> {
/// Send data to the devices
Expand Down
13 changes: 5 additions & 8 deletions autd3/src/modulation/static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Created Date: 30/04/2022
* Author: Shun Suzuki
* -----
* Last Modified: 29/12/2023
* Last Modified: 30/12/2023
* Modified By: Shun Suzuki (suzuki@hapis.k.u-tokyo.ac.jp)
* -----
* Copyright (c) 2022-2023 Shun Suzuki. All rights reserved.
Expand All @@ -26,10 +26,7 @@ pub struct Static {
impl Static {
/// constructor
pub fn new() -> Self {
Self {
intensity: EmitIntensity::MAX,
config: SamplingConfiguration::from_frequency_division(0xFFFFFFFF).unwrap(),
}
Self::with_intensity(EmitIntensity::MAX)
}

/// set emission intensity
Expand All @@ -38,10 +35,10 @@ impl Static {
///
/// * `intensity` - normalized emission intensity of the ultrasound (from 0 to 1)
///
pub fn with_intensity<A: Into<EmitIntensity>>(self, intensity: A) -> Self {
pub fn with_intensity<A: Into<EmitIntensity>>(intensity: A) -> Self {
Self {
intensity: intensity.into(),
..self
config: SamplingConfiguration::from_frequency_division(0xFFFFFFFF).unwrap(),
}
}

Expand Down Expand Up @@ -88,7 +85,7 @@ mod tests {

#[test]
fn test_static_with_intensity() {
let m = Static::new().with_intensity(0x1F);
let m = Static::with_intensity(0x1F);
assert_eq!(m.intensity, EmitIntensity::new(0x1F));
assert_eq!(
m.calc().unwrap(),
Expand Down

0 comments on commit 581672c

Please sign in to comment.