@linuxcnc-node/hal
Version:
Node.js bindings for LinuxCNC HAL library
158 lines (157 loc) • 6.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.setSignalValue = exports.setPinParamValue = exports.pinHasWriter = exports.newSignal = exports.getInfoParams = exports.getInfoSignals = exports.getInfoPins = exports.getValue = exports.disconnect = exports.connect = exports.setMsgLevel = exports.getMsgLevel = void 0;
const constants_1 = require("./constants");
/**
* Gets the current RTAPI message verbosity level used by HAL.
*
* @returns The current message level (e.g., `hal.MSG_INFO`).
* See {@link RtapiMsgLevel} for available levels.
*/
const getMsgLevel = () => {
const nativeValue = constants_1.halNative.get_msg_level();
return constants_1.RtapiMsgLevelFromValue[nativeValue] ?? "none";
};
exports.getMsgLevel = getMsgLevel;
/**
* Sets the RTAPI message verbosity level.
*
* @param level - The new message level to set. See {@link RtapiMsgLevel} for available levels.
*/
const setMsgLevel = (level) => {
constants_1.halNative.set_msg_level(constants_1.RtapiMsgLevelValue[level]);
};
exports.setMsgLevel = setMsgLevel;
/**
* Links a HAL pin to a HAL signal.
*
* @param pinName - The full name of the pin (e.g., "my-comp.out1").
* @param signalName - The name of the signal to connect to.
* @returns `true` on success, `false` on failure (error is thrown by native layer).
* @throws Error if pin or signal doesn't exist, or if connection fails.
*/
const connect = (pinName, signalName) => {
return constants_1.halNative.connect(pinName, signalName);
};
exports.connect = connect;
/**
* Unlinks a HAL pin from any signal it's currently connected to.
*
* @param pinName - The full name of the pin.
* @returns `true` on success, `false` on failure (error is thrown).
* @throws Error if pin doesn't exist or disconnect fails.
*/
const disconnect = (pinName) => {
return constants_1.halNative.disconnect(pinName);
};
exports.disconnect = disconnect;
/**
* Gets the current value of any HAL item (pin, parameter, or signal) identified by its full name.
*
* @param name - The full name of the pin, parameter, or signal.
* @returns The value of the item (number or boolean).
* @throws Error if the item is not found.
*/
const getValue = (name) => {
return constants_1.halNative.get_value(name);
};
exports.getValue = getValue;
/**
* Retrieves a list of all HAL pins currently in the system.
*
* @returns An array of `HalPinInfo` objects. See {@link HalPinInfo} for structure.
*/
const getInfoPins = () => {
const nativePins = constants_1.halNative.get_info_pins();
return nativePins.map((pin) => ({
...pin,
type: constants_1.HalTypeFromValue[pin.type] ?? "bit",
direction: constants_1.HalPinDirFromValue[pin.direction] ?? "in",
}));
};
exports.getInfoPins = getInfoPins;
/**
* Retrieves a list of all HAL signals currently in the system.
*
* @returns An array of `HalSignalInfo` objects. See {@link HalSignalInfo} for structure.
*/
const getInfoSignals = () => {
const nativeSignals = constants_1.halNative.get_info_signals();
return nativeSignals.map((signal) => ({
...signal,
type: constants_1.HalTypeFromValue[signal.type] ?? "bit",
}));
};
exports.getInfoSignals = getInfoSignals;
/**
* Retrieves a list of all HAL parameters currently in the system.
*
* @returns An array of `HalParamInfo` objects. See {@link HalParamInfo} for structure.
*/
const getInfoParams = () => {
const nativeParams = constants_1.halNative.get_info_params();
return nativeParams.map((param) => ({
...param,
type: constants_1.HalTypeFromValue[param.type] ?? "bit",
direction: constants_1.HalParamDirFromValue[param.direction] ?? "ro",
}));
};
exports.getInfoParams = getInfoParams;
/**
* Creates a new HAL signal.
*
* @param signalName - The desired name for the new signal.
* @param type - The data type for the new signal. See {@link HalType} for available types.
* @returns `true` on success, `false` on failure (error is thrown).
* @throws Error if signal name already exists or creation fails.
*/
const newSignal = (signalName, type) => {
return constants_1.halNative.new_sig(signalName, constants_1.HalTypeValue[type]);
};
exports.newSignal = newSignal;
/**
* Checks if the signal connected to an IN pin has at least one writer (another pin driving it).
*
* @param pinName - The full name of the IN pin.
* @returns `true` if the pin is connected to a signal and that signal has one or more writers,
* `false` otherwise.
* @throws Error if the pin does not exist.
*/
const pinHasWriter = (pinName) => {
return constants_1.halNative.pin_has_writer(pinName);
};
exports.pinHasWriter = pinHasWriter;
/**
* Sets the value of a HAL pin or parameter identified by its full name.
*
* The `value` is converted from its JavaScript type to a string and then parsed by
* the C++ layer, similar to `halcmd setp`. This can set unconnected IN pins
* (modifying their internal `dummysig`) or RW parameters.
*
* @param name - The full name of the pin or parameter.
* @param value - The value to set (string, number, or boolean).
* @returns `true` on success, `false` on failure (error is thrown).
* @throws Error if item not found, if trying to set OUT pin or connected IN pin,
* or if value conversion fails.
*
* @remarks Cannot set OUT pins or connected IN pins with this function.
* Use direct signal manipulation or component proxy access for connected items.
*/
const setPinParamValue = (name, value) => {
return constants_1.halNative.set_p(name, String(value));
};
exports.setPinParamValue = setPinParamValue;
/**
* Sets the value of an unconnected HAL signal identified by its name.
*
* The `value` is converted and parsed similarly to `setPinParamValue`.
*
* @param name - The full name of the signal.
* @param value - The value to set (string, number, or boolean).
* @returns `true` on success, `false` on failure (error is thrown).
* @throws Error if signal not found, if signal has writers, or if value conversion fails.
*/
const setSignalValue = (name, value) => {
return constants_1.halNative.set_s(name, String(value));
};
exports.setSignalValue = setSignalValue;