diff --git a/autd3-firmware-emulator/src/fpga/emulator/modulation.rs b/autd3-firmware-emulator/src/fpga/emulator/modulation.rs index f3860355..55db1f15 100644 --- a/autd3-firmware-emulator/src/fpga/emulator/modulation.rs +++ b/autd3-firmware-emulator/src/fpga/emulator/modulation.rs @@ -56,9 +56,13 @@ impl FPGAEmulator { } pub fn modulation_buffer(&self, segment: Segment) -> Vec { - (0..self.modulation_cycle(segment)) - .map(|i| self.modulation_at(segment, i)) - .collect() + let mut dst = vec![0; self.modulation_cycle(segment)]; + self.modulation_buffer_inplace(segment, &mut dst); + dst + } + + pub fn modulation_buffer_inplace(&self, segment: Segment, dst: &mut [u8]) { + (0..self.modulation_cycle(segment)).for_each(|i| dst[i] = self.modulation_at(segment, i)); } pub fn modulation_transition_mode(&self) -> TransitionMode { diff --git a/autd3-firmware-emulator/src/fpga/emulator/pwe.rs b/autd3-firmware-emulator/src/fpga/emulator/pwe.rs index 1899c22d..1f525cfe 100644 --- a/autd3-firmware-emulator/src/fpga/emulator/pwe.rs +++ b/autd3-firmware-emulator/src/fpga/emulator/pwe.rs @@ -10,11 +10,18 @@ impl FPGAEmulator { } pub fn pulse_width_encoder_table(&self) -> Vec { + let mut dst = vec![0; 256]; + self.pulse_width_encoder_table_inplace(&mut dst); + dst + } + + pub fn pulse_width_encoder_table_inplace(&self, dst: &mut [u8]) { self.mem .duty_table_bram() .iter() .flat_map(|&d| vec![(d & 0xFF) as u8, (d >> 8) as u8]) - .collect() + .enumerate() + .for_each(|(i, v)| dst[i] = v); } pub fn to_pulse_width(&self, a: EmitIntensity, b: u8) -> u8 { diff --git a/autd3-firmware-emulator/src/fpga/emulator/stm/foci.rs b/autd3-firmware-emulator/src/fpga/emulator/stm/foci.rs index 5755e28f..e2259474 100644 --- a/autd3-firmware-emulator/src/fpga/emulator/stm/foci.rs +++ b/autd3-firmware-emulator/src/fpga/emulator/stm/foci.rs @@ -23,7 +23,7 @@ impl FPGAEmulator { }] as u8 } - pub(crate) fn foci_stm_drives(&self, segment: Segment, idx: usize) -> Vec { + pub(crate) fn foci_stm_drives_inplace(&self, segment: Segment, idx: usize, dst: &mut [Drive]) { let bram = match segment { Segment::S0 => self.mem.stm_bram_0(), Segment::S1 => self.mem.stm_bram_1(), @@ -35,7 +35,8 @@ impl FPGAEmulator { .tr_pos .iter() .take(self.mem.num_transducers) - .map(|&tr| { + .enumerate() + .for_each(|(i, &tr)| { let tr_z = ((tr >> 32) & 0xFFFF) as i16 as i32; let tr_x = ((tr >> 16) & 0xFFFF) as i16 as i32; let tr_y = (tr & 0xFFFF) as i16 as i32; @@ -68,9 +69,8 @@ impl FPGAEmulator { let sin = ((sin / self.num_foci(segment) as u16) >> 1) as usize; let cos = ((cos / self.num_foci(segment) as u16) >> 1) as usize; let phase = self.mem.atan_table[(sin << 7) | cos]; - Drive::new(Phase::new(phase), EmitIntensity::new(intensity)) - }) - .collect() + dst[i] = Drive::new(Phase::new(phase), EmitIntensity::new(intensity)); + }); } pub fn local_tr_pos(&self) -> &[u64] { diff --git a/autd3-firmware-emulator/src/fpga/emulator/stm/gain.rs b/autd3-firmware-emulator/src/fpga/emulator/stm/gain.rs index 54ab5fad..3982ec1f 100644 --- a/autd3-firmware-emulator/src/fpga/emulator/stm/gain.rs +++ b/autd3-firmware-emulator/src/fpga/emulator/stm/gain.rs @@ -6,7 +6,7 @@ use autd3_driver::{ use crate::FPGAEmulator; impl FPGAEmulator { - pub(crate) fn gain_stm_drives(&self, segment: Segment, idx: usize) -> Vec { + pub(crate) fn gain_stm_drives_inplace(&self, segment: Segment, idx: usize, dst: &mut [Drive]) { match segment { Segment::S0 => self.mem.stm_bram_0(), Segment::S1 => self.mem.stm_bram_1(), @@ -15,12 +15,12 @@ impl FPGAEmulator { .iter() .skip(256 * idx) .take(self.mem.num_transducers) - .map(|&d| { - Drive::new( + .enumerate() + .for_each(|(i, &d)| { + dst[i] = Drive::new( Phase::new((d & 0xFF) as u8), EmitIntensity::new(((d >> 8) & 0xFF) as u8), ) }) - .collect() } } diff --git a/autd3-firmware-emulator/src/fpga/emulator/stm/mod.rs b/autd3-firmware-emulator/src/fpga/emulator/stm/mod.rs index 8eb9a6f6..84189373 100644 --- a/autd3-firmware-emulator/src/fpga/emulator/stm/mod.rs +++ b/autd3-firmware-emulator/src/fpga/emulator/stm/mod.rs @@ -93,10 +93,16 @@ impl FPGAEmulator { } pub fn drives_at(&self, segment: Segment, idx: usize) -> Vec { + let mut dst = vec![Drive::null(); self.mem.num_transducers]; + self.drives_at_inplace(segment, idx, &mut dst); + dst + } + + pub fn drives_at_inplace(&self, segment: Segment, idx: usize, dst: &mut [Drive]) { if self.is_stm_gain_mode(segment) { - self.gain_stm_drives(segment, idx) + self.gain_stm_drives_inplace(segment, idx, dst) } else { - self.foci_stm_drives(segment, idx) + self.foci_stm_drives_inplace(segment, idx, dst) } } }