forked from google/cyanobyte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython.jinja2
241 lines (235 loc) · 6.93 KB
/
python.jinja2
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
{% macro params(input) -%}
{% set args = namespace(index=0) %}
{% for key,variable in input|dictsort %}
{% if key != 'value' -%}
{{- ", " if args.index > 0 -}}{{- key -}}
{%- set args.index = args.index + 1 -%}
{%- endif %}
{%- endfor %}
{%- endmacro %}
{% macro variables(vars) -%}
{% if vars %}
{% for key,var in vars|dictsort %}
{{key | camel_to_snake}} = None # Variable declaration
{% endfor %}
{% endif %}
{%- endmacro %}
{% macro recursiveAssignLogic(logicalStep, keys) -%}
{% for key in keys -%}
{# Perform a recursive summation from an array of logical steps #}
{% if key == 'sum' -%}
({%- for step in logicalStep[key] -%}
{% if step is iterable and step is not string -%}
{{ recursiveAssignLogic(step, step.keys()) -}}
{%- else -%}
{{step | camel_to_snake}}
{%- endif %}
{{- "+" if not loop.last -}}
{%- endfor -%})
{%- endif %}
{# Perform a recursive difference from an array of logical steps #}
{% if key == 'difference' -%}
({%- for step in logicalStep[key] -%}
{% if step is iterable and step is not string -%}
{{ recursiveAssignLogic(step, step.keys()) -}}
{%- else -%}
{{step | camel_to_snake}}
{%- endif %}
{{- "-" if not loop.last -}}
{%- endfor -%})
{%- endif %}
{# Perform a recursive product from an array of logical steps #}
{% if key == 'product' -%}
({%- for step in logicalStep[key] -%}
{% if step is iterable and step is not string -%}
{{ recursiveAssignLogic(step, step.keys()) -}}
{%- else -%}
{{step | camel_to_snake}}
{%- endif %}
{{- "*" if not loop.last -}}
{%- endfor -%})
{%- endif %}
{# Perform a recursive division from an array of logical steps #}
{% if key == 'division' -%}
({%- for step in logicalStep[key] -%}
{% if step is iterable and step is not string -%}
{{ recursiveAssignLogic(step, step.keys()) -}}
{%- else -%}
{{ step | camel_to_snake }}
{%- endif %}
{{- "/" if not loop.last -}}
{%- endfor -%})
{%- endif %}
{# Perform a recursive modulus from an array of logical steps #}
{% if key == 'modulus' -%}
({%- for step in logicalStep[key] -%}
{% if step is iterable and step is not string -%}
{{ recursiveAssignLogic(step, step.keys()) -}}
{%- else -%}
{{ step | camel_to_snake }}
{%- endif %}
{{- "%" if not loop.last -}}
{%- endfor -%})
{%- endif %}
{# Perform a bitwise OR from an array of logical steps #}
{% if key == 'bitwiseOr' -%}
({%- for step in logicalStep[key] -%}
{% if step is iterable and step is not string -%}
{{ recursiveAssignLogic(step, step.keys()) -}}
{%- else -%}
{{step | camel_to_snake}}
{%- endif %}
{{- "|" if not loop.last -}}
{%- endfor -%})
{%- endif %}
{# Perform a bitwise AND from an array of logical steps #}
{% if key == 'bitwiseAnd' -%}
({%- for step in logicalStep[key] -%}
{% if step is iterable and step is not string -%}
{{ recursiveAssignLogic(step, step.keys()) -}}
{%- else -%}
{{step | camel_to_snake}}
{%- endif %}
{{- "&" if not loop.last -}}
{%- endfor -%})
{%- endif %}
{# Perform a power operation #}
{% if key == 'power' -%}
math.pow({{logicalStep[key][0]}}, {{logicalStep[key][1]}})
{%- endif %}
{# Perform an arc tangent operation #}
{% if key == 'arc tangent' -%}
math.atan({{logicalStep[key]}})
{%- endif %}
{# Bitwise ops #}
{%- if key == 'bitShiftLeft' -%}
({{logicalStep[key].var | camel_to_snake}} << {{logicalStep[key].bits}})
{%- endif %}
{%- if key == 'bitShiftRight' -%}
({{logicalStep[key].var | camel_to_snake}} >> {{logicalStep[key].bits}})
{%- endif %}
{%- endfor %}
{%- endmacro %}
{% macro importStdLibs(fields, functions, i2c, template) %}
{% if i2c.address is iterable and i2c.address is not string %}
{# Optionally import class #}
{% if template.enum is sameas false %}
from enum import Enum
{% set template.enum = true %}
{% endif %}
{% endif %}
{% if fields %}
{% for key,field in fields|dictsort %}
{% if field.enum %}
{# Optionally import class #}
{% if template.enum is sameas false %}
from enum import Enum
{% set template.enum = true %}
{% endif %}
{% endif %}
{% endfor %}
{% endif %}
{% if functions %}
{% for key,function in functions|dictsort %}
{# Check if we need to import `math` lib #}
{% for ckey,compute in function.computed|dictsort %}
{% if compute.output == 'int16' %}
{% if template.struct is sameas false %}
import struct
{% set template.struct = true %}
{% endif %}
{% endif %}
{% macro scanForMathLib(logicKeys) -%}
{% for logic in logicKeys %}
{% if logic is mapping %}
{% for logicKey in logic.keys() %}
{% if logicKey == 'power' or logicKey == 'arc tangent' %}
{% if template.math is sameas false %}
import math
{% set template.math = true %}
{% endif %}
{% elif logic[logicKey] is iterable and logic[logicKey] is not string -%}
{{- scanForMathLib(logic[logicKey]) -}}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{%- endmacro %}
{{- scanForMathLib(compute.logic) -}}
{% endfor %}
{% endfor %}
{% endif %}
{%- endmacro %}
{% macro importUstdLibs(fields, functions, template) %}
{% if functions %}
{% for key,function in functions|dictsort %}
{# Check if we need to import `math` lib #}
{% for ckey,compute in function.computed|dictsort %}
{% if compute.output == 'int16' %}
{% if template.struct is sameas false %}
import ustruct
{% set template.struct = true %}
{% endif %}
{% endif %}
{% macro scanForMathLib(logicKeys) -%}
{% for logic in logicKeys %}
{% if logic is mapping %}
{% for logicKey in logic.keys() %}
{% if logicKey == 'power' or logicKey == 'arc tangent' %}
{% if template.math is sameas false %}
import math
{% set template.math = true %}
{% endif %}
{% elif logicKey == '$delay' %}
{% if template.time is sameas false %}
import time
{% set template.time = true %}
{% endif %}
{% elif logic[logicKey] is iterable and logic[logicKey] is not string -%}
{{- scanForMathLib(logic[logicKey]) -}}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{%- endmacro %}
{{- scanForMathLib(compute.logic) -}}
{% endfor %}
{% endfor %}
{% endif %}
{%- endmacro %}
{% macro importLittleEndian(i2c) %}
{% if i2c.endian == 'little' %}
def _swap_endian(val, length):
"""
Swap the endianness of a number
"""
if length <= 8:
return val
if length <= 16:
return (val & 0xFF00) >> 8 | (val & 0xFF) << 8
if length <= 32:
return ((val & 0xFF000000) >> 24 |
(val & 0x00FF0000) >> 8 |
(val & 0x0000FF00) << 8 |
(val & 0x000000FF) << 24)
raise Exception('Cannot swap endianness for length ' + length)
{% endif %}
{%- endmacro %}
{% macro importSign(registers, template) %}
{# Add signing function if needed #}
{% for key,register in registers|dictsort %}
{% if register.signed %}
{# Optionally import class #}
{% if template.sign is sameas false %}
def _sign(val, length):
"""
Convert unsigned integer to signed integer
"""
if val & (1 << (length - 1)):
return val - (1 << length)
return val
{% set template.sign = true %}
{% endif %}
{% endif %}
{% endfor %}
{%- endmacro %}