diff --git a/configs/sim/axis/laser/python/remap.py b/configs/sim/axis/laser/python/remap.py index e11ffd0e577..a6d47c3995e 100644 --- a/configs/sim/axis/laser/python/remap.py +++ b/configs/sim/axis/laser/python/remap.py @@ -1,4 +1,3 @@ -from pyhal import * from raster import * from interpreter import INTERP_EXECUTE_FINISH, INTERP_OK @@ -8,8 +7,8 @@ def init(self): global rasterProgrammer rasterProgrammer = RasterProgrammer("raster-programmer") print("raster initialized") - - + + def rasterBegin(self, **words): global rasterProgrammer #machine must be in position before begin issued @@ -44,5 +43,4 @@ def rasterStop(self, **words): yield INTERP_EXECUTE_FINISH if rasterProgrammer: rasterProgrammer.stop() - diff --git a/lib/python/pyhal.py b/lib/python/pyhal.py deleted file mode 100644 index 4e3fb2bd41f..00000000000 --- a/lib/python/pyhal.py +++ /dev/null @@ -1,262 +0,0 @@ -"""hal library python interface using ctypes ffi interface""" - -from ctypes import* - - -lib = CDLL('liblinuxcnchal.so') -lib.hal_malloc.restype = c_void_p -lib.hal_malloc.argtype = [c_long] -lib.hal_comp_name.restype = c_char_p - -lib.hal_pin_new.argtypes = [c_char_p, c_int, c_int, c_void_p, c_int] - -lib.hal_signal_new.argtypes = [c_char_p, c_int] - -lib.hal_link.argtypes = [c_char_p, c_char_p] - -lib.hal_port_read.argtypes = [POINTER(c_int), c_char_p, c_uint] -lib.hal_port_read.restype = c_bool - -lib.hal_port_peek.argtypes = [POINTER(c_int), c_char_p, c_uint] -lib.hal_port_peek.restype = c_bool - -lib.hal_port_peek_commit.argtypes = [POINTER(c_int), c_char_p, c_uint] -lib.hal_port_peek.restype = c_bool - -lib.hal_port_write.argtypes = [POINTER(c_int), c_char_p, c_uint] -lib.hal_port_write.restype = c_bool - -lib.hal_port_readable.argtypes = [POINTER(c_int)] -lib.hal_port_readable.restype = c_uint - -lib.hal_port_writable.argtypes = [POINTER(c_int)] -lib.hal_port_writable.restype = c_uint - -lib.hal_port_buffer_size.argtypes = [POINTER(c_int)] -lib.hal_port_buffer_size.restype = c_uint - -lib.hal_port_clear.argtypes = [POINTER(c_int)] - -lib.hal_port_wait_readable.argtypes = [POINTER(POINTER(c_int)), c_uint, c_int] -lib.hal_port_wait_writable.argtypes = [POINTER(POINTER(c_int)), c_uint, c_int] - -import functools -import inspect -import warnings - -def pyhaldeprecate(reason): - def decorator(func): - @functools.wraps(func) - def warnfunc(*args, **kwargs): - warnings.warn("Use of deprecated class/function {} ({}).".format(func.__name__, reason), - FutureWarning, stacklevel=2) - return func(*args, **kwargs) - return warnfunc - return decorator - - -class HalException(Exception): - """An exception raised by hal library functions""" - pass - - -class halType: - BIT = 1 - FLOAT = 2 - SIGNED = 3 - UNSIGNED = 4 - PORT = 5 - - values = [BIT, FLOAT, SIGNED, UNSIGNED, PORT ] - - typeConversion = { BIT : c_bool, - FLOAT : c_double, - SIGNED : c_int32, - UNSIGNED : c_uint32, - PORT : c_int } - -class pinDir: - IN = 16 - OUT = 32 - IO = IN | OUT - - values = [IN, OUT, IO] - - -class pin(object): - def __init__(self, component, name, type, dir, data_ptr): - self.comp = component - self.name = name - self.type = type - self.dir = dir - self.data_ptr = data_ptr - - @property - def fullname(self): - return "{0}.{1}".format(self.comp.name, self.name) - - @property - def value(self): - if self.type == halType.PORT: - raise HalException("cannot get value of PORT pin") - else: - return self.data_ptr.contents.contents.value - - @value.setter - def value(self, val): - if self.type == halType.PORT: - raise HalException("cannot set value of PORT pin") - else: - self.data_ptr.contents.contents.value = val - - -@pyhaldeprecate("'pyhal' causes severe problems, has been replaced by 'hal' and will soon be removed") -class port(pin): - def __init__(self, component, name, type, dir, data_ptr): - pin.__init__(self, component, name, type, dir, data_ptr) - - @property - def __port(self): - return self.data_ptr.contents - - def read(self, count): - if self.dir != pinDir.IN: - raise HalException("cannot read output port") - - buff = create_string_buffer(count) - if not lib.hal_port_read(self.__port, buff, count): - return '' - else: - return buff.raw - - def peek(self, count): - if self.dir != pinDir.IN: - raise HalException("cannot peek output port") - - buff = create_string_buffer(count) - if not lib.hal_port_peek(self.__port, buff, count): - return '' - else: - return buff.raw - - def peek_commit(self, count): - if self.dir != pinDir.IN: - raise HalException("cannot peek commit output port") - return lib.hal_port_peek_commit(self.__port, count) - - def write(self, buff): - if self.dir != pinDir.OUT: - raise HalException("cannot write input port") - - return lib.hal_port_write(self.__port, buff.encode(), len(buff)) - - def readable(self): - if self.dir != pinDir.IN: - raise HalException("cannot read output port") - - return lib.hal_port_readable(self.__port) - - def writable(self): - if self.dir != pinDir.OUT: - raise HalException("cannot write input port") - - return lib.hal_port_writable(self.__port) - - def size(self): - return lib.hal_port_buffer_size(self.__port) - - def clear(self): - if self.dir != pinDir.IN: - raise HalException("cannot clear output port") - - lib.hal_port_clear(self.__port) - - def waitReadable(self, count): - if self.dir != pinDir.IN: - raise HalException("cannot read output port") - - lib.hal_port_wait_readable(self.data_ptr, count, 0) - - def waitWritable(self, count): - if self.dir != pinDir.OUT: - raise HalException("cannot write input port") - - lib.hal_port_wait_writable(self.data_ptr, count, 0) - - - -@pyhaldeprecate("'pyhal' causes severe problems, has been replaced by 'hal' and will soon be removed") -class component: - def __init__(self, name): - self.id = lib.hal_init(name.encode()) - self.name = name - self.pins = {} - if self.id < 0: - raise HalException('failed to initialized component "{0}"'.format(name)) - - - def __del__(self): - self.exit() - - - def exit(self): - if self.id > 0: - result = lib.hal_exit(self.id) - self.id = -1 - if result < 0: - raise HalException('hal_exit failed with code {0}'.format(result)) - - - def ready(self): - result = lib.hal_ready(self.id) - if result < 0: - raise HalException('hal_ready failed with code {0}'.format(result)) - - def name(self): - return lib.hal_comp_name(self.id) - - - def pinNew(self, name, type, dir): - if not type in halType.typeConversion: - raise HalException('failed to create pin "{0}". Unknown type {1}'.format(name, type)) - - if not dir in pinDir.values: - raise HalException('failed to create pin "{0}". Unknown dir {1}'.format(name, dir)) - - ctype = halType.typeConversion[type] - - ptr = self.halMalloc(sizeof(c_void_p)) - result = lib.hal_pin_new("{0}.{1}".format(self.name, name).encode(), type, dir, ptr, self.id) - if result < 0: - raise HalException('failed to create pin "{0}" with code {1}'.format(name, result)) - - if type == halType.PORT: - self.pins[name] = port(self, name, type, dir, cast(ptr, POINTER(POINTER(ctype)))) - else: - self.pins[name] = pin(self, name, type, dir, cast(ptr, POINTER(POINTER(ctype)))) - - return self.pins[name] - - def sigNew(self, name, type): - if not type in halType.values: - raise HalException('failed to create signal "{0}". Invalid type {1}'.format(name, type)) - - result = lib.hal_signal_new(name.encode(), type) - - if result < 0: - raise HalException('failed to create signal "{0}" with code {1}'.format(name, result)) - - def sigLink(self, pin_name, sig_name): - result = lib.hal_link(pin_name.encode(), sig_name.encode()) - if result < 0: - raise HalException('failed to link sig "{0}" to pin "{1}" with result {2}'.format(sig_name, pin_name, result)) - - - def halMalloc(self, count): - ptr = lib.hal_malloc(count) - memset(ptr, 0, count) - return ptr - - - - diff --git a/tests/pyhal/expected b/tests/pyhal/expected deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/tests/pyhal/test b/tests/pyhal/test deleted file mode 100755 index 2d8cb930be0..00000000000 --- a/tests/pyhal/test +++ /dev/null @@ -1,125 +0,0 @@ -#!/usr/bin/env python3 -from pyhal import * -import os - -os.system("realtime start") - -c = component("hello") - -port_in = c.pinNew("port-in", halType.PORT, pinDir.IN) -port_out = c.pinNew("port-out", halType.PORT, pinDir.OUT) - -in_s = c.pinNew("in-s", halType.SIGNED, pinDir.IN) -out_s = c.pinNew("out-s", halType.SIGNED, pinDir.OUT) - -in_u = c.pinNew("in-u", halType.UNSIGNED, pinDir.IN) -out_u = c.pinNew("out-u", halType.UNSIGNED, pinDir.OUT) - -in_f = c.pinNew("in-f", halType.FLOAT, pinDir.IN) -out_f = c.pinNew("out-f", halType.FLOAT, pinDir.OUT) - - -in_io = c.pinNew("in-io", halType.BIT, pinDir.IO) -out_io = c.pinNew("out-io", halType.BIT, pinDir.IO) - - -c.ready() - - - - - -#test signed in/out pins -c.sigNew("testSig-S", halType.SIGNED) -c.sigLink("hello.in-s", "testSig-S") -c.sigLink("hello.out-s", "testSig-S") -out_s.value = -100 -assert in_s.value == -100, "in_s.value should be -100. got {0}".format(in_s.value) -out_s.value = 34435 -assert in_s.value == 34435, "in_s.value should be 34435. got {0}".format(in_s.value) - - -#test unsigned in/out pins -c.sigNew("testSig-U", halType.UNSIGNED) -c.sigLink("hello.in-u", "testSig-U") -c.sigLink("hello.out-u", "testSig-U") -out_u.value = 65535 -assert in_u.value == out_u.value, "in_u.value should be {0}. got {0}".format(out_u.value, in_u.value) - - -#test float pins -c.sigNew("testSig-F", halType.FLOAT) -c.sigLink("hello.in-f", "testSig-F") -os.system("halcmd sets testSig-F -1000.0") - -assert in_f.value == -1000.0 -c.sigLink("hello.out-f", "testSig-F") - -out_f.value = 333.333 -assert in_f.value == 333.333 - -#test io bool -c.sigNew("testSig-IO", halType.BIT) -c.sigLink("hello.in-io", "testSig-IO") -c.sigLink("hello.out-io", "testSig-IO") - -out_io.value = False -assert in_io.value == False -out_io.value = True -assert in_io.value == True - -in_io.value = False -assert out_io.value == False - - - -#now test hal port -c.sigNew("testSig-PORT", halType.PORT) -c.sigLink("hello.port-in", "testSig-PORT") -c.sigLink("hello.port-out", "testSig-PORT") - -assert port_in.size() == 0 -assert port_out.size() == 0 - -#make sure to allocate a port on the signal -os.system('halcmd sets testSig-PORT 9') - -assert port_in.size() == 9 -assert port_out.size() == 9 - - -assert port_out.writable() == 8 -assert port_in.readable() == 0 - - -port_out.waitWritable(8) -assert port_out.write("hello") -assert port_out.writable() == 3 - -assert port_in.readable() == 5 -assert port_in.read(2) == b"he" -assert port_in.peek(10) == "" -assert port_in.peek(3) == b"llo" -assert port_in.readable() == 3 -assert port_in.read(3) == b"llo" - -assert port_in.readable() == 0 -assert port_out.write("hello") -assert port_in.read(5) == b"hello" - -assert port_in.readable() == 0 -assert port_in.read(5) == '' - - -assert port_out.writable() == 8 - -assert port_out.write("12345678") - -assert port_in.readable() == 8 -assert port_out.writable() == 0 - -assert port_in.read(8) == b"12345678" - -c.exit() -os.system("halrun -U") -exit(0)