Skip to content

Commit

Permalink
DS402: Cache supported operation modes (#247)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
acolomb authored Aug 2, 2021
1 parent 657062a commit 509f553
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions canopen/profiles/p402.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,13 @@ def is_op_mode_supported(self, mode):
:return: If the operation mode is supported
:rtype: bool
"""
mode_support = self.sdo[0x6502].raw & OperationMode.SUPPORTED[mode]
return mode_support == OperationMode.SUPPORTED[mode]
if not hasattr(self, '_op_mode_support'):
# Cache value only on first lookup, this object should never change.
self._op_mode_support = self.sdo[0x6502].raw
logger.info('Caching node {n} supported operation modes 0x{m:04X}'.format(
n=self.id, m=self._op_mode_support))
bits = OperationMode.SUPPORTED[mode]
return self._op_mode_support & bits == bits

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

0 comments on commit 509f553

Please sign in to comment.