linuxcnc_hal/hal_pin/output_pin.rs
1use crate::hal_pin::{pin_direction::PinDirection, PinWrite};
2use linuxcnc_hal_sys::{hal_pin_bit_new, hal_pin_float_new, hal_pin_s32_new, hal_pin_u32_new};
3
4/// A pin that can be written to by the component
5///
6/// Supported pin types are as follows
7///
8/// | Type | Storage | Equivalent `linuxcnc_hal_sys` function |
9/// | ----------------- | ------- | -------------------------------------- |
10/// | `OutputPin<f64>` | `f64` | [`hal_pin_float_new`] |
11/// | `OutputPin<u32>` | `u32` | [`hal_pin_u32_new`] |
12/// | `OutputPin<i32>` | `i32` | [`hal_pin_s32_new`] |
13/// | `OutputPin<bool>` | `bool` | [`hal_pin_bit_new`] |
14///
15/// # Examples
16///
17/// ## Create a pin
18///
19/// This example creates an `OutputPin` under `demo-component.named-pin`.
20///
21/// ```rust,no_run
22/// use linuxcnc_hal::{
23/// error::PinRegisterError,
24/// hal_pin::{OutputPin},
25/// prelude::*,
26/// HalComponent, RegisterResources, Resources,
27/// };
28/// use std::{
29/// error::Error,
30/// thread,
31/// time::{Duration, Instant},
32/// };
33///
34/// struct Pins {
35/// pin: OutputPin<f64>,
36/// }
37///
38/// impl Resources for Pins {
39/// type RegisterError = PinRegisterError;
40///
41/// fn register_resources(comp: &RegisterResources) -> Result<Self, Self::RegisterError> {
42/// Ok(Pins {
43/// pin: comp.register_pin::<OutputPin<f64>>("named-pin")?,
44/// })
45/// }
46/// }
47///
48/// fn main() -> Result<(), Box<dyn Error>> {
49/// let comp: HalComponent<Pins> = HalComponent::new("demo-component")?;
50///
51/// let Pins { pin } = comp.resources();
52///
53/// let start = Instant::now();
54///
55/// // Main control loop
56/// while !comp.should_exit() {
57/// pin.set_value(123.45f64);
58/// thread::sleep(Duration::from_millis(1000));
59/// }
60///
61/// Ok(())
62/// }
63/// ```
64#[derive(Debug)]
65pub struct OutputPin<S> {
66 pub(crate) name: String,
67 pub(crate) storage: *mut *mut S,
68}
69
70impl<S> Drop for OutputPin<S> {
71 fn drop(&mut self) {
72 debug!("Drop OutputPin {}", self.name);
73 }
74}
75
76impl_pin!(OutputPin, f64, hal_pin_float_new, PinDirection::Out);
77impl_pin!(OutputPin, u32, hal_pin_u32_new, PinDirection::Out);
78impl_pin!(OutputPin, i32, hal_pin_s32_new, PinDirection::Out);
79impl_pin!(OutputPin, bool, hal_pin_bit_new, PinDirection::Out);
80
81impl PinWrite for OutputPin<f64> {}
82impl PinWrite for OutputPin<u32> {}
83impl PinWrite for OutputPin<i32> {}
84impl PinWrite for OutputPin<bool> {}