Wed, 20 Jan 2021 11:37:03 +0100
reimplemented lasercutter changes
46 | 1 | #cython: language_level=3 |
15 | 2 | # |
46 | 3 | # This file is part of the Printrun suite. |
4 | # | |
5 | # Printrun is free software: you can redistribute it and/or modify | |
15 | 6 | # it under the terms of the GNU General Public License as published by |
7 | # the Free Software Foundation, either version 3 of the License, or | |
8 | # (at your option) any later version. | |
9 | # | |
46 | 10 | # Printrun is distributed in the hope that it will be useful, |
15 | 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | # GNU General Public License for more details. | |
14 | # | |
15 | # You should have received a copy of the GNU General Public License | |
16 | # along with Printrun. If not, see <http://www.gnu.org/licenses/>. | |
17 | ||
18 | from libc.stdlib cimport malloc, free | |
19 | from libc.stdint cimport uint8_t, uint32_t | |
20 | from libc.string cimport strlen, strncpy | |
21 | ||
22 | cdef char* copy_string(object value): | |
46 | 23 | value = value.encode('utf-8') |
15 | 24 | cdef char* orig = value |
25 | str_len = len(orig) | |
26 | cdef char* array = <char *>malloc(str_len + 1) | |
27 | strncpy(array, orig, str_len) | |
28 | array[str_len] = 0; | |
29 | return array | |
30 | ||
31 | cdef enum BitPos: | |
32 | pos_raw = 1 << 0 | |
33 | pos_command = 1 << 1 | |
34 | pos_is_move = 1 << 2 | |
35 | pos_x = 1 << 3 | |
36 | pos_y = 1 << 4 | |
37 | pos_z = 1 << 5 | |
38 | pos_e = 1 << 6 | |
39 | pos_f = 1 << 7 | |
40 | pos_i = 1 << 8 | |
41 | pos_j = 1 << 9 | |
42 | pos_relative = 1 << 10 | |
43 | pos_relative_e = 1 << 11 | |
44 | pos_extruding = 1 << 12 | |
45 | pos_current_x = 1 << 13 | |
46 | pos_current_y = 1 << 14 | |
47 | pos_current_z = 1 << 15 | |
48 | pos_current_tool = 1 << 16 | |
49 | pos_gcview_end_vertex = 1 << 17 | |
50 | # WARNING: don't use bits 24 to 31 as we store current_tool there | |
51 | ||
52 | cdef inline uint32_t has_var(uint32_t status, uint32_t pos): | |
53 | return status & pos | |
54 | ||
55 | cdef inline uint32_t set_has_var(uint32_t status, uint32_t pos): | |
56 | return status | pos | |
57 | ||
58 | cdef inline uint32_t unset_has_var(uint32_t status, uint32_t pos): | |
59 | return status & ~pos | |
60 | ||
61 | cdef class GLine: | |
62 | ||
63 | cdef char* _raw | |
64 | cdef char* _command | |
65 | cdef float _x, _y, _z, _e, _f, _i, _j | |
66 | cdef float _current_x, _current_y, _current_z | |
67 | cdef uint32_t _gcview_end_vertex | |
68 | cdef uint32_t _status | |
69 | ||
70 | __slots__ = () | |
71 | ||
72 | def __cinit__(self): | |
73 | self._status = 0 | |
74 | self._raw = NULL | |
75 | self._command = NULL | |
76 | ||
77 | def __init__(self, line): | |
78 | self.raw = line | |
79 | ||
80 | def __dealloc__(self): | |
81 | if self._raw != NULL: free(self._raw) | |
82 | if self._command != NULL: free(self._command) | |
83 | ||
84 | property x: | |
85 | def __get__(self): | |
86 | if has_var(self._status, pos_x): return self._x | |
87 | else: return None | |
88 | def __set__(self, value): | |
89 | self._x = value | |
90 | self._status = set_has_var(self._status, pos_x) | |
91 | property y: | |
92 | def __get__(self): | |
93 | if has_var(self._status, pos_y): return self._y | |
94 | else: return None | |
95 | def __set__(self, value): | |
96 | self._y = value | |
97 | self._status = set_has_var(self._status, pos_y) | |
98 | property z: | |
99 | def __get__(self): | |
100 | if has_var(self._status, pos_z): return self._z | |
101 | else: return None | |
102 | def __set__(self, value): | |
103 | self._z = value | |
104 | self._status = set_has_var(self._status, pos_z) | |
105 | property e: | |
106 | def __get__(self): | |
107 | if has_var(self._status, pos_e): return self._e | |
108 | else: return None | |
109 | def __set__(self, value): | |
110 | self._e = value | |
111 | self._status = set_has_var(self._status, pos_e) | |
112 | property f: | |
113 | def __get__(self): | |
114 | if has_var(self._status, pos_f): return self._f | |
115 | else: return None | |
116 | def __set__(self, value): | |
117 | self._f = value | |
118 | self._status = set_has_var(self._status, pos_f) | |
119 | property i: | |
120 | def __get__(self): | |
121 | if has_var(self._status, pos_i): return self._i | |
122 | else: return None | |
123 | def __set__(self, value): | |
124 | self._i = value | |
125 | self._status = set_has_var(self._status, pos_i) | |
126 | property j: | |
127 | def __get__(self): | |
128 | if has_var(self._status, pos_j): return self._j | |
129 | else: return None | |
130 | def __set__(self, value): | |
131 | self._j = value | |
132 | self._status = set_has_var(self._status, pos_j) | |
133 | property is_move: | |
134 | def __get__(self): | |
135 | if has_var(self._status, pos_is_move): return True | |
136 | else: return False | |
137 | def __set__(self, value): | |
138 | if value: self._status = set_has_var(self._status, pos_is_move) | |
139 | else: self._status = unset_has_var(self._status, pos_is_move) | |
140 | property relative: | |
141 | def __get__(self): | |
142 | if has_var(self._status, pos_relative): return True | |
143 | else: return False | |
144 | def __set__(self, value): | |
145 | if value: self._status = set_has_var(self._status, pos_relative) | |
146 | else: self._status = unset_has_var(self._status, pos_relative) | |
147 | property relative_e: | |
148 | def __get__(self): | |
149 | if has_var(self._status, pos_relative_e): return True | |
150 | else: return False | |
151 | def __set__(self, value): | |
152 | if value: self._status = set_has_var(self._status, pos_relative_e) | |
153 | else: self._status = unset_has_var(self._status, pos_relative_e) | |
154 | property extruding: | |
155 | def __get__(self): | |
156 | if has_var(self._status, pos_extruding): return True | |
157 | else: return False | |
158 | def __set__(self, value): | |
159 | if value: self._status = set_has_var(self._status, pos_extruding) | |
160 | else: self._status = unset_has_var(self._status, pos_extruding) | |
161 | property current_x: | |
162 | def __get__(self): | |
163 | if has_var(self._status, pos_current_x): return self._current_x | |
164 | else: return None | |
165 | def __set__(self, value): | |
166 | self._current_x = value | |
167 | self._status = set_has_var(self._status, pos_current_x) | |
168 | property current_y: | |
169 | def __get__(self): | |
170 | if has_var(self._status, pos_current_y): return self._current_y | |
171 | else: return None | |
172 | def __set__(self, value): | |
173 | self._current_y = value | |
174 | self._status = set_has_var(self._status, pos_current_y) | |
175 | property current_z: | |
176 | def __get__(self): | |
177 | if has_var(self._status, pos_current_z): return self._current_z | |
178 | else: return None | |
179 | def __set__(self, value): | |
180 | self._current_z = value | |
181 | self._status = set_has_var(self._status, pos_current_z) | |
182 | property current_tool: | |
183 | def __get__(self): | |
184 | if has_var(self._status, pos_current_tool): return self._status >> 24 | |
185 | else: return None | |
186 | def __set__(self, value): | |
187 | self._status = (self._status & ((1 << 24) - 1)) | (value << 24) | |
188 | self._status = set_has_var(self._status, pos_current_tool) | |
189 | property gcview_end_vertex: | |
190 | def __get__(self): | |
191 | if has_var(self._status, pos_gcview_end_vertex): return self._gcview_end_vertex | |
192 | else: return None | |
193 | def __set__(self, value): | |
194 | self._gcview_end_vertex = value | |
195 | self._status = set_has_var(self._status, pos_gcview_end_vertex) | |
196 | property raw: | |
197 | def __get__(self): | |
46 | 198 | if has_var(self._status, pos_raw): return self._raw.decode('utf-8') |
15 | 199 | else: return None |
200 | def __set__(self, value): | |
201 | # WARNING: memory leak could happen here, as we don't do the following : | |
202 | # if self._raw != NULL: free(self._raw) | |
203 | self._raw = copy_string(value) | |
204 | self._status = set_has_var(self._status, pos_raw) | |
205 | property command: | |
206 | def __get__(self): | |
46 | 207 | if has_var(self._status, pos_command): return self._command.decode('utf-8') |
15 | 208 | else: return None |
209 | def __set__(self, value): | |
210 | # WARNING: memory leak could happen here, as we don't do the following : | |
211 | # if self._command != NULL: free(self._command) | |
212 | self._command = copy_string(value) | |
213 | self._status = set_has_var(self._status, pos_command) | |
214 | ||
215 | cdef class GLightLine: | |
216 | ||
217 | cdef char* _raw | |
218 | cdef char* _command | |
219 | cdef uint8_t _status | |
220 | ||
221 | __slots__ = () | |
222 | ||
223 | def __cinit__(self): | |
224 | self._status = 0 | |
225 | self._raw = NULL | |
226 | self._command = NULL | |
227 | ||
228 | def __init__(self, line): | |
229 | self.raw = line | |
230 | ||
231 | def __dealloc__(self): | |
232 | if self._raw != NULL: free(self._raw) | |
233 | if self._command != NULL: free(self._command) | |
234 | ||
235 | property raw: | |
236 | def __get__(self): | |
46 | 237 | if has_var(self._status, pos_raw): return self._raw.decode('utf-8') |
15 | 238 | else: return None |
239 | def __set__(self, value): | |
240 | # WARNING: memory leak could happen here, as we don't do the following : | |
241 | # if self._raw != NULL: free(self._raw) | |
242 | self._raw = copy_string(value) | |
243 | self._status = set_has_var(self._status, pos_raw) | |
244 | property command: | |
245 | def __get__(self): | |
46 | 246 | if has_var(self._status, pos_command): return self._command.decode('utf-8') |
15 | 247 | else: return None |
248 | def __set__(self, value): | |
249 | # WARNING: memory leak could happen here, as we don't do the following : | |
250 | # if self._command != NULL: free(self._command) | |
251 | self._command = copy_string(value) | |
252 | self._status = set_has_var(self._status, pos_command) | |
253 | property is_move: | |
254 | def __get__(self): | |
255 | if has_var(self._status, pos_is_move): return True | |
256 | else: return False | |
257 | def __set__(self, value): | |
258 | if value: self._status = set_has_var(self._status, pos_is_move) | |
259 | else: self._status = unset_has_var(self._status, pos_is_move) |