forked from google/cyanobyte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraspberrypi.py
333 lines (317 loc) · 11.4 KB
/
raspberrypi.py
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
{% import 'macros.jinja2' as utils %}
{% import 'python.jinja2' as py %}
{% set template = namespace(enum=false, sign=false, math=false, struct=false, time=false) %}
{{ utils.pad_string('# ', utils.license(info.copyright.name, info.copyright.date, info.license.name)) -}}
#
# Auto-generated file for {{ info.title }} v{{ info.version }}.
# Generated from {{ fileName }} using Cyanobyte Codegen v{{ version }}
"""
Class for {{ info.title }}
"""
{% macro logic(logicalSteps, function) -%}
{% for step in logicalSteps %}
{% for key in step.keys() %}
{% if step[key] is mapping and 'rawRead' in step[key] %}
{% set bytes = (step[key].rawRead / 8) | round(1, 'ceil') | int %}
{{key}} = 0
{% for n in range(bytes) %}
{# Read each byte one at a time from device #}
_datum = self.bus.read_byte(self.device_address)
{{key}} = {{key}} << 8 | _datum
{% endfor %}
{% break %}
{% endif %}
{# Check if assignment is a send-op #}
{% if key == '$cmdWrite' %}
{% if 'value' in step[key] %}
self.set_{{step[key].register[12:].lower()}}({{step[key].value}})
{% else %}
self.set_{{step[key].register[12:].lower()}}()
{% endif %}
{% break %}
{%- endif %}
{% if key == '$delay' %}
time.sleep({{ step[key].for / 1000 }})
{{ logic(step[key].after, function) }}
{% break %}
{%- endif %}
{# Check if assignment op #}
{% if step[key] is string and step[key][0:1] == "=" %}
{{key | camel_to_snake}} {{step[key]}}
{%- endif %}
{# Check if assignment is a send-op #}
{% if key == 'send' %}
self.set_{{function.register[12:].lower()}}({{step[key]}})
{%- endif %}
{# Check if assignment is register read op #}
{% if step[key] is string and step[key][:12] == '#/registers/' %}
{{key | camel_to_snake}} = self.get_{{step[key][12:].lower()}}()
{%- endif %}
{# Check if assignment is function call op #}
{% if step[key] is string and step[key][:12] == '#/functions/' %}
{{key | camel_to_snake}} = self.{{step[key].lower() | regex_replace('#/functions/(?P<function>.+)/(?P<compute>.+)', '\\g<function>_\\g<compute>')}}()
{%- endif %}
{# If the value is a list, then this is a logical setter #}
{% if step[key] is iterable and step[key] is not string %}
{{key | camel_to_snake}} = {{py.recursiveAssignLogic(step[key][0], step[key][0].keys()) -}}
{%- endif %}
{% endfor %}
{%- endfor %}
{%- endmacro %}
{{ py.importStdLibs(fields, functions, i2c, template) -}}
import smbus
{# Create enums for fields #}
{% if fields %}
{% for key,field in fields|dictsort %}
{% if field.enum %}
{# Create enum class #}
class {{key[0].upper()}}{{key[1:]}}Values(Enum):
"""
{{utils.pad_string(" ", "Valid values for " + field.title)}}
"""
{% for ekey,enum in field.enum|dictsort %}
{{ekey.upper()}} = {{enum.value}} # {{enum.title}}
{% endfor %}
{% endif %}
{% endfor %}
{% endif %}
{% if imports %}
{% for key,data in imports|dictsort %}
{% for structKey,struct in data.structs.items() %}
{% if struct.is_enum %}
{# Create enum class from emb #}
class {{structKey[0].upper()}}{{structKey[1:]}}Values(Enum):
"""
{{utils.pad_string(" ", "Valid values for " + structKey)}}
"""
{% for ekey,enum in struct.fields.items() %}
{% if enum.value is not mapping %}
{% set val = enum.value.split(".")[0]+"Values."+enum.value.split(".")[1].upper() if "." in enum.value else enum.value %}
{{ekey.upper()}} = {{val}}
{% else %}
{% set symbol = enum.value.symbol %}
{% set arg0 = enum.value.arg[0] %}
{% set arg1 = enum.value.arg[1] %}
{% set val0 = arg0.split(".")[0]+"Values."+arg0.split(".")[1].upper() if "." in arg0 else arg0 %}
{% set val1 = arg1.split(".")[0]+"Values."+arg1.split(".")[1].upper() if "." in arg1 else arg1 %}
{{ekey.upper()}} = {{"{} {} {}".format(val0, symbol, val1)}}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}
{% endif %}
{% if i2c.address is iterable and i2c.address is not string %}
class DeviceAddressValues(Enum):
"""
Valid device addresses
"""
{% for address in i2c.address %}
I2C_ADDRESS_{{address}} = {{address}}
{% endfor %}
{% endif %}
{{ py.importLittleEndian(i2c) }}
{{ py.importSign(registers, template) }}
class {{ info.title }}:
"""
{{utils.pad_string(" ", info.description)}}
"""
{% if i2c.address is number %}
device_address = {{i2c.address}}
{% endif %}
{% for key,register in registers|dictsort %}
REGISTER_{{key.upper()}} = {{register.address}}
{% endfor %}
{% if i2c.address is iterable and i2c.address is not string %}
def __init__(self, address):
# Initialize connection to peripheral
self.bus = smbus.SMBus(1)
self.device_address = address
{% else %}
def __init__(self):
# Initialize connection to peripheral
self.bus = smbus.SMBus(1)
{% endif %}
{% if '_lifecycle' in functions and 'Begin' in functions._lifecycle.computed %}
self._lifecycle_begin()
{% endif %}
{% if imports %}
{% for key,data in imports|dictsort %}
{% for structKey,struct in data.structs|dictsort|reverse %}
{% if not struct.is_enum %}
def msg_{{key.lower()}}_{{structKey.lower()}}(
self,
{% for param_name, param_info in struct.fields|dictsort|reverse %}
{{param_name}},
{% endfor %}
): # Put params here
msg = 0
{% for param_name, param_info in struct.fields|dictsort|reverse %}
msg |= {{param_name}} {{"<< {}".format(param_info.offset_in_bit) if param_info.offset_in_bit != 0}}
{% endfor %}
return msg # Return the message data structure here
def decode_{{key.lower()}}_{{structKey.lower()}}(self, msg): # Put params here
res = []
{% for param_name, param_info in struct.fields|dictsort|reverse %}
{{param_name}} = msg {{">> {}".format(param_info.offset_in_bit) if param_info.offset_in_bit != 0}}
{{param_name}} &= 1 << {{param_info.size_in_byte}} - 1
res.append({{param_name}})
{% endfor %}
return res # Return the decoded msg
{% endif %}
{% endfor %}
{% endfor %}
{% endif -%}
{% for key,register in registers|dictsort %}
{% set bytes = (register.length / 8) | round(1, 'ceil') | int %}
{% if (not 'readWrite' in register) or ('readWrite' in register and 'R' is in(register.readWrite)) %}
def get_{{key.lower()}}(self):
"""
{{utils.pad_string(" ", register.description)}}
"""
{% if register.length <= 8 %}
val = self.bus.read_byte_data(
self.device_address,
self.REGISTER_{{key.upper()}}
)
{% elif register.length <= 16 %}
val = self.bus.read_word_data(
self.device_address,
self.REGISTER_{{key.upper()}}
)
{% elif register.length <= 32 %}
byte_list = self.bus.read_i2c_block_data(
self.device_address,
self.REGISTER_{{key.upper()}},
{{bytes}}
)
val = 0
{% for n in range(bytes) %}
val = val << 8 | byte_list[{{n}}]
{% endfor %}
{% endif %}
{% if i2c.endian == 'little' %}
val = _swap_endian(val, {{register.length}})
{% endif %}
{% if register.signed %}
# Unsigned > Signed integer
val = _sign(val, {{register.length}})
{% endif %}
return val
{% endif %}
{% if (not 'readWrite' in register) or ('readWrite' in register and 'W' is in(register.readWrite)) %}
def set_{{key.lower()}}(self{% if register.length > 0 %}, data{% endif %}):
"""
{{utils.pad_string(" ", register.description)}}
"""
{% if i2c.endian == 'little' %}
data = _swap_endian(data, {{register.length}})
{% endif %}
{% if register.length == 0 %}
self.bus.write_i2c_block_data(
self.device_address,
self.REGISTER_{{key.upper()}},
[]
)
{% elif register.length <= 8 %}
self.bus.write_byte_data(
self.device_address,
self.REGISTER_{{key.upper()}},
data
)
{% elif register.length <= 16 %}
self.bus.write_word_data(
self.device_address,
self.REGISTER_{{key.upper()}},
data
)
{% elif register.length <= 32 %}
buffer = []
{% for n in range(bytes) %}
buffer[{{n}}] = (data >> {{8 * (bytes - n - 1)}}) & 0xFF
{% endfor %}
self.bus.write_i2c_block_data(
self.device_address,
self.REGISTER_{{key.upper()}},
buffer
)
{% endif %}
{% endif %}
{% endfor %}
{% if fields %}
{% for key,field in fields|dictsort %}
{% if 'R' is in(field.readWrite) %}
{# Getter #}
def get_{{key.lower()}}(self):
"""
{{utils.pad_string(" ", field.description)}}
"""
# Read register data
# '#/registers/{{field.register[12:]}}' > '{{field.register[12:]}}'
val = self.get_{{field.register[12:].lower()}}()
# Mask register value
val = val & {{utils.mask(field.bitStart, field.bitEnd)}}
{% if field.bitEnd %}
# Bitshift value
val = val >> {{field.bitEnd}}
{% endif %}
return val
{% endif -%}
{%- if 'W' is in(field.readWrite) %}
{# Setter #}
def set_{{key.lower()}}(self, data):
"""
{{utils.pad_string(" ", field.description)}}
"""
{% if field.bitEnd %}
# Bitshift value
data = data << {{field.bitEnd}}
{% endif %}
# Read current register data
# '#/registers/{{field.register[12:]}}' > '{{field.register[12:]}}'
register_data = self.get_{{field.register[12:].lower()}}()
register_data = register_data | data
self.set_{{field.register[12:].lower()}}(register_data)
{% endif %}
{% endfor %}
{% endif %}
{% if functions %}
{% for key,function in functions|dictsort %}
{% for ckey,compute in function.computed|dictsort %}
{% if compute.input %}
def {{key.lower()}}_{{ckey.lower()}}(self, {{py.params(compute.input)}}):
{% else %}
def {{key.lower()}}_{{ckey.lower()}}(self):
{% endif %}
"""
{{utils.pad_string(" ", function.description)}}
"""
{# Declare our variables #}
{{ py.variables(compute.variables) }}
{# Read `value` if applicable #}
{% if 'input' in compute %}
{%- for vkey,variable in compute.input|dictsort %}
{% if vkey == 'value' %}
# Read value of register into a variable
value = self.get_{{function.register[12:].lower()}}()
{% endif %}
{% endfor -%}
{% endif %}
{# Handle the logic #}
{{ logic(compute.logic, function) -}}
{# Return if applicable #}
{# Return a tuple #}
{% if 'return' in compute and compute.return is not string %}
return [{% for returnValue in compute.return %}{{ returnValue | camel_to_snake }}{{ ", " if not loop.last }}{% endfor %}]
{# Return a plain value #}
{% elif compute.return is string %}
{# See if we need to massage the data type #}
{% if compute.output == 'int16' %}
# Convert from a unsigned short to a signed short
{{compute.return}} = struct.unpack("h", struct.pack("H", {{compute.return}}))[0]
{% endif %}
return {{compute.return}}
{% endif %}
{% endfor %}
{% endfor %}
{% endif %}