linuxcnc_hal/hal_pin/bidirectional_pin.rs
1use crate::hal_pin::{pin_direction::PinDirection, PinRead, 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 both read from and written to
5///
6/// Supported pin types are as follows
7///
8/// | Type | Storage | Equivalent `linuxcnc_hal_sys` function |
9/// | ------------------------ | ------- | -------------------------------------- |
10/// | `BidirectionalPin<f64>` | `f64` | [`hal_pin_float_new`] |
11/// | `BidirectionalPin<u32>` | `u32` | [`hal_pin_u32_new`] |
12/// | `BidirectionalPin<i32>` | `i32` | [`hal_pin_s32_new`] |
13/// | `BidirectionalPin<bool>` | `bool` | [`hal_pin_bit_new`] |
14///
15/// # Examples
16///
17/// ## Create a pin
18///
19/// This example creates a `BidirectionalPin` under `demo-component.named-pin`.
20///
21/// ```rust,no_run
22/// use linuxcnc_hal::{
23/// error::PinRegisterError,
24/// hal_pin::{BidirectionalPin},
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: BidirectionalPin<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::<BidirectionalPin<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/// pin.set_value(123.45f64);
60///
61/// thread::sleep(Duration::from_millis(1000));
62/// }
63///
64/// Ok(())
65/// }
66/// ```
67#[derive(Debug)]
68pub struct BidirectionalPin<S> {
69 pub(crate) name: String,
70 pub(crate) storage: *mut *mut S,
71}
72
73impl<S> Drop for BidirectionalPin<S> {
74 fn drop(&mut self) {
75 debug!("Drop BidirectionalPin {}", self.name);
76 }
77}
78
79impl_pin!(
80 BidirectionalPin,
81 f64,
82 hal_pin_float_new,
83 PinDirection::Bidirectional
84);
85impl_pin!(
86 BidirectionalPin,
87 u32,
88 hal_pin_u32_new,
89 PinDirection::Bidirectional
90);
91impl_pin!(
92 BidirectionalPin,
93 i32,
94 hal_pin_s32_new,
95 PinDirection::Bidirectional
96);
97impl_pin!(
98 BidirectionalPin,
99 bool,
100 hal_pin_bit_new,
101 PinDirection::Bidirectional
102);
103
104impl PinWrite for BidirectionalPin<f64> {}
105impl PinWrite for BidirectionalPin<u32> {}
106impl PinWrite for BidirectionalPin<i32> {}
107impl PinWrite for BidirectionalPin<bool> {}
108
109impl PinRead for BidirectionalPin<f64> {}
110impl PinRead for BidirectionalPin<u32> {}
111impl PinRead for BidirectionalPin<i32> {}
112impl PinRead for BidirectionalPin<bool> {}