Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions canopen/pdo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,35 @@
logger = logging.getLogger(__name__)


class _CombinedPdoMaps(Mapping[int, PdoMap]):
"""Combine RPDO and TPDO :class:`PdoMaps` without dummy zero offsets.

Avoids ``PdoMaps(0, 0, …)`` where ``__getitem__`` fallbacks would alias
``key`` to ``key + 1`` (see discussion on PR #613).
"""

def __init__(self, rx: PdoMaps, tx: PdoMaps):
self.rx = rx
self.tx = tx

def __getitem__(self, key: int) -> PdoMap:
for maps in (self.rx, self.tx):
try:
return maps[key]
except KeyError:
continue
raise KeyError(key)

def __iter__(self) -> Iterator[int]:
return itertools.chain(
(self.rx.map_offset + i - 1 for i in self.rx),
(self.tx.map_offset + i - 1 for i in self.tx),
)

def __len__(self) -> int:
return len(self.rx) + len(self.tx)


class PDO(PdoBase):
"""PDO Class for backwards compatibility.

Expand Down