@linuxcnc-node/hal
v2.1.3
Published
Node.js bindings for LinuxCNC HAL library
Maintainers
Readme
HAL Module for Node.js (@linuxcnc-node/hal)
This module provides Node.js bindings for the LinuxCNC Hardware Abstraction Layer (HAL). It allows you to create and interact with HAL components, pins, parameters, and signals directly from JavaScript or TypeScript.
Features
- Create HAL components`.
- Get and set values of pins and parameters.
- Monitoring: Watch pin and parameter value changes with configurable polling intervals and callback functions.
- Global HAL functions:
- Check if components exist or are ready.
- Manage RTAPI message levels.
- Create new signals.
- Connect pins to signals and disconnect them.
- Get and set values of arbitrary pins, parameters, or signals.
- Retrieve information about all pins, signals, or parameters in the system.
Installation
To install the @linuxcnc-node/hal module, use npm or yarn:
npm install @linuxcnc-node/hal
# or
yarn add @linuxcnc-node/halLinuxCNC Build Requirements
The module needs to compile against the LinuxCNC development headers and link against its libraries.
Standard LinuxCNC Installations
For standard LinuxCNC installations (e.g., from a Debian package or run-in-place after ./configure && make):
- The build script will attempt to locate the necessary files automatically.
Custom LinuxCNC Installations
For non-standard or custom LinuxCNC installations:
If LinuxCNC is installed in a custom location you will need to set the following environment variables before running npm install or yarn add:
LINUXCNC_INCLUDE: Path to the directory containing LinuxCNC's include files (e.g.,hal.h,rtapi.h). Example:export LINUXCNC_INCLUDE=/path/to/linuxcnc-dev/includeLINUXCNC_LIB: Path to the directory containing LinuxCNC's compiled libraries (e.g.,liblinuxcnchal.so). Example:export LINUXCNC_LIB=/path/to/linuxcnc-dev/lib
Usage Example
import { HalComponent } from "@linuxcnc-node/hal";
import * as hal from "@linuxcnc-node/hal";
// --- Component Creation ---
const comp = new HalComponent("my-js-comp");
console.log(`Component Name: ${comp.name}`);
console.log(`Component Prefix: ${comp.prefix}`);
// --- Adding Pins and Parameters ---
const outFloatPin = comp.newPin("output.float", "float", "out");
const inBitPin = comp.newPin("input.bit", "bit", "in");
const rwS32Param = comp.newParam("param.s32", "s32", "rw");
// --- Making Component Ready ---
comp.ready();
console.log(`Is 'my-js-comp' ready? ${HalComponent.isReady("my-js-comp")}`);
// --- Accessing Pin/Param Values ---
outFloatPin.setValue(123.45);
rwS32Param.setValue(-100);
console.log(`Value of output.float: ${outFloatPin.getValue()}`);
console.log(`Value of input.bit (not connected): ${inBitPin.getValue()}`);
// Or via component methods
comp.setValue("output.float", 456.78);
console.log(`Value via component: ${comp.getValue("output.float")}`);
// --- Global HAL Functions ---
hal.newSignal("my-js-signal", "bit");
hal.connect("my-js-comp.output.float", "another-signal-float");
hal.setSignalValue("my-js-signal", true);
console.log(`Value of 'my-js-signal': ${hal.getValue("my-js-signal")}`);
// --- Information Functions ---
console.log("All Pins:", JSON.stringify(hal.getInfoPins(), null, 2));
// --- Message Levels ---
hal.setMsgLevel("all");
console.log(`Current message level: ${hal.getMsgLevel()}`);
// --- Monitoring System ---
// Set up monitoring with custom polling interval
comp.setMonitoringOptions({ pollInterval: 20 }); // Check every 20ms
// Watch for changes on pins and parameters
outFloatPin.on("change", (newValue, oldValue) => {
console.log(`Output pin changed: ${oldValue} -> ${newValue}`);
});
rwS32Param.on("change", (newValue, oldValue) => {
console.log(`Parameter changed: ${oldValue} -> ${newValue}`);
});
// Remove specific callbacks when no longer needed
const myCallback = (val) => console.log(val);
outFloatPin.on("change", myCallback);
outFloatPin.off("change", myCallback);
// Clean up monitoring when done
comp.dispose(); // Stops all monitoring and cleans up resourcesExample Application
For a comprehensive example of how to use this module in a real-world application, see the HAL View example:
- HAL View Example: A modern Electron-based HAL viewer application that demonstrates advanced usage of the
@linuxcnc-node/halmodule.
Quick Reference
Main Entry Point
new HalComponent(name, prefix?)- Creates a new HAL component
HalComponent Methods
newPin(),newParam()- Create pins and parametersready(),unready()- Control component stategetValue(),setValue()- Get/set values by namegetPins(),getParams()- Retrieve created itemsgetPin(),getParam()- Get specific pin/param by namesetMonitoringOptions()- Configure monitoringdispose()- Clean up resources
Static Methods
HalComponent.exists(name)- Check if component existsHalComponent.isReady(name)- Check if component is ready
Pin/Param Methods
getValue(),setValue()- Get/set valueson("change", cb),off("change", cb)- Monitor value changes
Global Functions
getMsgLevel(),setMsgLevel()- Message level controlconnect(),disconnect()- Pin/signal connectionsnewSignal()- Create signalsgetValue(),setPinParamValue(),setSignalValue()- Value operationsgetInfoPins(),getInfoSignals(),getInfoParams()- Information queriespinHasWriter()- Check pin writer status
Current Limitations
- 64-bit Integers (
s64,u64): JavaScript's nativenumbertype is an IEEE 754 double-precision float. The C++ bindings convert 64-bit HAL integers todoublefor JavaScript. This means that integers larger thanNumber.MAX_SAFE_INTEGER(253-1) or smaller thanNumber.MIN_SAFE_INTEGERwill lose precision. FullBigIntsupport for 64-bit types is not implemented. - No
HAL_PORTSupport: Binding, creation, and interaction withHAL_PORTtype pins or signals are not currently implemented.
Implementation Detail: hal_priv.h
To provide comprehensive bindings that closely match the functionality available in the C API (and subsequently the Python bindings), this library includes hal_priv.h from the LinuxCNC source repository. This header allows access to internal HAL data structures and functions (like halpr_find_comp_by_name, hal_data->pin_list_ptr, etc.). This approach aims for functional parity with halcmd and Python's hal library where possible.
