Skip to content

Commit db9110e

Browse files
committed
eds: Factor out object code constants.
Move the ARR, DOMAIN, RECORD, VAR constant definitions to their own "objectcodes" module and add missing ones from the standard. Rename ARR to ARRAY. Add a reverse lookup function code2str() for convenience. Adjust all uses within the eds module.
1 parent 7c6dee5 commit db9110e

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

canopen/objectdictionary/eds.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
ODVariable,
1313
ObjectDictionary,
1414
datatypes,
15+
objectcodes,
1516
)
1617
from canopen.sdo import SdoClient
1718

@@ -21,13 +22,6 @@
2122

2223
logger = logging.getLogger(__name__)
2324

24-
# Object type. Don't confuse with Data type
25-
DOMAIN = 2
26-
VAR = 7
27-
ARR = 8
28-
RECORD = 9
29-
30-
3125
def import_eds(source, node_id):
3226
eds = RawConfigParser(inline_comment_prefixes=(';',))
3327
eds.optionxform = str
@@ -128,16 +122,16 @@ def import_eds(source, node_id):
128122
# DS306 4.6.3.2 object description
129123
# If the keyword ObjectType is missing, this is regarded as
130124
# "ObjectType=0x7" (=VAR).
131-
object_type = VAR
125+
object_type = objectcodes.VAR
132126
try:
133127
storage_location = eds.get(section, "StorageLocation")
134128
except NoOptionError:
135129
storage_location = None
136130

137-
if object_type in (VAR, DOMAIN):
131+
if object_type in (objectcodes.VAR, objectcodes.DOMAIN):
138132
var = build_variable(eds, section, node_id, index)
139133
od.add_object(var)
140-
elif object_type == ARR and eds.has_option(section, "CompactSubObj"):
134+
elif object_type == objectcodes.ARRAY and eds.has_option(section, "CompactSubObj"):
141135
arr = ODArray(name, index)
142136
last_subindex = ODVariable(
143137
"Number of entries", index, 0)
@@ -146,11 +140,11 @@ def import_eds(source, node_id):
146140
arr.add_member(build_variable(eds, section, node_id, index, 1))
147141
arr.storage_location = storage_location
148142
od.add_object(arr)
149-
elif object_type == ARR:
143+
elif object_type == objectcodes.ARRAY:
150144
arr = ODArray(name, index)
151145
arr.storage_location = storage_location
152146
od.add_object(arr)
153-
elif object_type == RECORD:
147+
elif object_type == objectcodes.RECORD:
154148
record = ODRecord(name, index)
155149
record.storage_location = storage_location
156150
od.add_object(record)
@@ -376,7 +370,7 @@ def export_variable(var, eds):
376370
section = f"{var.index:04X}sub{var.subindex:X}"
377371

378372
export_common(var, eds, section)
379-
eds.set(section, "ObjectType", f"0x{VAR:X}")
373+
eds.set(section, "ObjectType", f"0x{objectcodes.VAR:X}")
380374
if var.data_type:
381375
eds.set(section, "DataType", f"0x{var.data_type:04X}")
382376
if var.access_type:
@@ -414,7 +408,7 @@ def export_record(var, eds):
414408
section = f"{var.index:04X}"
415409
export_common(var, eds, section)
416410
eds.set(section, "SubNumber", f"0x{len(var.subindices):X}")
417-
ot = RECORD if isinstance(var, ODRecord) else ARR
411+
ot = objectcodes.RECORD if isinstance(var, ODRecord) else objectcodes.ARRAY
418412
eds.set(section, "ObjectType", f"0x{ot:X}")
419413
for i in var:
420414
export_variable(var[i], eds)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Object codes, as defined by CiA 301, not to be confused with data type."""
2+
3+
NULL = 0x00
4+
DOMAIN = 0x02
5+
DEFTYPE = 0x05
6+
DEFSTRUCT = 0x06
7+
VAR = 0x07
8+
ARRAY = 0x08
9+
RECORD = 0x09
10+
11+
12+
def code2str(code: int) -> str:
13+
"""Return the constant name for the given value, empty if not found."""
14+
for k, v in globals().items():
15+
if k.isupper() and v == code:
16+
return k
17+
return ""

0 commit comments

Comments
 (0)