linuxcnc_hal/hal_pin/input_pin.rs
1use crate::hal_pin::{pin_direction::PinDirection, PinRead};
2use linuxcnc_hal_sys::{hal_pin_bit_new, hal_pin_float_new, hal_pin_s32_new, hal_pin_u32_new};
3
4/// An input pin readable by the component
5///
6/// Supported pin types are as follows
7///
8/// | Type | Storage | Equivalent `linuxcnc_hal_sys` function |
9/// | ---------------- | ------- | -------------------------------------- |
10/// | `InputPin<f64>` | `f64` | [`hal_pin_float_new`] |
11/// | `InputPin<u32>` | `u32` | [`hal_pin_u32_new`] |
12/// | `InputPin<i32>` | `i32` | [`hal_pin_s32_new`] |
13/// | `InputPin<bool>` | `bool` | [`hal_pin_bit_new`] |
14///
15/// # Examples
16///
17/// ## Create a pin
18///
19/// This example creates an `InputPin` under `demo-component.named-pin`.
20///
21/// ```rust,no_run
22/// use linuxcnc_hal::{
23/// error::PinRegisterError,
24/// hal_pin::{InputPin},
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: InputPin<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::<InputPin<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/// println!("Input: {:?}", pin.value());
58///
59/// thread::sleep(Duration::from_millis(1000));
60/// }
61///
62/// Ok(())
63/// }
64/// ```
65#[derive(Debug)]
66pub struct InputPin<S> {
67 pub(crate) name: String,
68 pub(crate) storage: *mut *mut S,
69}
70
71impl<S> Drop for InputPin<S> {
72 fn drop(&mut self) {
73 debug!("Drop InputPin {}", self.name);
74 }
75}
76
77impl_pin!(InputPin, f64, hal_pin_float_new, PinDirection::In);
78impl_pin!(InputPin, u32, hal_pin_u32_new, PinDirection::In);
79impl_pin!(InputPin, i32, hal_pin_s32_new, PinDirection::In);
80impl_pin!(InputPin, bool, hal_pin_bit_new, PinDirection::In);
81
82impl PinRead for InputPin<f64> {}
83impl PinRead for InputPin<u32> {}
84impl PinRead for InputPin<i32> {}
85impl PinRead for InputPin<bool> {}