gcoder_line.pyx 7.87 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# This file is copied from GCoder.
#
# GCoder is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GCoder is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Printrun.  If not, see <http://www.gnu.org/licenses/>.

from libc.stdlib cimport malloc, free
17
from libc.stdint cimport uint32_t
18
from libc.string cimport strlen, strncpy
19

20 21 22 23 24 25 26 27
cdef char* copy_string(object value):
    cdef char* orig = value
    str_len = len(orig)
    cdef char* array = <char *>malloc(str_len + 1)
    strncpy(array, orig, str_len)
    array[str_len] = 0;
    return array

28 29 30 31 32 33 34 35
cdef enum BitPos:
    pos_x =                 1 << 0
    pos_y =                 1 << 1
    pos_z =                 1 << 2
    pos_e =                 1 << 3
    pos_f =                 1 << 4
    pos_i =                 1 << 5
    pos_j =                 1 << 6
36 37 38 39 40 41 42 43 44 45 46 47
    pos_is_move =           1 << 7
    pos_relative =          1 << 8
    pos_relative_e =        1 << 9
    pos_extruding =         1 << 10
    pos_current_x =         1 << 11
    pos_current_y =         1 << 12
    pos_current_z =         1 << 13
    pos_current_tool =      1 << 14
    pos_raw =               1 << 15
    pos_command =           1 << 16
    pos_gcview_end_vertex = 1 << 17
    # WARNING: don't use bits 24 to 31 as we store current_tool there
48 49 50 51 52 53 54

cdef inline uint32_t has_var(uint32_t status, uint32_t pos):
    return status & pos

cdef inline uint32_t set_has_var(uint32_t status, uint32_t pos):
    return status | pos

55 56 57
cdef inline uint32_t unset_has_var(uint32_t status, uint32_t pos):
    return status & ~pos

58
cdef class GLine:
59 60 61
    
    cdef char* _raw
    cdef char* _command
62
    cdef float _x, _y, _z, _e, _f, _i, _j
63 64
    cdef float _current_x, _current_y, _current_z
    cdef uint32_t _gcview_end_vertex
65
    cdef uint32_t _status
66 67 68 69

    __slots__ = ()

    def __cinit__(self):
70
        self._status = 0
71 72 73
        self._raw = NULL
        self._command = NULL

74 75 76
    def __init__(self, line):
        self.raw = line

77
    def __dealloc__(self):
78 79
        if self._raw != NULL: free(self._raw)
        if self._command != NULL: free(self._command)
80

81 82
    property x:
        def __get__(self):
83
            if has_var(self._status, pos_x): return self._x
84 85 86
            else: return None
        def __set__(self, value):
            self._x = value
87
            self._status = set_has_var(self._status, pos_x)
88 89
    property y:
        def __get__(self):
90
            if has_var(self._status, pos_y): return self._y
91 92 93
            else: return None
        def __set__(self, value):
            self._y = value
94
            self._status = set_has_var(self._status, pos_y)
95 96
    property z:
        def __get__(self):
97
            if has_var(self._status, pos_z): return self._z
98 99 100
            else: return None
        def __set__(self, value):
            self._z = value
101
            self._status = set_has_var(self._status, pos_z)
102 103
    property e:
        def __get__(self):
104
            if has_var(self._status, pos_e): return self._e
105 106 107
            else: return None
        def __set__(self, value):
            self._e = value
108
            self._status = set_has_var(self._status, pos_e)
109 110
    property f:
        def __get__(self):
111
            if has_var(self._status, pos_f): return self._f
112 113 114
            else: return None
        def __set__(self, value):
            self._f = value
115
            self._status = set_has_var(self._status, pos_f)
116 117
    property i:
        def __get__(self):
118
            if has_var(self._status, pos_i): return self._i
119 120 121
            else: return None
        def __set__(self, value):
            self._i = value
122
            self._status = set_has_var(self._status, pos_i)
123 124
    property j:
        def __get__(self):
125
            if has_var(self._status, pos_j): return self._j
126 127 128
            else: return None
        def __set__(self, value):
            self._j = value
129
            self._status = set_has_var(self._status, pos_j)
130 131
    property is_move:
        def __get__(self):
132 133
            if has_var(self._status, pos_is_move): return True
            else: return False
134
        def __set__(self, value):
135 136
            if value: self._status = set_has_var(self._status, pos_is_move)
            else: self._status = unset_has_var(self._status, pos_is_move)
137 138
    property relative:
        def __get__(self):
139 140
            if has_var(self._status, pos_relative): return True
            else: return False
141
        def __set__(self, value):
142 143
            if value: self._status = set_has_var(self._status, pos_relative)
            else: self._status = unset_has_var(self._status, pos_relative)
144 145
    property relative_e:
        def __get__(self):
146 147
            if has_var(self._status, pos_relative_e): return True
            else: return False
148
        def __set__(self, value):
149 150
            if value: self._status = set_has_var(self._status, pos_relative_e)
            else: self._status = unset_has_var(self._status, pos_relative_e)
151 152
    property extruding:
        def __get__(self):
153 154
            if has_var(self._status, pos_extruding): return True
            else: return False
155
        def __set__(self, value):
156 157
            if value: self._status = set_has_var(self._status, pos_extruding)
            else: self._status = unset_has_var(self._status, pos_extruding)
158 159
    property current_x:
        def __get__(self):
160
            if has_var(self._status, pos_current_x): return self._current_x
161 162 163
            else: return None
        def __set__(self, value):
            self._current_x = value
164
            self._status = set_has_var(self._status, pos_current_x)
165 166
    property current_y:
        def __get__(self):
167
            if has_var(self._status, pos_current_y): return self._current_y
168 169 170
            else: return None
        def __set__(self, value):
            self._current_y = value
171
            self._status = set_has_var(self._status, pos_current_y)
172 173
    property current_z:
        def __get__(self):
174
            if has_var(self._status, pos_current_z): return self._current_z
175 176 177
            else: return None
        def __set__(self, value):
            self._current_z = value
178
            self._status = set_has_var(self._status, pos_current_z)
179 180
    property current_tool:
        def __get__(self):
181
            if has_var(self._status, pos_current_tool): return self._status >> 24
182 183
            else: return None
        def __set__(self, value):
184
            self._status = (self._status & ((1 << 24) - 1)) | (value << 24)
185
            self._status = set_has_var(self._status, pos_current_tool)
186 187
    property gcview_end_vertex:
        def __get__(self):
188
            if has_var(self._status, pos_gcview_end_vertex): return self._gcview_end_vertex
189 190 191
            else: return None
        def __set__(self, value):
            self._gcview_end_vertex = value
192
            self._status = set_has_var(self._status, pos_gcview_end_vertex)
193 194
    property raw:
        def __get__(self):
195
            if has_var(self._status, pos_raw): return self._raw
196 197
            else: return None
        def __set__(self, value):
198 199
            # WARNING: memory leak could happen here, as we don't do the following :
            # if self._raw != NULL: free(self._raw)
200
            self._raw = copy_string(value)
201
            self._status = set_has_var(self._status, pos_raw)
202 203
    property command:
        def __get__(self):
204
            if has_var(self._status, pos_command): return self._command
205 206
            else: return None
        def __set__(self, value):
207 208
            # WARNING: memory leak could happen here, as we don't do the following :
            # if self._command != NULL: free(self._command)
209
            self._command = copy_string(value)
210
            self._status = set_has_var(self._status, pos_command)