linuxcnc_hal/hal_parameter/mod.rs
1//! HAL parameters
2
3#[macro_use]
4mod macros;
5
6mod parameter_trait;
7
8use linuxcnc_hal_sys::{
9 hal_param_bit_new, hal_param_dir_t_HAL_RO as HAL_RO, hal_param_dir_t_HAL_RW as HAL_RW,
10 hal_param_float_new, hal_param_s32_new, hal_param_u32_new,
11};
12pub use parameter_trait::HalParameter;
13
14/// Parameter write mode.
15#[derive(Copy, Clone, Debug)]
16pub enum ParameterPermissions {
17 /// The parameter may only be read by LinuxCNC.
18 ReadOnly = HAL_RO as isize,
19
20 /// The parameter can be both read and written by LinuxCNC.
21 ReadWrite = HAL_RW as isize,
22}
23
24/// A parameter that can be read and written by LinuxCNC
25///
26/// Supported parameter types are as follows
27///
28/// | Type | Storage | Equivalent `linuxcnc_hal_sys` function |
29/// | ------------------| ------- | -------------------------------------- |
30/// | `Parameter<f64>` | `f64` | [`hal_param_float_new`] |
31/// | `Parameter<u32>` | `u32` | [`hal_param_u32_new`] |
32/// | `Parameter<i32>` | `i32` | [`hal_param_s32_new`] |
33/// | `Parameter<bool>` | `bool` | [`hal_param_bit_new`] |
34///
35/// # Examples
36///
37/// ## Create a parameter
38///
39/// This example creates an `Parameter` under `demo-component.named-parameter`.
40///
41/// ```rust,no_run
42/// use linuxcnc_hal::{
43/// error::ParameterRegisterError,
44/// Parameter,
45/// prelude::*,
46/// HalComponent, RegisterResources, Resources,
47/// };
48/// use std::{
49/// error::Error,
50/// thread,
51/// time::{Duration, Instant},
52/// };
53///
54/// struct MyApp {
55/// parameter: Parameter<f64>,
56/// }
57///
58/// impl Resources for MyApp {
59/// type RegisterError = ParameterRegisterError;
60///
61/// fn register_resources(comp: &RegisterResources) -> Result<Self, Self::RegisterError> {
62/// Ok(MyApp {
63/// parameter: comp.register_parameter("named-parameter")?,
64/// })
65/// }
66/// }
67///
68/// fn main() -> Result<(), Box<dyn Error>> {
69/// let comp: HalComponent<MyApp> = HalComponent::new("demo-component")?;
70///
71/// let MyApp { parameter } = comp.resources();
72///
73/// let start = Instant::now();
74///
75/// // Main control loop
76/// while !comp.should_exit() {
77/// parameter.set_value(123.45f64);
78/// thread::sleep(Duration::from_millis(1000));
79/// }
80///
81/// Ok(())
82/// }
83/// ```
84#[derive(Debug)]
85pub struct Parameter<S> {
86 pub(crate) name: String,
87 pub(crate) storage: *mut S,
88}
89
90impl<S> Drop for Parameter<S> {
91 fn drop(&mut self) {
92 debug!("Drop Parameter {}", self.name);
93 }
94}
95
96impl_param!(Parameter, f64, hal_param_float_new);
97impl_param!(Parameter, u32, hal_param_u32_new);
98impl_param!(Parameter, i32, hal_param_s32_new);
99impl_param!(Parameter, bool, hal_param_bit_new);