From acd41302c7b42db95467c32f48ea575e00cdc1fc Mon Sep 17 00:00:00 2001
From: wendtalexander <wendtalexander@protonmail.com>
Date: Thu, 24 Oct 2024 11:51:17 +0200
Subject: [PATCH] [ui/plots] plotting now two different channels from the
 buffer

---
 pyrelacs/ui/plots/continously.py | 106 +++++++++++++++++++++++--------
 1 file changed, 80 insertions(+), 26 deletions(-)

diff --git a/pyrelacs/ui/plots/continously.py b/pyrelacs/ui/plots/continously.py
index ece2ca2..9d9da5b 100644
--- a/pyrelacs/ui/plots/continously.py
+++ b/pyrelacs/ui/plots/continously.py
@@ -15,7 +15,8 @@ class Continously:
     def __init__(self, figure: pg.GraphicsLayoutWidget, buffer: CircBuffer):
         self.figure = figure
         self.buffer = buffer
-        self.last_plotted_index = 0
+        self.last_plotted_index_analog_in_0 = 0
+        self.last_plotted_index_analog_in_1 = 0
         self.timer = QTimer()
 
     def plot(self, *args, **kwargs):
@@ -24,14 +25,25 @@ class Continously:
         prev_plot = self.figure.getItem(row=0, col=0)
         if prev_plot:
             self.figure.removeItem(prev_plot)
-        self.continous_ax = self.figure.addPlot(row=0, col=0)
+        self.analog_in_0 = self.figure.addPlot(row=0, col=0)
+        self.analog_in_1 = self.figure.addPlot(row=1, col=0)
 
         pen = pg.mkPen("red")
-        self.time = np.zeros(self.buffer.size)
-        self.data = np.empty(self.buffer.size)
-        self.line = self.continous_ax.plot(
-            self.time,
-            self.data,
+        self.time_analog_in_0 = np.zeros(self.buffer.size)
+        self.data_analog_in_0 = np.empty(self.buffer.size)
+        self.line_analog_in_0 = self.analog_in_0.plot(
+            self.time_analog_in_0,
+            self.data_analog_in_0,
+            pen=pen,
+            # symbol="o",
+        )
+
+        pen = pg.mkPen("red")
+        self.time_analog_in_1 = np.zeros(self.buffer.size)
+        self.data_analog_in_1 = np.empty(self.buffer.size)
+        self.line_analog_in_1 = self.analog_in_1.plot(
+            self.time_analog_in_1,
+            self.data_analog_in_1,
             pen=pen,
             # symbol="o",
         )
@@ -41,26 +53,61 @@ class Continously:
         self.PLOT_HISTORY = 500_000  # The amount of data you want to keep on screen
         self.timer.setInterval(150)
         self.timer.timeout.connect(self.update_plot)
+        self.timer.timeout.connect(self.update_plot_1)
         self.timer.start()
 
     def update_plot(self):
         current_index = self.buffer.write_index()
-        total_count = self.buffer.totalcount()
+        total_count = self.buffer.totalcount(channel=0)
+
+        start_time = time.time()
+        if total_count - self.last_plotted_index_analog_in_0 >= self.CHUNK_PLOT:
+            try:
+                times, items = self.buffer.read(
+                    self.last_plotted_index_analog_in_0,
+                    extend=self.CHUNK_PLOT,
+                    channel=0,
+                )
+                self.time_analog_in_0 = np.concatenate((self.time_analog_in_0, times))[
+                    -self.PLOT_HISTORY :
+                ]
+                self.data_analog_in_0 = np.concatenate((self.data_analog_in_0, items))[
+                    -self.PLOT_HISTORY :
+                ]
+                self.line_analog_in_0.setData(
+                    self.time_analog_in_0,
+                    self.data_analog_in_0,
+                )
+                self.last_plotted_index_analog_in_0 += self.CHUNK_PLOT
+            except IndexError:
+                log.error("Could not acces the data from the buffer for plotting")
+            end_time = time.time()
+            log.debug(f"total time for plotting {end_time - start_time}")
+        else:
+            pass
+
+    def update_plot_1(self):
+        total_count = self.buffer.totalcount(channel=1)
 
         start_time = time.time()
-        if total_count - self.last_plotted_index >= self.CHUNK_PLOT:
+        if total_count - self.last_plotted_index_analog_in_1 >= self.CHUNK_PLOT:
             try:
                 times, items = self.buffer.read(
-                    self.last_plotted_index,
+                    self.last_plotted_index_analog_in_1,
                     extend=self.CHUNK_PLOT,
+                    channel=1,
                 )
-                self.time = np.concatenate((self.time, times))[-self.PLOT_HISTORY :]
-                self.data = np.concatenate((self.data, items))[-self.PLOT_HISTORY :]
-                self.line.setData(
-                    self.time,
-                    self.data,
+                self.time_analog_in_1 = np.concatenate((self.time_analog_in_0, times))[
+                    -self.PLOT_HISTORY :
+                ]
+                self.data_analog_in_1 = np.concatenate((self.data_analog_in_0, items))[
+                    -self.PLOT_HISTORY :
+                ]
+                self.line_analog_in_1.setData(
+                    self.time_analog_in_1,
+                    self.data_analog_in_1,
                 )
-                self.last_plotted_index += self.CHUNK_PLOT
+                self.last_plotted_index_analog_in_1 += self.CHUNK_PLOT
             except IndexError:
                 log.error("Could not acces the data from the buffer for plotting")
             end_time = time.time()
@@ -70,19 +117,26 @@ class Continously:
 
     def stop_plotting(self):
         self.timer.stop()
-        if self.last_plotted_index > 0:
+        if self.last_plotted_index_analog_in_0 > 0:
             total_count = self.buffer.totalcount()
             times, items = self.buffer.read(
-                self.last_plotted_index,
-                extend=total_count - self.last_plotted_index,
+                self.last_plotted_index_analog_in_0,
+                extend=total_count - self.last_plotted_index_analog_in_0,
+            )
+            self.time_analog_in_0 = np.concatenate((self.time_analog_in_0, times))[
+                -self.PLOT_HISTORY :
+            ]
+            self.data_analog_in_0 = np.concatenate((self.data_analog_in_0, items))[
+                -self.PLOT_HISTORY :
+            ]
+            self.line_analog_in_0.setData(
+                self.time_analog_in_0,
+                self.data_analog_in_0,
             )
-            self.time = np.concatenate((self.time, times))[-self.PLOT_HISTORY :]
-            self.data = np.concatenate((self.data, items))[-self.PLOT_HISTORY :]
-            self.line.setData(
-                self.time,
-                self.data,
+            self.last_plotted_index_analog_in_0 += (
+                total_count - self.last_plotted_index_analog_in_0
             )
-            self.last_plotted_index += total_count - self.last_plotted_index
 
     def refresh(self):
-        self.continous_ax.enableAutoRange()
+        self.analog_in_0.enableAutoRange()
+        self.analog_in_1.enableAutoRange()