autd3capi_def/dynamic_datagram/
debug.rs

1/*
2 * File: debug.rs
3 * Project: dynamic_datagram
4 * Created Date: 06/12/2023
5 * Author: Shun Suzuki
6 * -----
7 * Last Modified: 06/12/2023
8 * Modified By: Shun Suzuki (suzuki@hapis.k.u-tokyo.ac.jp)
9 * -----
10 * Copyright (c) 2023 Shun Suzuki. All rights reserved.
11 *
12 */
13
14use std::{collections::HashMap, time::Duration};
15
16use super::DynamicDatagram;
17use autd3::prelude::*;
18use autd3_driver::{
19    error::AUTDInternalError,
20    operation::{Operation, TypeTag},
21};
22
23pub struct DynamicConfigureDebugOutputIdx {
24    idx_map: HashMap<usize, u8>,
25}
26
27impl DynamicConfigureDebugOutputIdx {
28    pub fn new(idx_map: HashMap<usize, u8>) -> Self {
29        Self { idx_map }
30    }
31}
32
33pub struct DynamicDebugOutIdxOp {
34    remains: HashMap<usize, usize>,
35    idx_map: HashMap<usize, u8>,
36}
37
38impl Operation for DynamicDebugOutIdxOp {
39    fn pack(&mut self, device: &Device, tx: &mut [u8]) -> Result<usize, AUTDInternalError> {
40        assert_eq!(self.remains[&device.idx()], 1);
41        tx[0] = TypeTag::Debug as u8;
42        tx[2] = self.idx_map.get(&device.idx()).cloned().unwrap_or(0xFF);
43        Ok(4)
44    }
45
46    fn required_size(&self, _: &Device) -> usize {
47        4
48    }
49
50    fn init(&mut self, geometry: &Geometry) -> Result<(), AUTDInternalError> {
51        self.remains = geometry.devices().map(|device| (device.idx(), 1)).collect();
52        Ok(())
53    }
54
55    fn remains(&self, device: &Device) -> usize {
56        self.remains[&device.idx()]
57    }
58
59    fn commit(&mut self, device: &Device) {
60        self.remains.insert(device.idx(), 0);
61    }
62}
63
64impl DynamicDatagram for DynamicConfigureDebugOutputIdx {
65    fn operation(&mut self) -> Result<(Box<dyn Operation>, Box<dyn Operation>), AUTDInternalError> {
66        Ok((
67            Box::new(DynamicDebugOutIdxOp {
68                remains: Default::default(),
69                idx_map: self.idx_map.clone(),
70            }),
71            Box::<autd3_driver::operation::NullOp>::default(),
72        ))
73    }
74
75    fn timeout(&self) -> Option<Duration> {
76        Some(Duration::from_millis(200))
77    }
78}