Skip to content

Commit 6bc9305

Browse files
committed
Custom Options for EDS file
Allowed to Read/Write options not defined by the EDS Standard.
1 parent 54ac5c2 commit 6bc9305

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

canopen/objectdictionary/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ def __init__(self, name: str, index: int):
209209
self.storage_location = None
210210
self.subindices = {}
211211
self.names = {}
212+
#: Key-Value pairs not defined by the standard
213+
self.custom_options = {}
212214

213215
def __repr__(self) -> str:
214216
return f"<{type(self).__qualname__} {self.name!r} at {pretty_index(self.index)}>"
@@ -268,6 +270,8 @@ def __init__(self, name: str, index: int):
268270
self.storage_location = None
269271
self.subindices = {}
270272
self.names = {}
273+
#: Key-Value pairs not defined by the standard
274+
self.custom_options = {}
271275

272276
def __repr__(self) -> str:
273277
return f"<{type(self).__qualname__} {self.name!r} at {pretty_index(self.index)}>"
@@ -374,6 +378,8 @@ def __init__(self, name: str, index: int, subindex: int = 0):
374378
self.storage_location = None
375379
#: Can this variable be mapped to a PDO
376380
self.pdo_mappable = False
381+
#: Key-Value pairs not defined by the standard
382+
self.custom_options = {}
377383

378384
def __repr__(self) -> str:
379385
subindex = self.subindex if isinstance(self.parent, (ODRecord, ODArray)) else None

canopen/objectdictionary/eds.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ def import_eds(source, node_id):
131131

132132
if object_type in (VAR, DOMAIN):
133133
var = build_variable(eds, section, node_id, index)
134+
var.custom_options = _get_custom_options(eds, section)
134135
od.add_object(var)
135136
elif object_type == ARR and eds.has_option(section, "CompactSubObj"):
136137
arr = objectdictionary.ODArray(name, index)
@@ -140,14 +141,17 @@ def import_eds(source, node_id):
140141
arr.add_member(last_subindex)
141142
arr.add_member(build_variable(eds, section, node_id, index, 1))
142143
arr.storage_location = storage_location
144+
arr.custom_options = _get_custom_options(eds, section)
143145
od.add_object(arr)
144146
elif object_type == ARR:
145147
arr = objectdictionary.ODArray(name, index)
146148
arr.storage_location = storage_location
149+
arr.custom_options = _get_custom_options(eds, section)
147150
od.add_object(arr)
148151
elif object_type == RECORD:
149152
record = objectdictionary.ODRecord(name, index)
150153
record.storage_location = storage_location
154+
record.custom_options = _get_custom_options(eds, section)
151155
od.add_object(record)
152156

153157
continue
@@ -253,6 +257,18 @@ def _revert_variable(var_type, value):
253257
else:
254258
return f"0x{value:02X}"
255259

260+
_STANDARD_OPTIONS = ["ObjectType" , "ParameterName" , "DataType" , "AccessType" ,
261+
"PDOMapping" , "LowLimit" , "HighLimit" , "DefaultValue" ,
262+
"ParameterValue" , "Factor" , "Description" , "Unit" ,
263+
"StorageLocation" , "CompactSubObj" ]
264+
265+
def _get_custom_options(eds, section):
266+
custom_options = {}
267+
for option, value in eds.items(section):
268+
if option not in _STANDARD_OPTIONS:
269+
custom_options[option] = value
270+
return custom_options
271+
256272

257273
def build_variable(eds, section, node_id, index, subindex=0):
258274
"""Creates a object dictionary entry.
@@ -332,6 +348,8 @@ def build_variable(eds, section, node_id, index, subindex=0):
332348
var.unit = eds.get(section, "Unit")
333349
except ValueError:
334350
pass
351+
352+
var.custom_options = _get_custom_options(eds, section)
335353
return var
336354

337355

@@ -406,12 +424,17 @@ def export_variable(var, eds):
406424
if getattr(var, 'unit', '') != '':
407425
eds.set(section, "Unit", var.unit)
408426

427+
for option, value in var.custom_options.items():
428+
eds.set(section, option, value)
429+
409430
def export_record(var, eds):
410431
section = f"{var.index:04X}"
411432
export_common(var, eds, section)
412433
eds.set(section, "SubNumber", f"0x{len(var.subindices):X}")
413434
ot = RECORD if isinstance(var, objectdictionary.ODRecord) else ARR
414435
eds.set(section, "ObjectType", f"0x{ot:X}")
436+
for option, value in var.custom_options.items():
437+
eds.set(section, option, value)
415438
for i in var:
416439
export_variable(var[i], eds)
417440

@@ -498,19 +521,19 @@ def export_record(var, eds):
498521
def mandatory_indices(x):
499522
return x in {0x1000, 0x1001, 0x1018}
500523

501-
def manufacturer_idices(x):
502-
return x in range(0x2000, 0x6000)
524+
def manufacturer_indices(x):
525+
return 0x2000 <= x < 0x6000
503526

504527
def optional_indices(x):
505528
return all((
506529
x > 0x1001,
507530
not mandatory_indices(x),
508-
not manufacturer_idices(x),
531+
not manufacturer_indices(x),
509532
))
510533

511534
supported_mantatory_indices = list(filter(mandatory_indices, od))
512535
supported_optional_indices = list(filter(optional_indices, od))
513-
supported_manufacturer_indices = list(filter(manufacturer_idices, od))
536+
supported_manufacturer_indices = list(filter(manufacturer_indices, od))
514537

515538
def add_list(section, list):
516539
eds.add_section(section)

0 commit comments

Comments
 (0)