From 70a07216801253f339cdc820e9f5216ffdd1ff7b Mon Sep 17 00:00:00 2001 From: Sven Sager Date: Wed, 16 Aug 2023 09:40:36 +0200 Subject: [PATCH 1/7] style: With shared_procimg, save changed outputs in set instead of list In sets, elements can only occur once. Therefore, no `if` statement has to check whether the element is already included in the list. Signed-off-by: Sven Sager --- src/revpimodio2/__about__.py | 2 +- src/revpimodio2/device.py | 2 +- src/revpimodio2/io.py | 10 ++++------ 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/revpimodio2/__about__.py b/src/revpimodio2/__about__.py index e83737c..78874e4 100644 --- a/src/revpimodio2/__about__.py +++ b/src/revpimodio2/__about__.py @@ -3,4 +3,4 @@ __author__ = "Sven Sager " __copyright__ = "Copyright (C) 2023 Sven Sager" __license__ = "LGPLv2" -__version__ = "2.6.0" +__version__ = "2.6.1rc1" diff --git a/src/revpimodio2/device.py b/src/revpimodio2/device.py index 0f27359..6631e59 100644 --- a/src/revpimodio2/device.py +++ b/src/revpimodio2/device.py @@ -155,7 +155,7 @@ class Device(object): self.__my_io_list = [] self._selfupdate = False self._shared_procimg = False - self._shared_write = [] + self._shared_write = set() self.shared_procimg(parentmodio._shared_procimg) # Set with register # Wertzuweisung aus dict_device diff --git a/src/revpimodio2/io.py b/src/revpimodio2/io.py index 7e1fdfc..df72dc5 100644 --- a/src/revpimodio2/io.py +++ b/src/revpimodio2/io.py @@ -706,10 +706,9 @@ class IOBase(object): # Für Bitoperationen sperren self._parentdevice._filelock.acquire() - if self._parentdevice._shared_procimg \ - and self not in self._parentdevice._shared_write: + if self._parentdevice._shared_procimg: # Mark this IO for write operations - self._parentdevice._shared_write.append(self) + self._parentdevice._shared_write.add(self) # Hier gibt es immer nur ein byte, als int holen int_byte = self._parentdevice._ba_devdata[self._slc_address.start] @@ -743,11 +742,10 @@ class IOBase(object): ) ) - if self._parentdevice._shared_procimg \ - and self not in self._parentdevice._shared_write: + if self._parentdevice._shared_procimg: with self._parentdevice._filelock: # Mark this IO as changed - self._parentdevice._shared_write.append(self) + self._parentdevice._shared_write.add(self) self._parentdevice._ba_devdata[self._slc_address] = value From 596658d6562596bdada7f1bd9b275ee1adce2128 Mon Sep 17 00:00:00 2001 From: Sven Sager Date: Thu, 17 Aug 2023 07:45:39 +0200 Subject: [PATCH 2/7] fix: `autorefresch` with `shared_procimg` writes all outputs automatic Fixes the bug that with automatic process image update and shared_procimg, outputs of devices are also written without automatic update enabled. Signed-off-by: Sven Sager --- src/revpimodio2/device.py | 4 ---- src/revpimodio2/helper.py | 4 +++- src/revpimodio2/modio.py | 2 -- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/revpimodio2/device.py b/src/revpimodio2/device.py index 6631e59..f2089e1 100644 --- a/src/revpimodio2/device.py +++ b/src/revpimodio2/device.py @@ -531,10 +531,6 @@ class Device(object): with self._filelock: self._shared_write.clear() self._shared_procimg = True if activate else False - if self._shared_procimg and self not in self._modio._lst_shared: - self._modio._lst_shared.append(self) - elif not self._shared_procimg and self in self._modio._lst_shared: - self._modio._lst_shared.remove(self) def syncoutputs(self) -> bool: """ diff --git a/src/revpimodio2/helper.py b/src/revpimodio2/helper.py index d0ae906..f1e9176 100644 --- a/src/revpimodio2/helper.py +++ b/src/revpimodio2/helper.py @@ -531,7 +531,9 @@ class ProcimgWriter(Thread): continue try: - for dev in self._modio._lst_shared: + for dev in self._modio._lst_refresh: + if not dev._shared_procimg: + continue # Set shared outputs before reading process image for io in dev._shared_write: if not io._write_to_procimg(): diff --git a/src/revpimodio2/modio.py b/src/revpimodio2/modio.py index b558fd5..67a8fee 100644 --- a/src/revpimodio2/modio.py +++ b/src/revpimodio2/modio.py @@ -69,7 +69,6 @@ class RevPiModIO(object): "_autorefresh", "_buffedwrite", "_configrsc", "_debug", "_devselect", \ "_exit", "_exit_level", "_imgwriter", "_ioerror", \ "_length", "_looprunning", "_lst_devselect", "_lst_refresh", \ - "_lst_shared", \ "_maxioerrors", "_monitoring", "_myfh", "_myfh_lck", \ "_procimg", "_replace_io_file", "_run_on_pi", \ "_set_device_based_cycle_time", "_simulator", "_shared_procimg", \ @@ -135,7 +134,6 @@ class RevPiModIO(object): self._length = 0 self._looprunning = False self._lst_refresh = [] - self._lst_shared = [] self._maxioerrors = 0 self._myfh = None self._myfh_lck = Lock() From 1afe16053d42045783b47fc5274f7b2fdd29a8db Mon Sep 17 00:00:00 2001 From: Sven Sager Date: Thu, 17 Aug 2023 08:16:41 +0200 Subject: [PATCH 3/7] fix: Set shared_procimg value is stored in the device Classes have accessed a global shared_procimg value during instantiation. However, this value is managed directly for each device individually. There can be no global value. Signed-off-by: Sven Sager --- src/revpimodio2/device.py | 1 - src/revpimodio2/modio.py | 7 +++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/revpimodio2/device.py b/src/revpimodio2/device.py index f2089e1..63a690b 100644 --- a/src/revpimodio2/device.py +++ b/src/revpimodio2/device.py @@ -156,7 +156,6 @@ class Device(object): self._selfupdate = False self._shared_procimg = False self._shared_write = set() - self.shared_procimg(parentmodio._shared_procimg) # Set with register # Wertzuweisung aus dict_device self._name = dict_device.get("name") diff --git a/src/revpimodio2/modio.py b/src/revpimodio2/modio.py index 67a8fee..fbe7bfb 100644 --- a/src/revpimodio2/modio.py +++ b/src/revpimodio2/modio.py @@ -71,7 +71,7 @@ class RevPiModIO(object): "_length", "_looprunning", "_lst_devselect", "_lst_refresh", \ "_maxioerrors", "_monitoring", "_myfh", "_myfh_lck", \ "_procimg", "_replace_io_file", "_run_on_pi", \ - "_set_device_based_cycle_time", "_simulator", "_shared_procimg", \ + "_set_device_based_cycle_time", "_simulator", "_init_shared_procimg", \ "_syncoutputs", "_th_mainloop", "_waitexit", \ "app", "core", "device", "exitsignal", "io", "summary" @@ -117,7 +117,7 @@ class RevPiModIO(object): self._procimg = "/dev/piControl0" if procimg is None else procimg self._set_device_based_cycle_time = True self._simulator = simulator - self._shared_procimg = shared_procimg or direct_output + self._init_shared_procimg = shared_procimg or direct_output self._syncoutputs = syncoutputs # TODO: bei simulator und procimg prüfen ob datei existiert / anlegen? @@ -363,6 +363,9 @@ class RevPiModIO(object): err_names_check[dev_new.name] = [] err_names_check[dev_new.name].append(str(dev_new.position)) + # Set shared_procimg mode, if requested on instantiation + dev_new.shared_procimg(self._init_shared_procimg) + # DeviceList für direkten Zugriff aufbauen setattr(self.device, dev_new.name, dev_new) From 521a0eb58923b2d03fb144ba24f6e42a87d24248 Mon Sep 17 00:00:00 2001 From: Sven Sager Date: Thu, 17 Aug 2023 12:08:08 +0200 Subject: [PATCH 4/7] fix: Wrong values in buffer with autorefresh and shared_procimg active When using `autofresh` in combination with `shared_procimg`, the outputs were written to the process image, but kept with old values in the local buffer. A query to a set output could therefore display an incorrect value under certain circumstances. Closes: #25 Signed-off-by: Sven Sager --- src/revpimodio2/helper.py | 21 ++++++++++++--------- src/revpimodio2/io.py | 1 + 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/revpimodio2/helper.py b/src/revpimodio2/helper.py index f1e9176..81b9115 100644 --- a/src/revpimodio2/helper.py +++ b/src/revpimodio2/helper.py @@ -531,20 +531,23 @@ class ProcimgWriter(Thread): continue try: - for dev in self._modio._lst_refresh: - if not dev._shared_procimg: - continue - # Set shared outputs before reading process image - for io in dev._shared_write: - if not io._write_to_procimg(): - raise IOError("error on _write_to_procimg") - dev._shared_write.clear() - fh.seek(0) fh.readinto(bytesbuff) for dev in self._modio._lst_refresh: with dev._filelock: + if dev._shared_procimg: + # Set modified outputs one by one + for io in dev._shared_write: + if not io._write_to_procimg(): + raise IOError("error on _write_to_procimg") + dev._shared_write.clear() + + # Read all device bytes, because it is shared + fh.seek(dev.offset) + bytesbuff[dev._slc_devoff] = \ + fh.read(len(dev._ba_devdata)) + if self._modio._monitoring or dev._shared_procimg: # Inputs und Outputs in Puffer dev._ba_devdata[:] = bytesbuff[dev._slc_devoff] diff --git a/src/revpimodio2/io.py b/src/revpimodio2/io.py index df72dc5..ceafcb3 100644 --- a/src/revpimodio2/io.py +++ b/src/revpimodio2/io.py @@ -594,6 +594,7 @@ class IOBase(object): return False else: + # Write one or more bytes to process image value = bytes(self._parentdevice._ba_devdata[self._slc_address]) with self._parentdevice._modio._myfh_lck: try: From b02c4011d784ebd4779dad42f7c8f5d788f879cb Mon Sep 17 00:00:00 2001 From: Sven Sager Date: Sat, 9 Sep 2023 11:11:25 +0200 Subject: [PATCH 5/7] fix: Changed misleading text of a ProcimgWriter warning If the IOs cannot be written quickly enough by the ProcimgWriter, this indicates high system utilization. The cycle function itself would trigger a different message through the lck_refresh lock. Signed-off-by: Sven Sager --- src/revpimodio2/helper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/revpimodio2/helper.py b/src/revpimodio2/helper.py index 81b9115..9ad4ad0 100644 --- a/src/revpimodio2/helper.py +++ b/src/revpimodio2/helper.py @@ -617,7 +617,7 @@ class ProcimgWriter(Thread): # Second default_timer call include calculation time from above if default_timer() - ot > self._refresh: warnings.warn( - "cycle time of {0} ms exceeded - can not hold cycle time!" + "io refresh time of {0} ms exceeded!" "".format(int(self._refresh * 1000)), RuntimeWarning ) From b279209b2ba64a97604cbf04ac1ecc5d56ee958b Mon Sep 17 00:00:00 2001 From: Sven Sager Date: Tue, 29 Aug 2023 09:43:32 +0200 Subject: [PATCH 6/7] build: Add environment variable to set alternative venv path Signed-off-by: Sven Sager --- .idea/misc.xml | 2 +- .idea/revpimodio2.iml | 2 +- Makefile | 25 ++++++++++++++++++------- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 3ea7bd2..2a9ab31 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,7 +3,7 @@ - + diff --git a/.idea/revpimodio2.iml b/.idea/revpimodio2.iml index d47a4ef..0e639dc 100644 --- a/.idea/revpimodio2.iml +++ b/.idea/revpimodio2.iml @@ -5,7 +5,7 @@ - + diff --git a/Makefile b/Makefile index 04f00e3..bfd58eb 100644 --- a/Makefile +++ b/Makefile @@ -5,23 +5,34 @@ MAKEFLAGS = --no-print-directory --no-builtin-rules # Variables PACKAGE = revpimodio2 -# If virtualenv exists, use it. If not, use PATH to find, except python3 -SYSTEM_PYTHON = /usr/bin/python3 -PYTHON = $(or $(wildcard venv/bin/python), $(SYSTEM_PYTHON)) +# Set path to create the virtual environment with package name +ifdef PYTHON3_VENV +VENV_PATH = $(PYTHON3_VENV)/$(PACKAGE) +else +VENV_PATH = venv +endif + +# If virtualenv exists, use it. If not, use PATH to find commands +SYSTEM_PYTHON = python3 +PYTHON = $(or $(wildcard $(VENV_PATH)/bin/python), $(SYSTEM_PYTHON)) all: build docs .PHONY: all ## Environment +venv-info: + echo Using path: "$(VENV_PATH)" + exit 0 + venv: - $(SYSTEM_PYTHON) -m venv venv - source venv/bin/activate && \ + $(SYSTEM_PYTHON) -m venv "$(VENV_PATH)" + source $(VENV_PATH)/bin/activate && \ python3 -m pip install --upgrade pip && \ python3 -m pip install -r requirements.txt exit 0 -.PHONY: venv +.PHONY: venv-info venv ## Build, install build: @@ -41,6 +52,6 @@ clean: rm -rf build docs/_build dist src/*.egg-info *.spec clean-all: clean - rm -R venv + rm -R $(VENV_PATH) .PHONY: clean clean-all From acc59ccaa5dcbf840270d55d85d911feba1efc56 Mon Sep 17 00:00:00 2001 From: Sven Sager Date: Sat, 9 Sep 2023 11:55:49 +0200 Subject: [PATCH 7/7] chore: Release 2.6.1 Signed-off-by: Sven Sager --- src/revpimodio2/__about__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/revpimodio2/__about__.py b/src/revpimodio2/__about__.py index 78874e4..c2fd3d4 100644 --- a/src/revpimodio2/__about__.py +++ b/src/revpimodio2/__about__.py @@ -3,4 +3,4 @@ __author__ = "Sven Sager " __copyright__ = "Copyright (C) 2023 Sven Sager" __license__ = "LGPLv2" -__version__ = "2.6.1rc1" +__version__ = "2.6.1"