File: pyhal.py

package info (click to toggle)
linuxcnc 2.9.0~pre1%2Bgit20230208.f1270d6ed7-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 274,984 kB
  • sloc: python: 142,166; ansic: 103,241; cpp: 99,140; tcl: 16,045; xml: 10,608; sh: 10,124; makefile: 1,229; javascript: 226; sql: 72; asm: 15
file content (245 lines) | stat: -rw-r--r-- 7,044 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
"""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 = [c_int, c_char_p, c_uint]
lib.hal_port_read.restype = c_bool

lib.hal_port_peek.argtypes = [c_int, c_char_p, c_uint]
lib.hal_port_peek.restype = c_bool

lib.hal_port_peek_commit.argtypes = [c_int, c_char_p, c_uint]
lib.hal_port_peek.restype = c_bool

lib.hal_port_write.argtypes = [c_int, c_char_p, c_uint]
lib.hal_port_write.restype = c_bool

lib.hal_port_readable.argtypes = [c_int]
lib.hal_port_readable.restype = c_uint

lib.hal_port_writable.argtypes = [c_int]
lib.hal_port_writable.restype = c_uint

lib.hal_port_buffer_size.argtypes = [c_int]
lib.hal_port_buffer_size.restype = c_uint

lib.hal_port_clear.argtypes = [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]

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


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.contents.value

    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)

    

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