From 33111e72d2767b4f8541d716dd63056391440a53 Mon Sep 17 00:00:00 2001 From: Svein Seldal Date: Tue, 14 May 2024 17:50:59 +0200 Subject: [PATCH 1/2] Rename Maps to PdoMaps and Map to PdoMap --- canopen/pdo/__init__.py | 6 +++--- canopen/pdo/base.py | 18 ++++++++++-------- canopen/profiles/p402.py | 6 +++--- doc/pdo.rst | 4 ++-- 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/canopen/pdo/__init__.py b/canopen/pdo/__init__.py index 46396e30..0f8cbf90 100644 --- a/canopen/pdo/__init__.py +++ b/canopen/pdo/__init__.py @@ -1,7 +1,7 @@ import logging from canopen import node -from canopen.pdo.base import PdoBase, Maps +from canopen.pdo.base import PdoBase, PdoMaps # Compatibility from .base import Variable @@ -38,7 +38,7 @@ class RPDO(PdoBase): def __init__(self, node): super(RPDO, self).__init__(node) - self.map = Maps(0x1400, 0x1600, self, 0x200) + self.map = PdoMaps(0x1400, 0x1600, self, 0x200) logger.debug('RPDO Map as {0}'.format(len(self.map))) def stop(self): @@ -63,7 +63,7 @@ class TPDO(PdoBase): def __init__(self, node): super(TPDO, self).__init__(node) - self.map = Maps(0x1800, 0x1A00, self, 0x180) + self.map = PdoMaps(0x1800, 0x1A00, self, 0x180) logger.debug('TPDO Map as {0}'.format(len(self.map))) def stop(self): diff --git a/canopen/pdo/base.py b/canopen/pdo/base.py index 52a0aa04..9d98fae4 100644 --- a/canopen/pdo/base.py +++ b/canopen/pdo/base.py @@ -27,7 +27,7 @@ class PdoBase(Mapping): def __init__(self, node): self.network = None - self.map = None # instance of Maps + self.map = None # instance of PdoMaps self.node = node def __iter__(self): @@ -124,7 +124,7 @@ def stop(self): pdo_map.stop() -class Maps(Mapping): +class PdoMaps(Mapping[int, "PdoMap"]): """A collection of transmit or receive maps.""" def __init__(self, com_offset, map_offset, pdo_node: PdoBase, cob_base=None): @@ -134,10 +134,10 @@ def __init__(self, com_offset, map_offset, pdo_node: PdoBase, cob_base=None): :param pdo_node: :param cob_base: """ - self.maps: Dict[int, "Map"] = {} + self.maps: Dict[int, "PdoMap"] = {} for map_no in range(512): if com_offset + map_no in pdo_node.node.object_dictionary: - new_map = Map( + new_map = PdoMap( pdo_node, pdo_node.node.sdo[com_offset + map_no], pdo_node.node.sdo[map_offset + map_no]) @@ -146,7 +146,7 @@ def __init__(self, com_offset, map_offset, pdo_node: PdoBase, cob_base=None): new_map.predefined_cob_id = cob_base + map_no * 0x100 + pdo_node.node.id self.maps[map_no + 1] = new_map - def __getitem__(self, key: int) -> "Map": + def __getitem__(self, key: int) -> "PdoMap": return self.maps[key] def __iter__(self) -> Iterable[int]: @@ -156,7 +156,7 @@ def __len__(self) -> int: return len(self.maps) -class Map: +class PdoMap: """One message which can have up to 8 bytes of variables mapped.""" def __init__(self, pdo_node, com_record, map_array): @@ -304,12 +304,12 @@ def on_message(self, can_id, data, timestamp): for callback in self.callbacks: callback(self) - def add_callback(self, callback: Callable[["Map"], None]) -> None: + def add_callback(self, callback: Callable[["PdoMap"], None]) -> None: """Add a callback which will be called on receive. :param callback: The function to call which must take one argument of a - :class:`~canopen.pdo.Map`. + :class:`~canopen.pdo.PdoMap`. """ self.callbacks.append(callback) @@ -609,3 +609,5 @@ def set_data(self, data: bytes): # For compatibility Variable = PdoVariable +Maps = PdoMaps +Map = PdoMap diff --git a/canopen/profiles/p402.py b/canopen/profiles/p402.py index 2e5fb133..d5b4ce5e 100644 --- a/canopen/profiles/p402.py +++ b/canopen/profiles/p402.py @@ -212,8 +212,8 @@ class BaseNode402(RemoteNode): def __init__(self, node_id, object_dictionary): super(BaseNode402, self).__init__(node_id, object_dictionary) self.tpdo_values = {} # { index: value from last received TPDO } - self.tpdo_pointers = {} # { index: pdo.Map instance } - self.rpdo_pointers = {} # { index: pdo.Map instance } + self.tpdo_pointers = {} # { index: pdo.PdoMap instance } + self.rpdo_pointers = {} # { index: pdo.PdoMap instance } def setup_402_state_machine(self, read_pdos=True): """Configure the state machine by searching for a TPDO that has the StatusWord mapped. @@ -459,7 +459,7 @@ def on_TPDOs_update_callback(self, mapobject): """Cache updated values from a TPDO received from this node. :param mapobject: The received PDO message. - :type mapobject: canopen.pdo.Map + :type mapobject: canopen.pdo.PdoMap """ for obj in mapobject: self.tpdo_values[obj.index] = obj.raw diff --git a/doc/pdo.rst b/doc/pdo.rst index 05e1e94d..bcef197b 100644 --- a/doc/pdo.rst +++ b/doc/pdo.rst @@ -89,7 +89,7 @@ API .. describe:: pdo[no] - Return the :class:`canopen.pdo.Map` for the specified map number. + Return the :class:`canopen.pdo.PdoMap` for the specified map number. First map starts at 1. .. describe:: iter(pdo) @@ -101,7 +101,7 @@ API Return the number of supported maps. -.. autoclass:: canopen.pdo.Map +.. autoclass:: canopen.pdo.PdoMap :members: .. describe:: map[name] From 172bb9143b6c9d17bb0bbab1f083f4c757e76140 Mon Sep 17 00:00:00 2001 From: Svein Seldal Date: Wed, 15 May 2024 20:32:49 +0200 Subject: [PATCH 2/2] Remove type hint that doesn't work with py3.8 --- canopen/pdo/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/canopen/pdo/base.py b/canopen/pdo/base.py index 9d98fae4..4ab95d52 100644 --- a/canopen/pdo/base.py +++ b/canopen/pdo/base.py @@ -124,7 +124,7 @@ def stop(self): pdo_map.stop() -class PdoMaps(Mapping[int, "PdoMap"]): +class PdoMaps(Mapping): """A collection of transmit or receive maps.""" def __init__(self, com_offset, map_offset, pdo_node: PdoBase, cob_base=None):