1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use crate::hal_pin::{pin_direction::PinDirection, PinWrite};
use linuxcnc_hal_sys::{hal_pin_bit_new, hal_pin_float_new, hal_pin_s32_new, hal_pin_u32_new};

/// A pin that can be written to by the component
///
/// Supported pin types are as follows
///
/// | Type              | Storage | Equivalent `linuxcnc_hal_sys` function |
/// | ----------------- | ------- | -------------------------------------- |
/// | `OutputPin<f64>`  | `f64`   | [`hal_pin_float_new`]                  |
/// | `OutputPin<u32>`  | `u32`   | [`hal_pin_u32_new`]                    |
/// | `OutputPin<i32>`  | `i32`   | [`hal_pin_s32_new`]                    |
/// | `OutputPin<bool>` | `bool`  | [`hal_pin_bit_new`]                    |
///
/// # Examples
///
/// ## Create a pin
///
/// This example creates an `OutputPin` under `demo-component.named-pin`.
///
/// ```rust,no_run
/// use linuxcnc_hal::{
///    error::PinRegisterError,
///    hal_pin::{OutputPin},
///    prelude::*,
///    HalComponent, RegisterResources, Resources,
/// };
/// use std::{
///    error::Error,
///    thread,
///    time::{Duration, Instant},
/// };
///
/// struct Pins {
///    pin: OutputPin<f64>,
/// }
///
/// impl Resources for Pins {
///    type RegisterError = PinRegisterError;
///
///    fn register_resources(comp: &RegisterResources) -> Result<Self, Self::RegisterError> {
///        Ok(Pins {
///            pin: comp.register_pin::<OutputPin<f64>>("named-pin")?,
///        })
///    }
/// }
///
/// fn main() -> Result<(), Box<dyn Error>> {
///    let comp: HalComponent<Pins> = HalComponent::new("demo-component")?;
///
///    let Pins { pin } = comp.resources();
///
///    let start = Instant::now();
///
///         // Main control loop
///     while !comp.should_exit() {
///         pin.set_value(123.45f64);
///         thread::sleep(Duration::from_millis(1000));
///     }
///
///    Ok(())
/// }
/// ```
#[derive(Debug)]
pub struct OutputPin<S> {
    pub(crate) name: String,
    pub(crate) storage: *mut *mut S,
}

impl<S> Drop for OutputPin<S> {
    fn drop(&mut self) {
        debug!("Drop OutputPin {}", self.name);
    }
}

impl_pin!(OutputPin, f64, hal_pin_float_new, PinDirection::Out);
impl_pin!(OutputPin, u32, hal_pin_u32_new, PinDirection::Out);
impl_pin!(OutputPin, i32, hal_pin_s32_new, PinDirection::Out);
impl_pin!(OutputPin, bool, hal_pin_bit_new, PinDirection::Out);

impl PinWrite for OutputPin<f64> {}
impl PinWrite for OutputPin<u32> {}
impl PinWrite for OutputPin<i32> {}
impl PinWrite for OutputPin<bool> {}