From f263caa2db965ded17cbeb02280dc0174bd73587 Mon Sep 17 00:00:00 2001
From: Nao Pross <np@0hm.ch>
Date: Sat, 27 Nov 2021 14:46:47 +0100
Subject: Move network code and update nix files

---
 src/gui.py                   | 42 ++----------------------------------------
 src/net.py                   | 35 +++++++++++++++++++++++++++++++++++
 src/nix/numpy-ringbuffer.nix | 12 ++++++++++++
 src/shell.nix                | 10 +++++++++-
 4 files changed, 58 insertions(+), 41 deletions(-)
 create mode 100644 src/nix/numpy-ringbuffer.nix

(limited to 'src')

diff --git a/src/gui.py b/src/gui.py
index a0db566..b2cbebb 100755
--- a/src/gui.py
+++ b/src/gui.py
@@ -13,7 +13,6 @@ import signal
 
 # Mathematics
 import numpy as np
-from numpy_ringbuffer import RingBuffer
 
 # For debugging
 import logging
@@ -37,42 +36,6 @@ setup_dearpygui()
 # Show demo for dev
 show_demo()
 
-#================================================
-# Network classes
-
-class network_plot(net.udpsource):
-    def __init__(self, url, nsamples, **kwargs):
-        net.udpsource.__init__(self, url)
-
-        self.nsamples = nsamples
-        self.plot = plot(**kwargs)
-
-        # create buffer and fill with zeroes
-        self.buffer = RingBuffer(capacity=nsamples, dtype=(np.float, 2))
-        for i in range(nsamples):
-            # TODO: remove random data used for testing
-            self.buffer.append(np.array([i, 1 + np.random.rand() / 5]))
-
-        self.bind()
-
-    def __enter__(self):
-        return self.plot.__enter__()
-
-    def __exit__(self, t, val, tb):
-        self.plot.__exit__(t, val, tb)
-
-    @property
-    def x_data(self):
-        return np.array(self.buffer[:,0])
-
-    @property
-    def y_data(self):
-        return np.array(self.buffer[:,1])
-
-    def refresh(self, series_tag):
-        # set_value(series_tag, [self.x_data, self.y_data])
-        pass
-
 
 #================================================
 # GUI Callback functions
@@ -135,8 +98,7 @@ with window(label="RX DSP Flow Graph", width=800, height=400, pos=(25,25), tag="
 #================================================
 # Network plots Window
 
-recv_plot = network_plot(url="udp://localhost:31415", nsamples=100, label="Test", height=300, width=800)
-
+recv_plot = net.network_plot(url="udp://localhost:31415", nsamples=100, label="Test", height=300, width=800)
 
 plots = {
     recv_plot: "plt_ampl"
@@ -158,7 +120,7 @@ show_viewport()
 # Main loop
 while is_dearpygui_running():
     for plt, tag in plots.items():
-        plt.refresh(tag)
+        plt.refresh_series(tag)
 
     render_dearpygui_frame()
 
diff --git a/src/net.py b/src/net.py
index 6bd71ac..2c91bb8 100644
--- a/src/net.py
+++ b/src/net.py
@@ -3,6 +3,8 @@ import socket
 from urllib.parse import urlparse
 
 import numpy as np
+from numpy_ringbuffer import RingBuffer
+import dearpygui.dearpygui as dpg
 
 
 class udpsource:
@@ -31,3 +33,36 @@ class udpsource:
         else:
             return None
 
+
+class network_plot(udpsource):
+    def __init__(self, url, nsamples, **kwargs):
+        udpsource.__init__(self, url)
+
+        self.nsamples = nsamples
+        self.plot = dpg.plot(**kwargs)
+
+        # create buffer and fill with zeroes
+        self.buffer = RingBuffer(capacity=nsamples, dtype=(float, 2))
+        for i in range(nsamples):
+            # TODO: remove random data used for testing
+            self.buffer.append(np.array([i, 1 + np.random.rand() / 5]))
+
+        self.bind()
+
+    def __enter__(self):
+        return self.plot.__enter__()
+
+    def __exit__(self, t, val, tb):
+        self.plot.__exit__(t, val, tb)
+
+    @property
+    def x_data(self):
+        return np.array(self.buffer[:,0])
+
+    @property
+    def y_data(self):
+        return np.array(self.buffer[:,1])
+
+    def refresh_series(self, tag):
+        dpg.set_value(tag, [self.x_data, self.y_data])
+        pass
diff --git a/src/nix/numpy-ringbuffer.nix b/src/nix/numpy-ringbuffer.nix
new file mode 100644
index 0000000..b97b01a
--- /dev/null
+++ b/src/nix/numpy-ringbuffer.nix
@@ -0,0 +1,12 @@
+{ lib, pkgs, buildPythonPackage, fetchPypi, isPy38, autoPatchelfHook }:
+
+buildPythonPackage rec {
+  pname = "numpy_ringbuffer";
+  version = "0.2.1";
+  src = fetchPypi {
+    inherit pname version;
+    sha256 = "1vrw38jb3cy9m0c1xxvkk5sf1hpgv58x649a2nnqi9ljdl5wcydc";
+  };
+
+  buildInputs =  (with pkgs.python3Packages; [ numpy ]);
+}
diff --git a/src/shell.nix b/src/shell.nix
index 666bab3..22771e2 100644
--- a/src/shell.nix
+++ b/src/shell.nix
@@ -7,10 +7,18 @@ let
     isPy38 = pkgs.python38Packages.isPy38;
   };
 
+  numpy-ringbuffer = callPackage ./nix/numpy-ringbuffer.nix {
+    buildPythonPackage = pkgs.python38Packages.buildPythonPackage;
+    fetchPypi = pkgs.python38Packages.fetchPypi;
+    isPy38 = pkgs.python38Packages.isPy38;
+  };
+
 in mkShell {
-  buildInputs = [ dearpygui ] ++ (with pkgs; [
+  buildInputs = [ dearpygui numpy-ringbuffer ] ++ (with pkgs; [
     gnuradio
     python38Packages.setuptools
+    python38Packages.matplotlib
+    python38Packages.numpy
     # gnuradio block dev dependencies
     cmake ninja pkg-config log4cpp mpir boost175 gmp volk doxygen
     python38Packages.pybind11
-- 
cgit v1.2.1