feat(python): add interface file and other small changes
This commit is contained in:
parent
168e881884
commit
c42f892099
8 changed files with 373 additions and 31 deletions
|
|
@ -1,5 +1,110 @@
|
|||
use std::time::Duration;
|
||||
|
||||
use argus_core::signals::{InterpolationMethod, Signal};
|
||||
use pyo3::prelude::*;
|
||||
|
||||
use crate::PyArgusError;
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub enum SignalKind {
|
||||
Bool,
|
||||
Int,
|
||||
UnsignedInt,
|
||||
Float,
|
||||
}
|
||||
|
||||
#[pyclass(name = "Signal", subclass)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PySignal {
|
||||
pub kind: SignalKind,
|
||||
pub interpolation: InterpolationMethod,
|
||||
}
|
||||
|
||||
macro_rules! impl_signals {
|
||||
($ty_name:ident, $ty:ty) => {
|
||||
paste::paste! {
|
||||
#[pyclass(extends=PySignal)]
|
||||
pub struct [<$ty_name Signal>](Signal<$ty>);
|
||||
|
||||
impl [<$ty_name Signal>] {
|
||||
#[inline]
|
||||
fn super_type() -> PySignal {
|
||||
PySignal {
|
||||
interpolation: InterpolationMethod::Linear,
|
||||
kind: SignalKind::$ty_name,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
impl [<$ty_name Signal>] {
|
||||
fn __repr__(&self) -> String {
|
||||
format!("Signal::<{}>::{:?}", stringify!($ty), self.0)
|
||||
}
|
||||
|
||||
/// Create a new empty signal
|
||||
#[new]
|
||||
#[pyo3(signature = ())]
|
||||
fn new() -> (Self, PySignal) {
|
||||
(Self(Signal::new()), Self::super_type())
|
||||
}
|
||||
|
||||
fn __init__(self_: PyRef<'_, Self>) -> PyRef<'_, Self> {
|
||||
self_
|
||||
}
|
||||
|
||||
/// Create a new signal with constant value
|
||||
#[staticmethod]
|
||||
fn constant(py: Python<'_>, value: $ty) -> PyResult<Py<Self>> {
|
||||
Py::new(
|
||||
py,
|
||||
(Self(Signal::constant(value)), Self::super_type())
|
||||
)
|
||||
}
|
||||
|
||||
/// Create a new signal from some finite number of samples
|
||||
#[staticmethod]
|
||||
fn from_samples(samples: Vec<(f64, $ty)>) -> PyResult<Py<Self>> {
|
||||
let ret: Signal<$ty> = samples
|
||||
.into_iter()
|
||||
.map(|(t, v)| (Duration::from_secs_f64(t), v))
|
||||
.collect();
|
||||
Python::with_gil(|py| {
|
||||
Py::new(
|
||||
py,
|
||||
(
|
||||
Self(ret),
|
||||
PySignal {
|
||||
interpolation: InterpolationMethod::Linear,
|
||||
kind: SignalKind::$ty_name,
|
||||
},
|
||||
),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/// Push a new sample into the given signal.
|
||||
#[pyo3(signature = (time, value))]
|
||||
fn push(&mut self, time: f64, value: $ty) -> Result<(), PyArgusError> {
|
||||
self.0.push(Duration::from_secs_f64(time), value)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl_signals!(Bool, bool);
|
||||
impl_signals!(Int, i64);
|
||||
impl_signals!(UnsignedInt, u64);
|
||||
impl_signals!(Float, f64);
|
||||
|
||||
pub fn init(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||
m.add_class::<PySignal>()?;
|
||||
m.add_class::<BoolSignal>()?;
|
||||
m.add_class::<IntSignal>()?;
|
||||
m.add_class::<UnsignedIntSignal>()?;
|
||||
m.add_class::<FloatSignal>()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue