diff options
author | Pavel Shramov <psha@lebu.psha.org.ru> | 2010-11-03 16:12:28 +0300 |
---|---|---|
committer | Pavel Shramov <psha@lebu.psha.org.ru> | 2010-11-03 17:50:30 +0300 |
commit | 81f8f84b1ec1070156bace1728f395a95fcc9cff (patch) | |
tree | c9ee260e8fb93df395cecb46fccae8f8ae49c726 | |
parent | a7c08f633c2075fd38b43c1b48fe205666f6daf4 (diff) | |
download | linuxcnc-81f8f84b1ec1070156bace1728f395a95fcc9cff.tar.gz linuxcnc-81f8f84b1ec1070156bace1728f395a95fcc9cff.zip |
pyhal: Add python module over C-module
Add hal.py and rename hal.so to _hal.so
Move documentation and wrappers into hal.py
-rw-r--r-- | lib/python/hal.py | 68 | ||||
-rw-r--r-- | src/hal/Submakefile | 2 | ||||
-rw-r--r-- | src/hal/halmodule.cc | 6 |
3 files changed, 72 insertions, 4 deletions
diff --git a/lib/python/hal.py b/lib/python/hal.py new file mode 100644 index 000000000..af66fc85f --- /dev/null +++ b/lib/python/hal.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python +# vim: sts=4 sw=4 et + +""" + +This module allows the creation of userspace HAL components in Python. +This includes pins and parameters of the various HAL types. + +Typical usage: + +import hal, time +h = hal.component("component-name") +# create pins and parameters with calls to h.newpin and h.newparam +h.newpin("in", hal.HAL_FLOAT, hal.HAL_IN) +h.newpin("out", hal.HAL_FLOAT, hal.HAL_OUT) +h.ready() # mark the component as 'ready' + +try: + while 1: + # act on changed input pins; update values on output pins + time.sleep(1) + h['out'] = h['in'] +except KeyboardInterrupt: pass + + +When the component is requested to exit with 'halcmd unload', a +KeyboardInterrupt exception will be raised. +""" + +import _hal +from _hal import * + +class _ItemWrap(object): + def __new__(cls, item): + if not isinstance(item, _hal.item): + raise TypeError("Constructor argument must be _hal.item: %s" % type(item)) + self = object.__new__(cls) + for f in ['get', 'set', 'get_type', 'get_name', 'get_dir', 'is_pin', '__repr__']: + setattr(self, f, getattr(item, f)) + return self + + def __init__(self, item): + self._item = item + + name = property(lambda s: s._item.get_name()) + type = property(lambda s: s._item.get_type()) + dir = property(lambda s: s._item.get_dir()) + + value = property(lambda s: s._item.get(), lambda s,v: s._item.set(v)) + +class Pin(_ItemWrap): + def __init__(self, item): + _ItemWrap.__init__(self, item) + if not item.is_pin(): + raise TypeError("Must be constructed from pin object") + +class Param(_ItemWrap): + def __init__(self, item): + _ItemWrap.__init__(self, item) + if item.is_pin(): + raise TypeError("Must be constructed from param object") + +class component(_hal.component): + def newpin(self, *a, **kw): return Pin(_hal.component.newpin(self, *a, **kw)) + def newparam(self, *a, **kw): return Param(_hal.component.newparam(self, *a, **kw)) + + def getpin(self, *a, **kw): return Pin(_hal.component.getpin(self, *a, **kw)) + def getparam(self, *a, **kw): return Param(_hal.component.getparam(self, *a, **kw)) diff --git a/src/hal/Submakefile b/src/hal/Submakefile index 3c16d2094..65c7aeb62 100644 --- a/src/hal/Submakefile +++ b/src/hal/Submakefile @@ -18,7 +18,7 @@ $(HALLIB).0: $(call TOOBJS, $(HALLIBSRCS)) HALMODULESRCS := hal/halmodule.cc PYSRCS += $(HALMODULESRCS) -HALMODULE := ../lib/python/hal.so +HALMODULE := ../lib/python/_hal.so $(HALMODULE): $(call TOOBJS, $(HALMODULESRCS)) $(HALLIB) $(ECHO) Linking python module $(notdir $@) @$(CXX) $(LDFLAGS) -shared -o $@ $^ diff --git a/src/hal/halmodule.cc b/src/hal/halmodule.cc index fe8b8f398..c0c007c6e 100644 --- a/src/hal/halmodule.cc +++ b/src/hal/halmodule.cc @@ -504,7 +504,7 @@ PyTypeObject halobject_type = { pyhal_getattro, /*tp_getattro*/ pyhal_setattro, /*tp_setattro*/ 0, /*tp_as_buffer*/ - Py_TPFLAGS_DEFAULT, /*tp_flags*/ + Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /*tp_flags*/ "HAL Component", /*tp_doc*/ 0, /*tp_traverse*/ 0, /*tp_clear*/ @@ -918,8 +918,8 @@ const char *module_doc = "Interface to emc2's hal\n" ; extern "C" -void inithal(void) { - PyObject *m = Py_InitModule3("hal", module_methods, +void init_hal(void) { + PyObject *m = Py_InitModule3("_hal", module_methods, module_doc); pyhal_error_type = PyErr_NewException((char*)"hal.error", NULL, NULL); |