A safe, high-level interface to LinuxCNC's HAL (Hardware Abstraction Layer) module.
For lower level, unsafe use, see the linuxcnc-hal-sys
crate.
bash
cargo add linuxcnc-hal
Please consider becoming a sponsor so I may continue to maintain this crate in my spare time!
More examples can be found in the examples/
folder.
```rust,norun use linuxcnchal::{hal_pin::HalPinF64, HalComponentBuilder}; use std::{ error::Error, thread, time::{Duration, Instant}, };
fn main() -> Result<(), Boxempty
let mut builder = HalComponentBuilder::new("pins")?;
let input_1 = builder.register_input_pin::<HalPinF64>("input-1")?;
let output_1 = builder.register_output_pin::<HalPinF64>("output-1")?;
// All pins added, component is now ready. This consumes the builder and registers signal
// handlers.
let comp = builder.ready()?;
let start = Instant::now();
// Main control loop
while !comp.should_exit() {
let time = start.elapsed().as_secs() as i32;
// Set output pin to elapsed seconds since component started
output_1.set_value(time.into())?;
// Print the current value of the input pin
println!("Input: {:?}", input_1.value());
// Sleep for 1000ms. This should be a lower time if the component needs to update more
// frequently.
thread::sleep(Duration::from_millis(1000));
}
// The custom implementation of `Drop` for `HalComponent` ensures that `hal_exit()` is called
// at this point. Registered signal handlers are also deregistered.
Ok(())
} ```
bindgen
must be set up correctly for linuxcnc-hal-sys
to work correctly. Follow the requirements section of its docs.
To run and debug any HAL components, the LinuxCNC simulator can be set up. There's a guide here for Linux Mint (and other Debian derivatives).
bash
cargo build
You can also run ./build.sh
to run all the commands that would normally be run in CI.
bash
cargo test
The docs make heavy use of intra-rustdoc-links. To get the links to render correctly, run with nightly:
bash
rustup toolchain add nightly
cargo +nightly doc
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.