a121_rs/
num.rs

1use num::Complex;
2
3use a121_sys::{
4    acc_int16_complex_t, acc_processing_meter_to_points, acc_processing_points_to_meter,
5};
6
7#[derive(Debug, Clone)]
8pub struct AccComplex {
9    inner: acc_int16_complex_t,
10}
11
12impl AccComplex {
13    pub fn new() -> Self {
14        Self::default()
15    }
16
17    /// Creates a new `AccComplex` instance from a raw pointer.
18    ///
19    /// # Safety
20    /// This function is unsafe because it dereferences a raw pointer.
21    /// The caller must ensure that the pointer is valid and points to a properly initialized `acc_int16_complex_t` struct.
22    pub unsafe fn from_ptr(ptr: *const acc_int16_complex_t) -> Self {
23        Self { inner: *ptr }
24    }
25
26    /// Returns a mutable pointer to the inner `acc_int16_complex_t` struct.
27    /// # Safety
28    /// This function is unsafe because it returns a raw pointer.
29    pub unsafe fn mut_ptr(&mut self) -> *mut acc_int16_complex_t {
30        &mut self.inner
31    }
32
33    pub fn ptr(&self) -> *const acc_int16_complex_t {
34        &self.inner
35    }
36}
37
38impl Default for AccComplex {
39    fn default() -> Self {
40        Self {
41            inner: acc_int16_complex_t { real: 0, imag: 0 },
42        }
43    }
44}
45
46impl From<AccComplex> for Complex<i16> {
47    fn from(acc_complex: AccComplex) -> Self {
48        Complex::new(acc_complex.inner.real, acc_complex.inner.imag)
49    }
50}
51
52impl From<Complex<i16>> for AccComplex {
53    fn from(complex: Complex<i16>) -> Self {
54        Self {
55            inner: acc_int16_complex_t {
56                real: complex.re,
57                imag: complex.im,
58            },
59        }
60    }
61}
62
63#[derive(Default)]
64pub struct Points {
65    pub points: i32,
66}
67
68impl Points {
69    pub fn new(points: i32) -> Self {
70        Self { points }
71    }
72
73    pub fn to_meters(&self) -> f32 {
74        unsafe { acc_processing_points_to_meter(self.points) }
75    }
76
77    pub fn meters_to_points(meters: f32) -> Self {
78        let points = unsafe { acc_processing_meter_to_points(meters) };
79        Self { points }
80    }
81}