Skip to content

Commit 60c417b

Browse files
committed
Support PDO lookup by communication parameter record index.
Store each PDO under two different index numbers in the dummy PdoMaps object's "maps" attribute. That would duplicate them in sequential access while iterating / counting though, so override the PdoBase dunder methods to explicitly chain just the two sub-sequences rx and tx.
1 parent 5e5c1e4 commit 60c417b

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

canopen/pdo/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import itertools
12
import logging
3+
from collections.abc import Iterator
24

35
from canopen import node
46
from canopen.pdo.base import PdoBase, PdoMap, PdoMaps, PdoVariable
@@ -35,8 +37,16 @@ def __init__(self, node, rpdo: PdoBase, tpdo: PdoBase):
3537
# The object 0x1A00 equals to key '1' so we remove 1 from the key
3638
for key, value in self.rx.items():
3739
self.map.maps[self.rx.map_offset + (key - 1)] = value
40+
self.map.maps[self.rx.com_offset + (key - 1)] = value
3841
for key, value in self.tx.items():
3942
self.map.maps[self.tx.map_offset + (key - 1)] = value
43+
self.map.maps[self.tx.com_offset + (key - 1)] = value
44+
45+
def __iter__(self) -> Iterator[int]:
46+
return itertools.chain(self.rx, self.tx)
47+
48+
def __len__(self) -> int:
49+
return len(self.rx) + len(self.tx)
4050

4151

4252
class RPDO(PdoBase):

test/test_pdo.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def test_pdo_getitem(self):
5555
self.assertEqual(by_mapping_record['INTEGER16 value'].raw, -3)
5656
self.assertIs(node.tpdo[0x1A00], by_mapping_record)
5757
self.assertIs(node.tpdo[0x1800], by_mapping_record)
58+
self.assertIs(node.pdo[0x1800], by_mapping_record)
5859
by_object_name = node.pdo['INTEGER16 value']
5960
self.assertIsInstance(by_object_name, canopen.pdo.PdoVariable)
6061
self.assertIs(by_object_name.od, node.object_dictionary['INTEGER16 value'])

0 commit comments

Comments
 (0)