Skip to content

Commit 6925b79

Browse files
committed
hal: C++ API (hal.hh) and pybind11 bindings on the new HAL API
Reintroduce a C++ interface for HAL, built strictly on the public C API and the user-land query API: no hal_priv.h, no direct shared memory access, no re-implemented library internals. - hal.hh: type-safe, header-only C++ layer in linuxcnc::hal. Typed pin/param handles via traits<T> over the rtapi_ types (compile-time accessor selection), runtime-typed pin_t variant and anypin for name-based access, component class with add_pin for the struct-member idiom, and ULAPI by-name query/set functions when the query API is present. Handles re-read the shmem slot on every access so hal_link() slot rewrites stay visible (C pointer-variable semantics). Handles are move-only; HAL objects are unique. Query callback paths are exception-free; errors are reported after the library releases the HAL mutex. - halpybind.cc: pybind11 module (halpp.so) exposing component, Pin, enums and the by-name functions. Built alongside _hal/hal.py, replacing nothing. Library init/exit at import; string set values delegate to setps_common_cb for halcmd-consistent parsing. - taskclass converted to the new API (it was the only hal.hh user). - HAL_PORT intentionally exempted until the API break: the port handle type and creation semantics are still in flux. - tests/halpp: Python and native C++ smoke suites. Based on the pybind11 branch by rene-dev, re-based on the new HAL API.
1 parent a1edf0d commit 6925b79

7 files changed

Lines changed: 926 additions & 142 deletions

File tree

debian/control.top.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Build-Depends:
4040
psmisc,
4141
python3,
4242
python3-dev,
43+
python3-pybind11,
4344
python3-tk,
4445
python3-xlib,
4546
tcl,

src/emc/task/taskclass.cc

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,26 @@ using namespace linuxcnc;
4040
********************************************************************/
4141
int Task::iocontrol_hal_init(void)
4242
{
43-
iocontrol.add_pin("user-enable-out", hal_dir::OUT, iocontrol_data.user_enable_out);
44-
iocontrol.add_pin("emc-enable-in", hal_dir::IN, iocontrol_data.emc_enable_in);
45-
iocontrol.add_pin("user-request-enable", hal_dir::OUT, iocontrol_data.user_request_enable);
46-
iocontrol.add_pin("coolant-mist", hal_dir::OUT, iocontrol_data.coolant_mist);
47-
iocontrol.add_pin("coolant-flood", hal_dir::OUT, iocontrol_data.coolant_flood);
48-
iocontrol.add_pin("tool-prep-pocket", hal_dir::OUT, iocontrol_data.tool_prep_pocket);
49-
iocontrol.add_pin("tool-from-pocket", hal_dir::OUT, iocontrol_data.tool_from_pocket);
50-
iocontrol.add_pin("tool-prep-index", hal_dir::OUT, iocontrol_data.tool_prep_index);
51-
iocontrol.add_pin("tool-prep-number", hal_dir::OUT, iocontrol_data.tool_prep_number);
52-
iocontrol.add_pin("tool-number", hal_dir::OUT, iocontrol_data.tool_number);
53-
iocontrol.add_pin("tool-prepare", hal_dir::OUT, iocontrol_data.tool_prepare);
54-
iocontrol.add_pin("tool-prepared", hal_dir::IN, iocontrol_data.tool_prepared);
55-
iocontrol.add_pin("tool-change", hal_dir::OUT, iocontrol_data.tool_change);
56-
iocontrol.add_pin("tool-changed", hal_dir::IN, iocontrol_data.tool_changed);
57-
iocontrol.ready();
58-
if (iocontrol.error < 0)
43+
try {
44+
iocontrol.add_pin("user-enable-out", hal_dir::OUT, iocontrol_data.user_enable_out);
45+
iocontrol.add_pin("emc-enable-in", hal_dir::IN, iocontrol_data.emc_enable_in);
46+
iocontrol.add_pin("user-request-enable", hal_dir::OUT, iocontrol_data.user_request_enable);
47+
iocontrol.add_pin("coolant-mist", hal_dir::OUT, iocontrol_data.coolant_mist);
48+
iocontrol.add_pin("coolant-flood", hal_dir::OUT, iocontrol_data.coolant_flood);
49+
iocontrol.add_pin("tool-prep-pocket", hal_dir::OUT, iocontrol_data.tool_prep_pocket);
50+
iocontrol.add_pin("tool-from-pocket", hal_dir::OUT, iocontrol_data.tool_from_pocket);
51+
iocontrol.add_pin("tool-prep-index", hal_dir::OUT, iocontrol_data.tool_prep_index);
52+
iocontrol.add_pin("tool-prep-number", hal_dir::OUT, iocontrol_data.tool_prep_number);
53+
iocontrol.add_pin("tool-number", hal_dir::OUT, iocontrol_data.tool_number);
54+
iocontrol.add_pin("tool-prepare", hal_dir::OUT, iocontrol_data.tool_prepare);
55+
iocontrol.add_pin("tool-prepared", hal_dir::IN, iocontrol_data.tool_prepared);
56+
iocontrol.add_pin("tool-change", hal_dir::OUT, iocontrol_data.tool_change);
57+
iocontrol.add_pin("tool-changed", hal_dir::IN, iocontrol_data.tool_changed);
58+
iocontrol.ready();
59+
} catch(const std::exception &e) {
60+
fprintf(stderr, "iocontrol_hal_init: %s\n", e.what());
5961
return -1;
62+
}
6063
return 0;
6164
}
6265

src/hal/Submakefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,19 @@ $(HALMODULE): $(call TOOBJS, $(HALMODULESRCS)) $(HALLIB)
2828
$(ECHO) Linking python module $(notdir $@)
2929
$(Q)$(CXX) $(LDFLAGS) -shared -o $@ $^
3030

31+
# pybind11 C++ bindings (hal.hh based)
32+
# setps_util.c only exists in the tree with the HAL query API
33+
HALPPSRCS := hal/halpybind.cc $(wildcard hal/utils/setps_util.c)
34+
PYSRCS += $(HALPPSRCS)
35+
36+
# pybind11 headers: python3-pybind11 (system include) or pip user site
37+
$(call TOOBJS, $(HALPPSRCS)): EXTRAFLAGS += $(shell python3 -m pybind11 --includes 2>/dev/null)
38+
39+
HALPP := ../lib/python/halpp.so
40+
$(HALPP): $(call TOOBJS, $(HALPPSRCS)) $(HALLIB)
41+
$(ECHO) Linking python module $(notdir $@)
42+
$(Q)$(CXX) $(LDFLAGS) -shared -o $@ $^
43+
3144
TARGETS += $(HALLIB) ../lib/liblinuxcnchal.so.0
3245
PYTARGETS += $(HALMODULE)
46+
PYTARGETS += $(HALPP)

0 commit comments

Comments
 (0)