autd3_firmware_emulator/fpga/emulator/
pwe.rs

1use autd3_driver::firmware::fpga::{EmitIntensity, PulseWidth};
2
3use super::FPGAEmulator;
4
5impl FPGAEmulator {
6    #[must_use]
7    pub fn pulse_width_encoder_table_at(&self, idx: usize) -> PulseWidth<u16, 9> {
8        PulseWidth::new(self.mem.duty_table_bram.borrow()[idx]).unwrap()
9    }
10
11    #[must_use]
12    pub fn pulse_width_encoder_table(&self) -> Vec<PulseWidth<u16, 9>> {
13        let mut dst = vec![PulseWidth::new(0).unwrap(); 256];
14        self.pulse_width_encoder_table_inplace(&mut dst);
15        dst
16    }
17
18    pub fn pulse_width_encoder_table_inplace(&self, dst: &mut [PulseWidth<u16, 9>]) {
19        dst.iter_mut().enumerate().for_each(|(i, d)| {
20            *d = self.pulse_width_encoder_table_at(i);
21        });
22    }
23
24    #[must_use]
25    pub fn to_pulse_width(&self, a: EmitIntensity, b: u8) -> PulseWidth<u16, 9> {
26        let key = (a.0 as usize * b as usize) / 255;
27        dbg!(key);
28        dbg!(self.pulse_width_encoder_table_at(key))
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35
36    static ASIN_TABLE: &[u8; 512] = include_bytes!("asin.dat");
37
38    fn to_pulse_width_actual(a: u8, b: u8) -> PulseWidth<u16, 9> {
39        let idx = (a as usize * b as usize) / 255;
40        PulseWidth::new(u16::from_le_bytes([
41            ASIN_TABLE[(idx << 1) + 1],
42            ASIN_TABLE[idx << 1],
43        ]))
44        .unwrap()
45    }
46
47    #[test]
48    fn test_to_pulse_width() {
49        let fpga = FPGAEmulator::new(249);
50        itertools::iproduct!(0x00..=0xFF, 0x00..=0xFF).for_each(|(a, b)| {
51            assert_eq!(
52                to_pulse_width_actual(a, b),
53                fpga.to_pulse_width(EmitIntensity(a), b)
54            );
55        });
56    }
57}