UNPKG

@linuxcnc-node/hal

Version:

Node.js bindings for LinuxCNC HAL library

142 lines (141 loc) 5.99 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MSG_ALL = exports.MSG_DBG = exports.MSG_INFO = exports.MSG_WARN = exports.MSG_ERR = exports.MSG_NONE = exports.HAL_RW = exports.HAL_RO = exports.HAL_IO = exports.HAL_OUT = exports.HAL_IN = exports.HAL_U64 = exports.HAL_S64 = exports.HAL_U32 = exports.HAL_S32 = exports.HAL_FLOAT = exports.HAL_BIT = exports.setSignalValue = exports.setPinParamValue = exports.pinHasWriter = exports.newSignal = exports.getInfoParams = exports.getInfoSignals = exports.getInfoPins = exports.getValue = exports.disconnect = exports.connect = exports.setMsgLevel = exports.getMsgLevel = exports.componentIsReady = exports.componentExists = exports.component = exports.Param = exports.Pin = exports.HalComponent = void 0; const component_1 = require("./component"); // Renamed to avoid conflict Object.defineProperty(exports, "HalComponent", { enumerable: true, get: function () { return component_1.HalComponent; } }); Object.defineProperty(exports, "Pin", { enumerable: true, get: function () { return component_1.Pin; } }); Object.defineProperty(exports, "Param", { enumerable: true, get: function () { return component_1.Param; } }); let halNative; const addonPathCandidates = [ "../build/Release/hal_addon.node", "../../build/Release/hal_addon.node", // Fallback for debug builds "../../build/Debug/hal_addon.node", ]; for (const candidate of addonPathCandidates) { try { halNative = require(candidate); break; // Found and loaded } catch (e) { if (candidate === addonPathCandidates[addonPathCandidates.length - 1]) { // Last attempt console.error("FATAL: Failed to load linuxcnc-node native addon. Please ensure it's built correctly and that LinuxCNC is in your PATH."); const loadError = e; console.error("Details:", loadError.message); throw new Error("linuxcnc-node native addon could not be loaded."); } } } // --- Exported types and enums --- __exportStar(require("./enums"), exports); // Exports HalType, HalPinDir, etc. // --- Module-level functions --- /** * Creates a new HAL component instance. * @param name The name of the component. * @param prefix Optional prefix for pin/param names. Defaults to component name. * @returns A HalComponent instance. */ const component = (name, prefix) => { // `halNative.HalComponent` is the N-API constructor function const nativeComponentInstance = new halNative.HalComponent(name, prefix); return new component_1.HalComponent(nativeComponentInstance, name, prefix || name); }; exports.component = component; const componentExists = (name) => { return halNative.component_exists(name); }; exports.componentExists = componentExists; const componentIsReady = (name) => { return halNative.component_is_ready(name); }; exports.componentIsReady = componentIsReady; const getMsgLevel = () => { return halNative.get_msg_level(); }; exports.getMsgLevel = getMsgLevel; const setMsgLevel = (level) => { halNative.set_msg_level(level); }; exports.setMsgLevel = setMsgLevel; const connect = (pinName, signalName) => { return halNative.connect(pinName, signalName); }; exports.connect = connect; const disconnect = (pinName) => { return halNative.disconnect(pinName); }; exports.disconnect = disconnect; const getValue = (name) => { return halNative.get_value(name); }; exports.getValue = getValue; const getInfoPins = () => { return halNative.get_info_pins(); }; exports.getInfoPins = getInfoPins; const getInfoSignals = () => { return halNative.get_info_signals(); }; exports.getInfoSignals = getInfoSignals; const getInfoParams = () => { return halNative.get_info_params(); }; exports.getInfoParams = getInfoParams; const newSignal = (signalName, type) => { return halNative.new_sig(signalName, type); }; exports.newSignal = newSignal; const pinHasWriter = (pinName) => { return halNative.pin_has_writer(pinName); }; exports.pinHasWriter = pinHasWriter; /** * Sets the value of any HAL pin or parameter. * @param name The full name of the pin or parameter. * @param value The value to set */ const setPinParamValue = (name, value) => { return halNative.set_p(name, String(value)); }; exports.setPinParamValue = setPinParamValue; /** * Sets the value of any unconnected HAL signal. * @param name The full name of the signal. * @param value The value to set */ const setSignalValue = (name, value) => { return halNative.set_s(name, String(value)); }; exports.setSignalValue = setSignalValue; // Re-export constants from the native module for direct use (e.g., hal.HAL_FLOAT) exports.HAL_BIT = halNative.HAL_BIT; exports.HAL_FLOAT = halNative.HAL_FLOAT; exports.HAL_S32 = halNative.HAL_S32; exports.HAL_U32 = halNative.HAL_U32; exports.HAL_S64 = halNative.HAL_S64; exports.HAL_U64 = halNative.HAL_U64; exports.HAL_IN = halNative.HAL_IN; exports.HAL_OUT = halNative.HAL_OUT; exports.HAL_IO = halNative.HAL_IO; exports.HAL_RO = halNative.HAL_RO; exports.HAL_RW = halNative.HAL_RW; exports.MSG_NONE = halNative.MSG_NONE; exports.MSG_ERR = halNative.MSG_ERR; exports.MSG_WARN = halNative.MSG_WARN; exports.MSG_INFO = halNative.MSG_INFO; exports.MSG_DBG = halNative.MSG_DBG; exports.MSG_ALL = halNative.MSG_ALL;