a121_rs/
processing.rs

1use core::ffi::c_void;
2
3use metadata::ProcessingMetaData;
4
5use crate::config::RadarConfig;
6use crate::num::AccComplex;
7use a121_sys::{
8    acc_processing_create, acc_processing_destroy, acc_processing_execute, acc_processing_result_t,
9    acc_processing_t,
10};
11
12pub mod metadata;
13
14#[derive(Debug, Clone)]
15pub struct ProcessingResult {
16    inner: acc_processing_result_t,
17    pub frame: AccComplex,
18}
19
20impl ProcessingResult {
21    pub fn new() -> Self {
22        Self::default()
23    }
24
25    /// Get a mutable reference to the frame data
26    /// # Safety
27    /// This function is unsafe because it returns a mutable reference to the frame data, which is a raw pointer
28    pub unsafe fn mut_ptr(&mut self) -> *mut acc_processing_result_t {
29        &mut self.inner
30    }
31
32    pub fn ptr(&self) -> *const acc_processing_result_t {
33        &self.inner
34    }
35}
36
37impl Default for ProcessingResult {
38    fn default() -> Self {
39        let mut frame: AccComplex = AccComplex::new();
40        let inner = acc_processing_result_t {
41            data_saturated: false,
42            frame_delayed: false,
43            calibration_needed: false,
44            temperature: 0,
45            frame: unsafe { frame.mut_ptr() },
46        };
47        Self { inner, frame }
48    }
49}
50
51pub struct Processing {
52    inner: *mut acc_processing_t,
53    metadata: ProcessingMetaData,
54}
55
56impl Processing {
57    pub fn new(config: &RadarConfig) -> Self {
58        let mut metadata = ProcessingMetaData::new();
59        let inner = unsafe { acc_processing_create(config.ptr(), metadata.mut_ptr()) };
60        Self { inner, metadata }
61    }
62
63    pub fn metadata(&self) -> &ProcessingMetaData {
64        &self.metadata
65    }
66
67    pub fn execute(&mut self, buffer: &mut [u8]) -> ProcessingResult {
68        let mut result = ProcessingResult::new();
69        unsafe {
70            acc_processing_execute(
71                self.inner,
72                buffer.as_mut_ptr() as *mut c_void,
73                result.mut_ptr(),
74            );
75        }
76        result
77    }
78}
79
80impl Drop for Processing {
81    fn drop(&mut self) {
82        unsafe {
83            acc_processing_destroy(self.inner);
84        }
85    }
86}
87
88impl From<acc_processing_result_t> for ProcessingResult {
89    fn from(result: acc_processing_result_t) -> Self {
90        let frame = unsafe { AccComplex::from_ptr(result.frame) };
91        Self {
92            inner: result,
93            frame,
94        }
95    }
96}
97
98impl From<ProcessingResult> for acc_processing_result_t {
99    fn from(result: ProcessingResult) -> Self {
100        result.inner
101    }
102}