-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcbusconfig.py
472 lines (380 loc) · 13.2 KB
/
cbusconfig.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# cbusconfig.py
import gc
from micropython import const
import cbus
# import i2ceeprom
import logger
FILES_CONFIG_FILENAME = const('/config.dat')
FILES_NVS_FILENAME = const('/nvs.dat')
FILES_EVENTS_FILENAME = const('/events.dat')
# JSON_NVS_FILENAME = const('/nvs.json')
# JSON_EVENTS_FILE_NAME = const('/events.json')
CONFIG_TYPE_FILES = const(0)
# CONFIG_TYPE_JSON = const(1)
# CONFIG_TYPE_I2C_EEPROM = const(2)
class storage_backend:
events = ''
nvs = ''
def __init__(self, storage_type=CONFIG_TYPE_FILES, ev_offset: int = 0):
self.logger = logger.logger()
self.storage_type = storage_type
self.ev_offset = ev_offset
def init_config(self, config):
pass
def load_config(self, data_len):
pass
def store_config(self, config):
pass
def init_events(self, events):
pass
def load_events(self, ev_size):
pass
def store_events(self, events):
pass
def init_nvs(self, nvs):
pass
def load_nvs(self, num_nvs):
pass
def store_nvs(self, nvs):
pass
# backend using binary files
class files_backend(storage_backend):
def __init__(self, ev_offset: int = 0):
self.logger = logger.logger()
super().__init__(ev_offset)
def init_config(self, config):
f = open(FILES_CONFIG_FILENAME, 'wb')
f.write(bytearray(config))
f.close()
def load_config(self, data_len):
try:
f = open(FILES_CONFIG_FILENAME, 'rb')
except OSError:
return None
try:
data = f.read()
except UnicodeError:
return None
f.close()
return bytearray(data)
def store_config(self, config):
f = open(FILES_CONFIG_FILENAME, 'wb')
f.write(bytearray(config))
f.close()
def init_events(self, events):
f = open(FILES_EVENTS_FILENAME, 'wb')
f.write(bytearray(events))
f.close()
def load_events(self, ev_size):
try:
f = open(FILES_EVENTS_FILENAME, 'rb')
except OSError:
return None
try:
data = f.read()
except UnicodeError:
return None
f.close()
return bytearray(data)
def store_events(self, events):
f = open(FILES_EVENTS_FILENAME, 'wb')
f.write(bytearray(events))
f.close()
def init_nvs(self, nvs):
f = open(FILES_NVS_FILENAME, 'wb')
f.write(bytearray(nvs))
f.close()
def load_nvs(self, num_nvs):
try:
f = open(FILES_NVS_FILENAME, 'rb')
except OSError:
return None
try:
data = f.read()
except UnicodeError:
return None
f.close()
return bytearray(data)
def store_nvs(self, nvs):
f = open(FILES_NVS_FILENAME, 'wb')
f.write(bytearray(nvs))
f.close()
# backend using json text files
# class json_backend(storage_backend):
#
# def __init__(self, ev_offset: int = 0):
# self.logger = logger.logger()
# super(json_backend, self).__init__(ev_offset)
#
# @staticmethod
# def dict_from_array(a: list) -> dict:
# d = dict([(key, value) for key, value in enumerate(a)])
# return d
#
# def init_events(self, events):
# f = open(JSON_EVENTS_FILE_NAME, 'w')
# d = self.dict_from_array(events)
# s = json.dumps(d)
# f.write(bytearray(s))
# f.close()
#
# def load_events(self, ev_size):
# try:
# f = open(JSON_EVENTS_FILE_NAME, 'r')
# except OSError:
# return None
#
# try:
# data = f.read()
# except UnicodeError:
# return None
#
# f.close()
# s = data.encode('ascii')
# d = json.loads(s)
# b = bytearray(d.values())
# return bytearray(b)
#
# def store_events(self, events):
# f = open(JSON_EVENTS_FILE_NAME, 'w')
# d = self.dict_from_array(events)
# s = json.dumps(d)
# f.write(bytearray(s))
# f.close()
#
# def init_nvs(self, nvs):
# with open(JSON_NVS_FILENAME, 'w') as f:
# d = self.dict_from_array(nvs)
# s = json.dumps(d)
# f.write(bytearray(s))
#
# def load_nvs(self, num_nvs):
# try:
# f = open(JSON_NVS_FILENAME, 'r')
# except OSError:
# return None
#
# try:
# data = f.read()
# except UnicodeError:
# return None
#
# f.close()
# s = data.encode('ascii')
# d = json.loads(s)
# b = bytearray(d.values())
# return bytearray(b)
#
# def store_nvs(self, nvs):
# f = open(JSON_NVS_FILENAME, 'w')
# d = self.dict_from_array(nvs)
# s = json.dumps(d)
# f.write(bytearray(s))
# f.close()
# class eeprom_backend(storage_backend):
# def __init__(self, ev_offset):
# self.logger = logger.logger()
# # self.logger.log(f'eeprom_backend init, offset = {ev_offset}')
# super().__init__(ev_offset)
# self.eeprom = i2ceeprom.i2ceeprom()
#
# def load_events(self, ev_size):
#
# data = bytearray()
#
# for i in range(0, ev_size):
# data.extend(self.eeprom.read(i + self.ev_offset))
#
# return data
#
# def store_events(self, events):
# for i in range(0, len(events)):
# self.eeprom.write((i + self.ev_offset), events[i])
#
# def load_nvs(self, num_nvs):
#
# data = bytearray()
#
# for i in range(0, num_nvs):
# data.extend(self.eeprom.read(i))
#
# return data
#
# def store_nvs(self, nvs):
# for i in range(0, len(nvs)):
# self.eeprom.write(i, nvs[i])
class cbusconfig:
def __init__(self, storage_type=CONFIG_TYPE_FILES, num_nvs=20, num_events=64, num_evs=4):
self.logger = logger.logger()
self.storage_type = storage_type
self.num_nvs = num_nvs
self.num_events = num_events
self.num_evs = num_evs
self.event_size = 4 + self.num_evs
if self.storage_type == CONFIG_TYPE_FILES:
self.backend = files_backend()
# elif self.storage_type == CONFIG_TYPE_JSON:
# self.backend = json_backend()
else:
raise ValueError('unknown storage type')
self.mode = 0
self.canid = 0
self.node_number = 0
self.was_reset = False
def begin(self) -> None:
# load or init module config data
self.config_data = self.backend.load_config(10)
if not self.config_data:
self.config_data = bytearray(0x0 for _ in range(10))
self.backend.init_config(self.config_data)
self.mode = self.config_data[0]
self.canid = self.config_data[1]
self.node_number = (self.config_data[2] << 8) + self.config_data[3]
self.was_reset = self.config_data[4]
# load or init NVs
self.nvs = self.backend.load_nvs(self.num_nvs)
if not self.nvs:
self.nvs = bytearray(0x0 for _ in range(self.num_nvs))
self.backend.init_nvs(self.nvs)
# load or init events
self.events = self.backend.load_events(self.num_events * self.event_size)
if not self.events:
self.events = bytearray(0xff for _ in range(self.num_events * self.event_size))
self.backend.init_events(self.events)
def set_mode(self, mode: int) -> None:
self.config_data[0] = mode
self.backend.store_config(self.config_data)
self.mode = mode
def set_canid(self, canid: int) -> None:
self.config_data[1] = canid
self.backend.store_config(self.config_data)
self.canid = canid
def set_node_number(self, node_number: int) -> None:
self.config_data[2] = int(node_number >> 8)
self.config_data[3] = node_number & 0xff
self.backend.store_config(self.config_data)
self.node_number = node_number
def set_reset_flag(self, state: bool) -> None:
self.config_data[4] = state
self.backend.store_config(self.config_data)
def find_existing_event(self, nn: int, en: int, opcode: int=0) -> int:
if opcode & (1 << 3):
# zero the NN for short events
nn = 0
for i in range(self.num_events):
offset = i * self.event_size
if ((self.events[offset] << 8) + self.events[offset + 1]) == nn and (
(self.events[offset + 2] << 8) + self.events[offset + 3]) == en:
return i
return -1
def find_event_by_ev(self, evnum: int, evval: int) -> int:
for i in range(self.num_events):
if self.read_event_ev(i, evnum) == evval:
return i
return -1
# test ... mod.cbus.config.find_event_by_evs(((1, 3), (2, 6), (3, 9), (4, 12),))
def find_event_by_evs(self, query: tuple[tuple[int, int], ...]) -> int:
for i in range(self.num_events):
found = True
for j in range(len(query)):
if self.read_event_ev(i, query[j][0]) != query[j][1]:
found = False
break
if found:
return i
return -1
def find_event_space(self) -> int:
for i in range(self.num_events):
offset = i * self.event_size
if (
self.events[offset] == 0xff
and self.events[offset + 1] == 0xff
and self.events[offset + 2] == 0xff
and self.events[offset + 3] == 0xff
):
return i
return -1
def read_event(self, index: int) -> bytearray:
offset = self.event_size * index
return self.events[offset: offset + self.event_size]
def write_event(self, nn: int, en: int, evnum: int, evval: int) -> bool:
idx = self.find_existing_event(nn, en)
if idx < 0:
idx = self.find_event_space()
if idx < 0:
return False
offset = idx * self.event_size
self.events[offset] = int(nn >> 8)
self.events[offset + 1] = nn & 0xff
self.events[offset + 2] = int(en >> 8)
self.events[offset + 3] = en & 0xff
self.events[offset + 4 + (evnum - 1)] = evval
self.backend.store_events(self.events)
return True
def read_event_ev(self, idx: int, evnum: int) -> int:
return self.events[(idx * self.event_size) + 4 + (evnum - 1)]
def write_event_ev(self, idx, evnum: int, evval: int) -> None:
self.events[(idx * self.event_size) + 4 + (evnum - 1)] = evval
self.backend.store_events(self.events)
def clear_event(self, nn: int, en: int) -> bool:
idx = self.find_existing_event(nn, en)
if idx < 0:
return False
for i in range(self.event_size):
self.events[(idx * self.event_size) + 1] = 0xff
self.backend.store_events(self.events)
return True
def count_events(self) -> int:
count = 0
for i in range(self.num_events):
t = self.read_event(i)
if t[0] == 0xff and t[1] == 0xff and t[2] == 0xff and t[3] == 0xff:
pass
else:
count += 1
return count
def clear_all_events(self) -> None:
self.events = bytearray([0xff] * (self.num_events * self.event_size))
self.backend.store_events(self.events)
def read_nv(self, nvnum: int) -> int:
return self.nvs[nvnum - 1]
def write_nv(self, nvnum: int, value: int) -> None:
self.nvs[nvnum - 1] = value
self.backend.store_nvs(self.nvs)
def print_event_table(self, hex: bool = True, print_all: bool = False) -> None:
for i in range(self.num_events):
if print_all or (self.events[i * self.event_size] < 0xff):
print(f'{i:3} = ', end='')
for j in range(0, self.event_size):
if hex:
print(f'{self.events[(i * self.event_size) + j]:02x} ', end='')
else:
print(f'{self.events[(i * self.event_size) + j]:03} ', end='')
print()
def print_nvs(self) -> None:
for i, nv in enumerate(self.nvs):
print(f'{i + 1:3} = {nv:03} 0x{nv:02x}')
def print_config(self) -> None:
for i, cf in enumerate(self.config_data):
print(f'{i:3} = {cf:03} 0x{cf:02x}')
def reset_module(self) -> None:
if self.mode == cbus.MODE_SLIM:
self.logger.log('reset_module')
self.config_data = bytearray(0x0 for _ in range(10))
self.nvs = bytearray(0x0 for _ in range(self.num_nvs))
self.events = bytearray(0xff for _ in range(self.num_events * self.event_size))
self.backend.store_config(self.config_data)
self.backend.store_nvs(self.nvs)
self.backend.store_events(self.events)
self.set_reset_flag(True)
self.reboot()
else:
self.logger.log('set module to SLiM before resetting')
@staticmethod
def reboot() -> None:
import machine
machine.soft_reset()
@staticmethod
def free_memory() -> int:
gc.collect()
return gc.mem_free()