Skip to content

Commit 509f553

Browse files
authored
DS402: Cache supported operation modes (#247)
* ds402: Cache supported operation modes. Requesting the same immutable OD object on each change of operation mode takes quite some time to process. Introduce a local cache member variable in order to avoid requesting more than once. * ds402: Log message about cache operation mode support.
1 parent 657062a commit 509f553

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

canopen/profiles/p402.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,13 @@ def is_op_mode_supported(self, mode):
388388
:return: If the operation mode is supported
389389
:rtype: bool
390390
"""
391-
mode_support = self.sdo[0x6502].raw & OperationMode.SUPPORTED[mode]
392-
return mode_support == OperationMode.SUPPORTED[mode]
391+
if not hasattr(self, '_op_mode_support'):
392+
# Cache value only on first lookup, this object should never change.
393+
self._op_mode_support = self.sdo[0x6502].raw
394+
logger.info('Caching node {n} supported operation modes 0x{m:04X}'.format(
395+
n=self.id, m=self._op_mode_support))
396+
bits = OperationMode.SUPPORTED[mode]
397+
return self._op_mode_support & bits == bits
393398

394399
def on_TPDOs_update_callback(self, mapobject):
395400
"""This function receives a map object.

0 commit comments

Comments
 (0)